MATLAB Examples

Compute Moving Average Using System Objects

Create a dsp.MovingAverage System object™ to compute the 10-point moving average of the streaming signal. Use a dsp.MatFileReader System object to read data from the accelerometer MAT file. View the moving average output in the time scope.

The System objects automatically index the data into frames. Choose a frame size of 714 samples. There are 7140 samples or 10 frames of data in each column of the MAT file. Each iteration loop computes the moving average of 1 frame of data.

frameSize = 714;
reader = dsp.MatFileReader('SamplesPerFrame',frameSize,...
    'Filename','LSM9DS1accelData73.mat','VariableName','data');
movAvg = dsp.MovingAverage(10);
scope = dsp.TimeScope('NumInputPorts',2,'SampleRate',119,'YLimits',[-2500 2500],...
    'ChannelNames',{'Input','Moving Average along X data'},'TimeSpan',60,'ShowLegend',true);

while ~isDone(reader)
    accel = reader();
    avgData = movAvg(accel);
    scope(accel(:,1),avgData(:,1));
end

The processing loop is very simple. The System Objects handle data indexing and states automatically.