Averaging a signal at periodic intervals in Simulink
Show older comments
I am trying to compute the average of a signal at periodic intervals. The signal is sampled at some rate say 0.1s, i want to compute avegare after every one second in simulink. I tried to integrate the signal over every second and then multipy it one second to get an avg value of signal. This method is not valid as the integraor is causing my function to blow up.
Please share any possible methods, Thanks.
Accepted Answer
More Answers (1)
Jim Riggs
on 15 Feb 2019
2 votes
In my mind, the easiest way to do this is to create a Matlab function to do it, and embed the function into a Matlab block in Simulink. It's a lot harder to do this kind of thing in Simulink because of the complex logic involved. (Stateflow helps a lot)
Make a matlab function that stores the signal in a circular buffer. Once you have enough samples in the buffer, you can output the average value over the N=10 samples. You can make the code so that it only outpus a new value every 1 second, or it can output every 0.1 s. (i.e. a "sliding average" ) using the 10 most recent samples.
4 Comments
Fargham Ahmad
on 15 Feb 2019
The implementation looks like this in Simulink.
I call the Matlab function WindowAverage. The inpus are the input signal X, and the window size Navg. This is the number of samples you want to average. The third input, Nout, is the number of samples between outputs. So setting Navg to 10 will give you an average of 10 sampples, and settig Nout to 10 will give an output calculation every 10 samples. If you set Nout to 1, you will get a running average of the last 10 samples output every pass (but there will be a 10 sample delay for the first output while the buffer is being filled.

Inside the Matlab Function block, there is Matlab code to define the WindowAverage function. This function calls two other functions to manage the buffer pointer, so these two functions have to be defined and placed in your matlab path. These functions are :
pointer_increment and pointer_relative.
Function pointer_increment handles keeping track of the curent pointer position in the ring buffer.
Function pointer_relative gives relative pointer values from the current pointer position in the buffer. This is how you figure out what the most recent 10 samples are.
Unfortunately, I cannot upload source code files, so you will have to re-code the functions based on the attached pdf file.
Here is a sample output for a linear ramp test case:
This is a 0.1 sample rate, Navg = 10 and Nout = 10.

This is for Navg = 10 and Nout = 5

And this one is Navg = 10 and Nout = 2

Notice how they all have a 10 sample delay at the start.
Notice also that this test case runs at 0.1 time step for 10 seconds, and therefore generates 100 calls to the WindowAverage function. I set the buffer size to 50 in the function to verify that it wrapped correctly back to the start when it reaches the end.
Fargham Ahmad
on 16 Feb 2019
Categories
Find more on Simulink 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!