Moving Variance
Moving variance
Libraries:
DSP System Toolbox /
Statistics
Description
The Moving Variance block computes the moving variance 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 variance. In the sliding window method, a window of specified length moves over the data sample by sample, and the block computes the variance over the data in the window. In the exponential weighting method, the block subtracts each sample of the data from the average, squares the difference, and multiplies the squared result by a weighting factor. The block then computes the variance by adding all the weighted data. For more details on these methods, see Algorithms.
Examples
Ports
Input
x — Data input
vector  matrix
The block computes the moving variance of the data specified at this input port. Specify real or complexvalued multichannel inputs of the size mbyn, where m ≥ 1 and n ≥ 1.
When the Allow arbitrary frame length for fixedsize input signals parameter appears and is not selected, and you input a fixedsize signal, the frame length must be a multiple of the hop size (window length − overlap length). In all other cases, the input frame length can be arbitrary.
The block accepts variablesize inputs (frame length changes during simulation). When you input a variablesize signal, the frame length of the signal can be arbitrary.
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 variance output
column vector  row vector  matrix
Moving variance output, returned as a vector or a matrix. The block computes the moving variance based on the Method parameter settings using either the sliding window method or the exponential weighting method. For more details, see Algorithms.
This table provides more details on the dimensions of the output signal.
Input Signal  Input Dimensions  Output Dimensions When Allow arbitrary frame length for fixedsize input signals Appears  Output Dimensions When Allow arbitrary frame length for fixedsize input signals Does Not Appear 

Fixedsize signal  mbyn, where m is a multiple of the hop size (window length − overlap length)  (m/hop size)byn  mbyn 
Fixedsize signal  mbyn, where m is not a multiple of the hop size (window length − overlap length) 
If you do not select Allow arbitrary frame length for fixedsize input signals, the block errors.  mbyn 
Variablesize signal  mbyn  ceil (m/hop
size)byn  mbyn 
When the output has an upper bound size of
ceil
(m/hop size)byn, during
simulation, the size of the first dimension varies within this bound and the size of the
second dimension remains constant. For an example that shows this behavior, see Compute Moving Variance of Noisy Square Wave Signal.
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 — Moving variance 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 variance over the data in the window.Exponential weighting
— The block subtracts each sample of the data from the average, squares the difference, and multiplies the squared result by a weighting factor. The block then computes the variance by adding all the weighted data. The magnitude of the weighting factors decreases exponentially as the age of the data increases, but the magnitude never reaches zero.
For more details on these methods, see Algorithms.
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 variance of the current sample with respect to all the previous samples in the channel.
Dependencies
To enable this parameter, 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
To enable this parameter, set Method to Sliding
window
and select the Specify window length check
box.
Overlap length — Overlap length between windows
3
(default)  nonnegative integer
Specify the overlap length between sliding windows as a nonnegative integer. The value of overlap length varies in the range [0, Window length − 1].
Dependencies
To enable this parameter, set Method to Sliding
window
and select the Specify window length check
box.
Allow arbitrary frame length for fixedsize input signals — Allow arbitrary frame length for fixedsize input signals
off (default)  on
Specify whether fixedsize input signals (whose size does not change during simulation) can have an arbitrary frame length, where the frame length does not have to be a multiple of the hop size. Hop size is defined as Window length − Overlap length. The block uses this parameter setting only for fixedsize input signals and ignores this parameter if the input has a variablesize.
When the input signal is a variablesize signal, the signal can have arbitrary frame length, that is, the frame length does not have to be a multiple of the hop size.
For fixedsize input signals, if you:
Select the Allow arbitrary frame length for fixedsize input signals parameter, the frame length of the signal does not have to be a multiple of the hop size. If the input is not a multiple of the hop size, then the output is generally a variablesize signal. Therefore, to support arbitrary input size, the block must also support variablesize operations, which you can enable by selecting the Allow arbitrary frame length for fixedsize input signals parameter.
Clear the Allow arbitrary frame length for fixedsize input signals parameter, the input frame length must be a multiple of the hop size.
Dependencies
To enable this parameter, 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
To enable this parameter, 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
To enable this parameter, set Method to Exponential
weighting
and clear the Specify forgetting factor from input
port check box.
Simulate using — Type of simulation to run
Interpreted execution
(default)  Code generation
Specify the type of simulation to run. You can set this parameter to:
Interpreted execution
–– Simulate model using the MATLAB^{®} interpreter. This option shortens startup time.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 subsequent simulations.
Block Characteristics
Data Types 

Multidimensional Signals 

VariableSize Signals 

Algorithms
Sliding Window Method
In the sliding window method, the output at the current sample is the variance of the current sample with respect to the data in the window. To compute the first output sample, the algorithm waits until it receives the hop size number of input samples. Hop size is defined as window length – overlap length. Remaining samples in the window are considered to be zero. As an example, if the window length is 5 and the overlap length is 2, then the algorithm waits until it receives 3 samples of input to compute the first sample of the output. After generating the first output, it generates the subsequent output samples for every hop size number of input samples.
When you do not specify the window length, the algorithm chooses an infinite window length. In this mode, the output is the moving variance of the current sample with respect to all previous samples in the channel.
Consider an example of computing the moving variance of a streaming input data using the sliding window method. The algorithm uses a window length of 4 and an overlap length of 3. With each input sample that comes in, the window of length 4 moves along the data.
Exponential Weighting Method
In the exponential weighting method, the moving variance is computed recursively using these formulas:
$$\begin{array}{l}{s}^{2}{}_{N,\lambda}=\frac{1}{{v}_{N,\lambda}}{\displaystyle \sum _{k=1}^{N}{\lambda}^{Nk}}{\left[{x}_{k}{\overline{x}}_{N,\lambda}\right]}^{2}\\ {v}_{N,\lambda}=\frac{2\lambda (1{\lambda}^{N1})}{(1\lambda )(1+\lambda )}\end{array}$$
To compute the moving variance, the algorithm implements these equations recursively.
$${s}^{2}{}_{N,\lambda}$$ — Moving variance of the current data sample with respect to the rest of the data in the channel.
$${\overline{x}}_{N,\lambda}$$ — Moving average at the current sample. For details on computing the moving average, see
dsp.MovingAverage
.$${\left[{x}_{k}{\overline{x}}_{N,\lambda}\right]}^{2}$$ — Difference between each data sample and the average of the data, squared.
$$\sum _{k=1}^{N}{\lambda}^{Nk}}{\left[{x}_{k}{\overline{x}}_{N,\lambda}\right]}^{2$$ — Difference between each data sample and the average of the data, squared and multiplied with the forgetting factor. All the squared terms are added.
$$\frac{1}{{v}_{N,\lambda}}$$ — Weighting factor applied to the sum.
λ — Forgetting factor you can specify through the
ForgettingFactor
property.
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 variance, 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 past samples are given an equal weight.
Consider an example of computing the moving variance using the exponential weighting method. The forgetting factor is 0.9.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced in R2016bR2024a: Change in the default value of Simulate using parameter
The default value of the Simulate using parameter is now
Interpreted execution
. With this change, the block uses the
MATLAB interpreter for simulation by default.
R2022b: New Overlap length parameter
Starting in R2022b, you can specify the overlap length between sliding windows using the Overlap length parameter.
R2022b: Support for arbitrary input frame length
Moving Variance block supports input signals with arbitrary frame lengths when the:
Input signal is a fixedsize signal (frame length does not change during simulation) and you select the Allow arbitrary frame length for fixedsize input signals parameter (if enabled).
Input signal is a variablesize signal (frame length changes during simulation).
When this block supports an arbitrary frame length input signal, the input frame length does not have to be a multiple of the hop size.
See Also
Blocks
 Variance  Moving Average  Moving Maximum  Moving Minimum  Moving Standard Deviation  Moving RMS  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)