![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174106/image.png)
How can I calculate running mean over 40 samples in Simulink?
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I've got the following signal simulated in Simulink:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/161414/image.png)
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
0 个评论
采纳的回答
Ced
2016-4-16
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174106/image.png)
0 个评论
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!