Compute variance of input or sequence of inputs
Statistics
dspstat3
The Variance block computes the unbiased variance of each row or column of the input, along vectors of a specified dimension of the input, or of the entire input. The Variance block can also track the variance of a sequence of inputs over a period of time. The Running variance parameter selects between basic operation and running operation.
When you do not select the Running variance check box, the block computes the variance of each row or column of the input, along vectors of a specified dimension of the input, or of the entire input at each individual sample time, and outputs the array y. Each element in y is the variance of the corresponding column, row, vector, or entire input. The output y depends on the setting of the Find the variance value over parameter. For example, consider a 3dimensional input signal of size MbyNbyP:
Entire input
— The
output at each sample time is a scalar that contains the variance
of the entire input.
y = var(u(:)) % Equivalent MATLAB code
Each row
— The output
at each sample time consists of an Mby1byP array,
where each element contains the variance of each vector over the second
dimension of the input. For an input that is an MbyN matrix,
the output at each sample time is an Mby1 column
vector.
y = var(u,0,2) % Equivalent MATLAB code
Each column
— The
output at each sample time consists of a 1byNbyP array,
where each element contains the variance of each vector over the first
dimension of the input. For an input that is an MbyN matrix,
the output at each sample time is a 1byN row
vector.
y = var(u,0,1) % Equivalent MATLAB code
In this mode, the block treats lengthM unoriented vector inputs as Mby1 column vectors.
Specified dimension
—
The output at each sample time depends on Dimension.
If Dimension is set to 1, the output is
the same as that when you select Each column
.
If Dimension is set to 2
,
the output is the same as when you select Each row
.
If Dimension is set to 3
,
the output at each sample time is an MbyN matrix
containing the variance of each vector over the third dimension of
the input.
y = var(u,0,Dimension) % Equivalent MATLAB code
For purely real or purely imaginary inputs, the variance of an MbyN matrix is the square of the standard deviation:
$$y={\sigma}^{2}=\frac{{\displaystyle \sum _{i=1}^{M}{\displaystyle \sum _{j=1}^{N}{\left{u}_{ij}\right}^{2}\frac{{\left{\displaystyle \sum _{i=1}^{M}{\displaystyle \sum _{j=1}^{N}{u}_{ij}}}\right}^{2}}{M*N}}}}{M*N1}$$
For complex inputs, the variance is given by the following equation:
$${\sigma}^{2}={\sigma}_{\mathrm{Re}}{}^{2}+{\sigma}_{\mathrm{Im}}{}^{2}$$
When you select the Running variance check box, the block tracks the variance of successive inputs to the block. In this mode, you must also specify a value for the Input processing parameter:
When you select Elements as channels
(sample based)
, the block outputs an MbyN array.
Each element y_{ij} of the
output contains the variance of the element u_{ij} over
all inputs since the last reset.
When you select Columns as channels (frame
based)
, the block outputs an MbyN matrix.
Each element y_{ij} of the
output contains the variance of the jth column
over all inputs since the last reset, up to and including element u_{ij} of
the current input.
When your inputs are of variable size, and you select the Running variance check box, there are two options:
If you set the Input processing parameter
to Elements as channels (sample based)
,
the state is reset.
If you set the Input processing parameter
to Columns as channels (frame based)
, then
there are two cases:
When the input size difference is in the number of channels (i.e., number of columns), the state is reset.
When the input size difference is in the length of channels (i.e., number of rows), there is no reset and the running operation is carried out as usual.
The block resets the running variance whenever a reset event is detected at the optional Rst port. The reset sample time must be a positive integer multiple of the input sample time.
You specify the reset event in the Reset port parameter:
None
disables the Rst port.
Rising edge
— Triggers
a reset operation when the Rst input does one of the following:
Rises from a negative value to a positive value or zero
Rises from zero to a positive value, where the rise is not a continuation of a rise from a negative value to zero (see the following figure)
Falling edge
— Triggers
a reset operation when the Rst input does one of the following:
Falls from a positive value to a negative value or zero
Falls from zero to a negative value, where the fall is not a continuation of a fall from a positive value to zero (see the following figure)
Either edge
— Triggers
a reset operation when the Rst input is a Rising edge
or Falling
edge
(as described earlier)
Nonzero sample
—
Triggers a reset operation at each sample time that the Rst input
is not zero
Note: When running simulations in the Simulink^{®} MultiTasking mode, reset signals have a onesample latency. Therefore, when the block detects a reset event, there is a onesample delay at the reset port rate before the block applies the reset. For more information on latency and the Simulink tasking modes, see Excess Algorithmic Delay (Tasking Latency) and TimeBased Scheduling and Code Generation in the Simulink Coder™ documentation. 
The parameters on the Data Types pane of the block dialog are only used for fixedpoint inputs. For purely real or purely imaginary inputs, the variance of the input is the square of its standard deviation. For complex inputs, the output is the sum of the variance of the real and imaginary parts of the input.
The following diagram shows the data types used within the Variance block for fixedpoint signals.
The results of the magnitudesquared calculations in the figure are in the product output data type. You can set the accumulator, product output, and output data types in the block dialog as discussed in Dialog Box.
In the following ex_variance_ref model,
the Variance block calculates the running variance of a 3by2 matrix
input, u
. The Input processing parameter
is set to Columns as channels (frame based)
,
so the block processes the input as a two channel signal with a frame
size of three. The running variance is reset at t=2
by an impulse to the block's Rst port.
The operation of the block is shown in the following figure.
The Main pane of the Variance block dialog appears as follows.
Enables running operation when selected.
Specify how the block should process the input when computing the running variance. You can set this parameter to one of the following options:
Columns as channels (frame based)
—
When you select this option, the block treats each column of the input
as a separate channel.
Elements as channels (sample based)
—
When you select this option, the block treats each element of the
input as a separate channel.
This parameter appears only when you select the Running variance check box.
Note:
The option 
Specify the reset event that causes the block to reset the running variance. The sample time of the input to the Rst port must be a positive integer multiple of the input sample time. This parameter appears only when you select the Running variance check box. For more information, see Resetting the Running Variance
Specify whether to find the variance along rows, columns, entire input, or the dimension specified in the Dimension parameter. For more information, see Basic Operation.
Specify the dimension (onebased value) of the input signal,
over which the variance is computed. The value of this parameter cannot
exceed the number of dimensions in the input signal. This parameter
is only visible when the Find the variance value over parameter
is set to Specified dimension
.
The Data Types pane of the Variance block dialog appears as follows.
Select the rounding mode for fixedpoint operations.
Select the overflow mode for fixedpoint operations.
Note: See FixedPoint Data Types for more information on how the product output, accumulator, and output data types are used in this block. 
Use this parameter to specify how to designate the inputsquared product word and fraction lengths:
When you select Same as input
,
these characteristics match those of the input to the block.
When you select Binary point scaling
,
you can enter the word length and the fraction length of the inputsquared
product, in bits.
When you select Slope and bias scaling
,
you can enter the word length, in bits, and the slope of the inputsquared
product. This block requires poweroftwo slope and a bias of zero.
Use this parameter to specify how to designate the inputsumsquared product word and fraction lengths:
When you select Same as inputsquared
product
, these characteristics match those of the inputsquared
product.
When you select Binary point scaling
,
you can enter the word length and the fraction length of the inputsumsquared
product, in bits.
When you select Slope and bias scaling
,
you can enter the word length, in bits, and the slope of the inputsumsquared
product. This block requires poweroftwo slope and a bias of zero.
Use this parameter to specify the accumulator word and fraction lengths resulting from a complexcomplex multiplication in the block:
When you select Same as inputsquared
product
, these characteristics match those of the inputsquared
product.
When you select Same as input
,
these characteristics match those of the input to the block.
When you select Binary point scaling
,
you can enter the word length and the fraction length of the accumulator,
in bits.
When you select Slope and bias scaling
,
you can enter the word length, in bits, and the slope of the accumulator.
This block requires poweroftwo slope and a bias of zero.
Choose how you specify the output word length and fraction length:
When you select Same as accumulator
,
these characteristics match those of the accumulator.
When you select Same as inputsquared
product
, these characteristics match those of the inputsquared
product.
When you select Same as input
,
these characteristics match those of the input to the block.
When you select Binary point scaling
,
you can enter the word length and the fraction length of the output,
in bits.
When you select Slope and bias scaling
,
you can enter the word length, in bits, and the slope of the output.
This block requires poweroftwo slope and a bias of zero.
Select this parameter to prevent the fixedpoint tools from overriding the data types you specify on the block mask.
Port  Supported Data Types 

Input 

Reset 

Mean  DSP System Toolbox 
RMS  DSP System Toolbox 
Standard Deviation  DSP System Toolbox 
var  MATLAB 