How can I calculate running mean over 40 samples in Simulink?

3 views (last 30 days)
Hello,
I've got the following signal simulated in Simulink:
No I would like to return the average of this signal over a sample time of 40 secondes and running. So average 1 would be over the periode 0-40. Average 2 over the period 1-41, average 3 over 2-42 etc. In Matlab I did this with the following code:
if (tijd >= Reistijd)
inc = inc + 1;
ii = ii + 1;
avgrho(ii) = mean(rho_measurement(inc-Reistijd:inc));
end

Accepted Answer

Ced
Ced on 16 Apr 2016
What you are describing is known as a moving average filter.
If you want to do this in simulink (i.e. while running the simulation), you could implement it as a filter. You can create it with blocks, but for a window of 40 samples, that would be a pain. I would thus suggest to either use an S function block, or define the filter coefficients
N = 40;
b = ones(1,N)/N;
a = 1
and use a transfer function block. There is also a Simulink tutorial on how to create an actual simulink "object" for a moving average filter, see here
If you want to do this as post-processing in Matlab, there are plenty of ways to do this. One possibility is to view it as a convolution, i.e.
N = 40;
t = linspace(0,2,100);
x_raw = sin(2*pi*t) + randn(1,length(t));
mav_filt = ones(1,N)/N;
x_mav = conv(x_raw,mav_filt,'same');

More Answers (0)

Categories

Find more on Schedule Model Components in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!