%% Compute Moving Average Using Only MATLAB Functions
% This example shows how to compute the moving average of a signal using the movmean function.
% The movmean function computes the 10-point moving average of the noisy data coming from an accelerometer. The three columns in this data represent the linear acceleration of the accelerometer in the  X-axis, Y-axis, and Z-axis, respectively. All the data is available in a MAT file. Plot the moving average of the X-axis data.

winLen = 10;
accel = load('LSM9DS1accelData73.mat');
movAvg = movmean(accel.data,winLen,'Endpoints','fill');
plot([accel.data(:,1),movAvg(:,1)]);
legend('Input','Moving average along X data');

% The data is not very large (7140 samples in each column) and is entirely available for processing. The movmean function is designed to handle such one-time computations. However, if the data is very large, such as in the order of GB, or if the data is a live stream that needs to be processed in real time, then use System objects. The System objects divide the data into segments called frames and process each frame in an iteration loop seamlessly. This approach is memory efficient, because only one frame of data is processed at any given time. Also, the System objects are optimized to handle states internally.
% Copyright 2012 The MathWorks, Inc.