Moving Variance
R2026bMoving variance
Moving Variance block
To add a block to a model, double-click the canvas and start typing the block name. Then, select the block from the list.
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.
You can change the window length during simulation by selecting the Make window length tunable parameter. Specify the tunable window length from the block dialog box by updating the Window length parameter or through the input port WL by selecting the Specify window length from input port parameter. For more details, see Algorithms. (since R2026b)
Examples
Compute Moving Variance of Noisy Square Wave Signal
Compare the sliding window variance method and the exponentially weighted variance method in Simulink® using the Moving Variance block.
Ports
Input
The block computes the moving variance of the data specified at this input port. Specify real- or complex-valued multichannel inputs of the size m-by-n, where m ≥ 1 and n ≥ 1.
When the Allow arbitrary frame length for fixed-size input signals parameter appears and is not selected, and you input a fixed-size 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 variable-size inputs (frame length changes during simulation). When you input a variable-size signal, the frame length of the signal can be arbitrary.
This port is unnamed until you enable the WL input port or the lambda input port.
Data Types: single | double
Complex Number Support: Yes
Specify the forgetting factor as a nonnegative real scalar in the range [0,1]. The forgetting factor determines how much weight the block gives the past data. 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 and all previous samples are given an equal weight. A forgetting factor of 0 indicates no memory and the past samples have no weight on the current computation.
Dependencies
To enable this port, set Method to Exponential
weighting and select Specify forgetting factor from input
port.
Data Types: single | double
Since R2026b
Specify the tunable window length as a positive integer. The block uses this value as the sliding window length for computing the moving variance.
When you select Auto-adjust invalid window length, the block adjusts invalid window length values from the input port and issues a warning. For more information on what values are invalid, see the Auto-adjust invalid window length parameter description.
When you clear the Auto-adjust invalid window length parameter, the block throws an error for invalid window length values.
Dependencies
To enable this port, set Method to Sliding
window, and select these parameters:
Specify window length
Make window length tunable
Specify window length from input port
Data Types: single | double
Output
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 fixed-size input signals Appears | Output Dimensions When Allow arbitrary frame length for fixed-size input signals Does Not Appear |
|---|---|---|---|
| Fixed-size signal | m-by-n, where m is a multiple of the hop size (window length − overlap length) | (m/hop size)-by-n | m-by-n |
| Fixed-size signal | m-by-n, where m is not a multiple of the hop size (window length − overlap length) |
If you do not select Allow arbitrary frame length for fixed-size input signals, the block errors. | m-by-n |
| Variable-size signal | m-by-n | ceil(m/hop
size)-by-n | m-by-n |
When the output has an upper bound size of
ceil(m/hop size)-by-n, during
simulation, the size of the first dimension varies in 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.
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.
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 parameter, 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.
Specify the length of the sliding window in samples as a positive integer.
When you select the Make window length tunable parameter, the window length you specify is tunable and must be less than or equal to the value you specify in the Maximum window length parameter. When you clear the Make window length tunable parameter, the window length is fixed. (since R2026b)
Dependencies
To enable this parameter, set Method to Sliding
window and select Specify window length. In addition, if
you select Make window length tunable, you must clear the
Specify window length from input port parameter.
Specify the number of overlapped samples 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, select Specify window length, and clear
Make window length tunable.
Since R2026b
Select this parameter to make the window length tunable, that is, you can change the window length during simulation through the block dialog box by updating the Window length parameter or through the input port WL by selecting the Specify window length from input port parameter.
When you clear this parameter, the window length is fixed and you cannot change it during simulation. Specify the window length through the Window length parameter.
Dependencies
To enable this parameter, set Method to Sliding
window and select Specify window length.
Since R2026b
When you select this parameter, the block reads the tunable window length from the input port, WL. When you clear this parameter, the block reads the tunable window length from the Window length parameter.
Dependencies
To enable this parameter, set Method to Sliding
window, and select these parameters:
Specify window length
Make window length tunable
Since R2026b
Specify the maximum value of the tunable window length as a positive integer. The window length you specify during simulation cannot exceed this value.
Dependencies
To enable this parameter, set Method to Sliding
window, and select these parameter:
Specify window length
Make window length tunable
Since R2026b
When you select this parameter, the block adjusts invalid window length values from the input port and issues a warning.
If the tunable window length value is greater than Maximum window length, the block uses Maximum window length.
If the tunable window length value is less than 1, the block uses 1.
If the tunable window length value is not an integer, the block uses the floored value.
When you clear this parameter, the block throws an error for invalid window length values.
Dependencies
To enable this parameter, set Method to Sliding
window, and select these parameters:
Specify window length
Make window length tunable
Specify window length from input port
Specify whether fixed-size 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 fixed-size input signals and ignores this parameter if the input has a variable-size.
When the input signal is a variable-size 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 fixed-size input signals, if you:
Select the Allow arbitrary frame length for fixed-size 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 variable-size signal. So, to support arbitrary input size, the block must also support variable-size operations, which you can enable by selecting the Allow arbitrary frame length for fixed-size input signals parameter.
Clear the Allow arbitrary frame length for fixed-size 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, select Specify window length, and clear
Make window length tunable.
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 box through the Forgetting factor parameter.
Dependencies
To enable this parameter, set Method to Exponential
weighting.
Specify the exponential weighting factor as a nonnegative real scalar in the range [0,1]. This value determines how much weight the 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 and all the past samples are given an equal weight. A forgetting factor of 0 indicates no memory and the past samples have no weight on the current computation.
Tunable: Yes
Dependencies
To enable this parameter, set Method to Exponential
weighting and clear the Specify forgetting factor from input
port check box.
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 |
|
Variable-Size Signals |
|
Algorithms
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.
![Sliding window method for moving variance with window length 4 and input samples [-1, -2, 3, 2, 5, 2]. At each time step n=0 through n=5, the window fills with zeros until length 4 is reached, then moves along the data. Moving variance computes var for each window position, producing values 0.250, 0.916, 4.667, 5.667, 8.667, 2.000 at n=5](movvar_slidewin.png)
In the exponential weighting method, the moving variance is computed recursively using these formulas:
To compute the moving variance, the algorithm implements these equations recursively.
— Moving variance of the current data sample with respect to the rest of the data in the channel.
— Moving average at the current sample. For details on computing the moving average, see
dsp.MovingAverage.— Difference between each data sample and the average of the data, squared.
— Difference between each data sample and the average of the data, squared and multiplied with the forgetting factor. All the squared terms are added.
— Weighting factor applied to the sum.
λ — Forgetting factor you can specify through the
ForgettingFactorproperty.
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.
![Exponential weighting method for moving variance with forgetting factor 0.9. Three input frames: [2,3,4,5] at n=0 producing outputs [0.0000, 0.5000, 0.9982, 1.6587], [6,7,8,9] at n=1 producing outputs [2.4780, 3.4514, 4.5734, 5.8375], and [3,4,6,8] at n=2 producing outputs [6.2519, 5.5814, 4.7901, 4.9790]](movvar_expwei.png)
When you specify the window length to be tunable, the moving variance algorithm maintains an internal buffer of size MaxWL−1 to store past input samples, where MaxWL is the maximum window length. At each time step, the moving variance algorithm performs these operations:
Concatenates the buffer with the current input to form an extended data sequence.
Extracts the most recent window length samples from the extended data sequence.
Computes the moving variance over the extracted samples using a sliding window and discards the endpoints.
Stores the most recent MaxWL−1 samples in the buffer for the next time step.
This approach allows you to change the window length at each time step while the algorithm maintains continuity of the output signal.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced in R2016bYou can now change the window length during simulation by selecting the Make window length tunable parameter. Specify the tunable window length from the block dialog box by updating the Window length parameter or through the WL input port by selecting Specify window length from input port. The Maximum window length parameter sets the upper bound for the tunable window length. Select Auto-adjust invalid window length to control whether invalid window length values are automatically adjusted with a warning or cause an error.
You can now specify a forgetting factor of 0 in the Moving Variance block.
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.
Starting in R2022b, you can specify the overlap length between sliding windows using the Overlap length parameter.
Moving Variance block supports input signals with arbitrary frame lengths when the:
Input signal is a fixed-size signal (frame length does not change during simulation) and you select the Allow arbitrary frame length for fixed-size input signals parameter (if enabled).
Input signal is a variable-size 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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
