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

1 回表示 (過去 30 日間)
Igor Batoukhtine
Igor Batoukhtine 2016 年 4 月 16 日
回答済み: Ced 2016 年 4 月 16 日
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

採用された回答

Ced
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');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeScopes and Data Logging についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by