Moving Standard Deviation (MSTD)
The Moving Standard Deviation (MSTD) instruction calculates a moving standard deviation and
average for the In signal.
Available Languages
Ladder Diagram
This instruction is not available in ladder diagram logic.
Function Block
This instruction is not available in function block.
Structured Text
MSTD(MSTD_tag, StorageArray);
Operands
Structured Text
Operand | Type | Format | Description |
|---|---|---|---|
block tag | MOVING_STD_DEV | structure | MSTD structure |
StorageArray | REAL | array | Holds the In samples. This array must be at least as large as NumberOfSamples. |
MOVING_STD_DEV Structure
Input Parameter | Data Type | Description |
|---|---|---|
EnableIn | BOOL | Enable input. If cleared, the instruction does not execute and outputs are not
updated. Default is set. |
In | REAL | The analog signal input to the instruction. Valid = any float Default = 0.0 |
InFault | BOOL | Bad health indicator for the input. If In is read from an analog input, then
InFault is normally controlled by fault status on the analog input. When set,
InFault indicates the input signal has an error, the instruction sets the
appropriate bit in Status, and the instruction holds Out and Average at their
current values. When InFault transitions from set to cleared, the instruction
initializes the averaging algorithm and continues executing. Default is cleared. |
Initialize | BOOL | Initialize input to the instruction. When set, the instruction sets Out = 0.0 and
Average = In, except when InFault is set, in which case, the instruction holds both
Out and Average at their current values. When Initialize transitions from set to
cleared, the instruction initializes the standard deviation algorithm and continues
executing. Default is cleared. |
SampleEnable | BOOL | Enable for taking a sample of In. When set, the instruction enters the value of In
into the storage array and calculates a new Out and Average value. When SampleEnable
is cleared and Initialize is cleared, the instruction holds Out and Average at their
current values. Default is cleared. |
NumberOfSamples | DINT | The number of samples to be used in the calculation. If this value is invalid, the
instruction sets the appropriate bit in Status and the instruction holds Out and
Average at their current values. When NumberOfSamples becomes valid again, the
instruction initializes the standard deviation algorithm and continues
executing. Valid = 1 to size of the storage array Default = 1 |
Output Parameter | Data Type | Description |
|---|---|---|
EnableOut | BOOL | Indicates if instruction is enabled. Cleared to false if Out overflows. |
Out | REAL | The calculated output of the algorithm. |
Average | REAL | The calculated average of the algorithm. |
Status | DINT | Status of the function block. |
InstructFault (Status.0) | BOOL | The instruction detected one of the following execution errors. This is not a
minor or major controller error. Check the remaining status bits to determine what
occurred. |
InFaulted (Status.1) | BOOL | In health is bad. InFault is set. |
NumberOfSampInv (Status.2) | BOOL | NumberOfSamples invalid or not compatible with array size. |
See Structured Text syntax for more information on the syntax of
expressions within structured text.
Description
The MSTD instruction supports any input queue length. Each scan, if SampleEnable is set, the
instruction enters the value of In into a storage array. When the storage array is full, each
new value of In causes the oldest entry to be deleted.
The MSTD instruction uses these equations for the outputs:
Condition | Action |
|---|---|
Average | ![]() |
Out | ![]() |
The instruction will not place an invalid In value (NAN or
INF) into the storage array. When In is invalid, the instruction sets Out = In, sets
Average = In, and logs an overflow minor fault, if this reporting is enabled. When In becomes
valid, the instruction initializes the standard deviation algorithm and continues
executing.
INF) into the storage array. When In is invalid, the instruction sets Out = In, sets
Average = In, and logs an overflow minor fault, if this reporting is enabled. When In becomes
valid, the instruction initializes the standard deviation algorithm and continues
executing.You can make runtime changes to the NumberOfSamples parameter. If you increase the number,
the instruction incrementally processes new data from the current sample size to the new
sample size. If you decrease the number, the instruction re-calculates the standard deviation
from the beginning of the sample array to the new NumberOfSamples value.
Initializing the Standard Deviation Algorithm
Certain conditions, such as instruction first scan and instruction first run, require the
instruction to initialize the standard deviation algorithm. When this occurs, the instruction
considers the StorageArray empty and incrementally processes samples from 1 to the
NumberOfSamples value. For example:

Affects Math Status Flags
No
Major/Minor Faults
None specific to this instruction. See Common Attributes for operand-related faults.
Execution
Structured Text
In Structured Text, EnableIn is always True during a normal scan. If the instruction is in
the control path activated by the logic, it executes.
Condition | Action |
|---|---|
Prescan | EnableIn and EnableOut bits are cleared to false. |
Normal Execution | EnableIn and EnableOut bits are set to true. The instruction executes. |
Postscan | EnableIn and EnableOut bits are cleared to false. |
Example
Each scan that SampleEnable is set, the instruction places the value of In into array
storage, calculates the standard deviation of the values in array storage, and places the
result in Out. Out becomes an input parameter for function_block_C.
Structured Text
MSTD_01.In := input_value; MSTD_01.SampleEnable := enable_sample; MSTD(MSTD_01,storage); deviation := MSTD_01.Out;
Provide Feedback

