Moving Average
Moving average
 Library:
DSP System Toolbox / Statistics
Description
The Moving Average block computes the moving average of the input signal along each channel independently over time. The block uses either the sliding window method or the exponential weighting method to compute the moving average. In the sliding window method, a window of specified length moves over the data sample by sample, and the block computes the average over the data in the window. In the exponential weighting method, the block multiplies the data samples with a set of weighting factors and then sums the weighted data to compute the average. For more details on these methods, see Algorithms.
Ports
Input
x
— Data input
column vector  row vector  matrix
Data over which the block computes the moving average. The block accepts realvalued or complexvalued multichannel inputs, that is, mbyn size inputs, where m ≥ 1 and n ≥ 1. The block also accepts variablesize inputs. During simulation, you can change the size of each input channel. However, the number of channels cannot change.
This port is unnamed until you set Method to
Exponential weighting
and select the Specify forgetting
factor from input port parameter.
Data Types: single
 double
Complex Number Support: Yes
lambda
— Forgetting factor
positive real scalar in the range (0,1]
The forgetting factor determines how much weight past data is given. A forgetting factor of 0.9 gives more weight to the older data than does a forgetting factor of 0.1. A forgetting factor of 1.0 indicates infinite memory – all previous samples are given an equal weight.
Dependencies
This port appears when you set Method to Exponential
weighting
and select the Specify forgetting factor from input
port parameter.
Data Types: single
 double
Output
Port_1
— Moving average output
column vector  row vector  matrix
The size of the moving average output matches the size of the input. The block uses either the sliding window method or the exponential weighting method to compute the moving average, as specified by the Method parameter. For more details, see Algorithms.
Data Types: single
 double
Complex Number Support: Yes
Parameters
If a parameter is listed as tunable, then you can change its value during simulation.
Method
— Averaging method
Sliding window
(default)  Exponential weighting
Sliding window
— A window of length Window length moves over the input data along each channel. For every sample the window moves over, the block computes the average over the data in the window.Exponential weighting
— The block multiplies the samples by a set of weighting factors. The magnitude of the weighting factors decreases exponentially as the age of the data increases, but the magnitude never reaches zero. To compute the average, the algorithm sums the weighted data.
Specify window length
— Flag to specify window length
on (default)  off
When you select this check box, the length of the sliding window is equal to the value you specify in Window length. When you clear this check box, the length of the sliding window is infinite. In this mode, the block computes the average of the current sample and all previous samples in the channel.
Dependencies
This parameter appears when you set Method to Sliding
window
.
Window length
— Length of sliding window
4 (default)  positive scalar integer
Specifies the length of the sliding window in samples.
Dependencies
This parameter appears when you set Method to Sliding
window
and select the Specify window length check
box.
Specify forgetting factor from input port
— Flag to specify forgetting factor
off (default)  on
When you select this check box, the forgetting factor is input through the lambda port. When you clear this check box, the forgetting factor is specified on the block dialog through the Forgetting factor parameter.
Dependencies
This parameter appears only when you set Method to
Exponential weighting
.
Forgetting factor
— Exponential weighting factor
0.9 (default)  positive real scalar in the range (0,1]
The forgetting factor determines how much weight past data is given. A forgetting factor of 0.9 gives more weight to the older data than does a forgetting factor of 0.1. A forgetting factor of 1.0 indicates infinite memory – all previous samples are given an equal weight.
Tunable: Yes
Dependencies
This parameter appears when you set Method to
Exponential weighting
and clear the Specify forgetting
factor from input port check box.
Simulate using
— Type of simulation to run
Code generation
(default)  Interpreted execution
Specify the type of simulation to run as one of the following:
Code generation
–– Simulate model using generated C code. The first time you run a simulation, Simulink^{®} generates C code for the block. The C code is reused for subsequent simulations, as long as the model does not change. This option requires additional startup time but provides faster simulation speed thanInterpreted execution
.Interpreted execution
–– Simulate model using the MATLAB^{®} interpreter. This option shortens startup time but has slower simulation speed thanCode generation
.
Block Characteristics
Data Types 

Multidimensional Signals 

VariableSize Signals 

Algorithms
Sliding Window Method
In the sliding window method, the output for each input sample is the average of the current sample and the Len – 1 previous samples. Len is the length of the window in samples. To compute the first Len – 1 outputs, when the window does not have enough data yet, the algorithm fills the window with zeros. As an example, to compute the average when the second input sample comes in, the algorithm fills the window with Len – 2 zeros. The data vector, x, is then the two data samples followed by Len – 2 zeros.
When you do not specify the window length, the algorithm chooses an infinite window length. In this mode, the output is the moving average of the current sample and all the previous samples in the channel.
For an example, see Sliding Window Method and Exponential Weighting Method.
Exponential Weighting Method
In the exponential weighting method, the moving average is computed recursively using these formulas:
$$\begin{array}{l}{w}_{N,\lambda}=\lambda {w}_{N1,\lambda}+1\\ {\overline{x}}_{N,\lambda}=\left(1\frac{1}{{w}_{N,\lambda}}\right){\overline{x}}_{N1,\lambda}+\left(\frac{1}{{w}_{N,\lambda}}\right){x}_{N}\end{array}$$
$${\overline{x}}_{N,\lambda}$$ — Moving average at the current sample
$${x}_{N}$$ — Current data input sample
$${\overline{x}}_{N1,\lambda}$$ — Moving average at the previous sample
λ — Forgetting factor
$${w}_{N,\lambda}$$ — Weighting factor applied to the current data sample
$$\left(1\frac{1}{{w}_{N,\lambda}}\right){\overline{x}}_{N1,\lambda}$$ — Effect of the previous data on the average
For the first sample, where N = 1, the algorithm chooses $${w}_{N,\lambda}$$ = 1. For the next sample, the weighting factor is updated and used to compute the average, as per the recursive equation. As the age of the data increases, the magnitude of the weighting factor decreases exponentially and never reaches zero. In other words, the recent data has more influence on the current average than the older data.
The value of the forgetting factor determines the rate of change of the weighting factors. A forgetting factor of 0.9 gives more weight to the older data than does a forgetting factor of 0.1. A forgetting factor of 1.0 indicates infinite memory. All the previous samples are given an equal weight.
For an example, see Sliding Window Method and Exponential Weighting Method.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
See Also
Blocks
 Moving RMS  Moving Maximum  Moving Minimum  Moving Standard Deviation  Moving Variance  Median Filter
Objects
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)