# How to derive the moving average of a time series generated in simulink?

8 views (last 30 days)
yingyu hua on 2 May 2024
Commented: yingyu hua on 4 May 2024
Hi,
I have a dynamics system under excitation and I want to compute the moving average of the quantity "POW" I defined. In my case, the moving window has length 100, the sampling time was 0.01. So I expect to have the moving average "POW" every second.
clear all
clc
m1=0.947; m2=m1; Cp=1.1;
J=0.1*0.15^2/2;
L1=0.125;L2=0.035;
g=9.8;FI0=pi/2;
Kem=0.48; Rc=3.74;Re=5;
Cem=Kem^2/(Rc+Re);
TT=500;dt=1E-2;
tt=[0:dt:TT]';
fl=0.1;
Ag=9.8*0.4;fg=1;
accg=Ag*sin(2*pi*fg*tt);
sim("buffermovemean.slx")
However, if I run the "questiononbuffer.m", the error message is :
"Error using questiononbuffer
The signal at 'Output Port 1' of 'buffermovemean/Moving Average2' is a variable-size signal with a nondiscrete sample time. The
sample time for any variable-size signal must be discrete."
So I tried to use buffer to transform the time series "POW" with fixed frame length 100 by the block "signal from workspace" and then apply moving-average, but the error message appears as:
Error using questiononbuffer
Error evaluating parameter 'X' in 'buffermovemean/Signal From Workspace'
Caused by:
Error using questiononbuffer
Unrecognized function or variable 'POW'.
Error using questiononbuffer
Variable 'POW' does not exist.
Suggested Actions:
• Load a file into base workspace. - Fix
• Create a new variable. - Fix
So my question is if the time series is generated in real time in the simulink, how to derive its moving average for a fixed duration like in my case? PS, when I use matlab 2019b, I could directly connect the moving-average block after the time series to get its moving average. But in matlab 2023b, error reports. How to fix it?
@Walter Roberson
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

Poorna on 3 May 2024
Hi yingyu,
I see you are facing issues while trying to compute the moving average of the output signal. I was able to reproduce both of the issues mentioned in your query.
The reason you are getting the error: "variable-size signal must be discrete" is because the output of the "moving average" block is a variable-size signal and variable-size signals must have discrete sample time. The reason the output is a variable-size signal is that you have enabled the "Allow arbitrary frame length for fixed-size input signals".
Now, to resolve the problem, here are the steps I suggest you to take to get the desired output.
• I see, you want to get the average of every 100 samples without any overlap. In order get this output, you should set the window length property of the "moving average" block to 100, and the "overlap length" property to 0.
• Disable the "Allow arbitrary frame length for fixed-size input signals" checkbox. We don't need this as we are going to provide the input to the "moving average" block in multiples of the hopsize, which is now 100 by using the "Buffer" block.
• Pass the input through a "Buffer" block before passing it to the "moving average" block. Set the Buffer size to 100. This will make sure that the input to the "moving average" block is in multiples of 100.
• Now, the input to a buffer block must be a discrete-time signal. To make the input discrete-time, change the sample time of all the sources(like constant blocks) to 0.01. Replace the continuous-time integrator with a discrete-time integrator. This will make sure that the input to the "Buffer" block is discrete.
Now, you can get the required output without any errors by simulating the model.
Note that if you want to follow the second approach that you mentioned which is by logging the signal, then you could simply set the "Save format" property of the "to workspace" block to "Array" format and then read the data. Make sure that the signal is already written to the workspace before reading.
To know more about the "moving average" block and its different properties refer to the following documentation:
To know more about the other blocks used refer to the following documentation:
Hope this Helps!
##### 1 CommentShow -1 older commentsHide -1 older comments
yingyu hua on 4 May 2024
Thank you for your prompt reply! now I get the buffer block only accepts the discrete system output (the last suggestion)! Very helpful! Thanks!

Sign in to comment.

### Categories

Find more on Sources in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!