MovingAverageFilter
- Namespace
- ZCore
FUNCTION_BLOCK MovingAverageFilter
This functionblock calculates the average over a series of values, averaging inside a defined window.
It is optimimized for low memory consumation and fast performance.
The window size and sampling rate of the function block is set when constructing the object. These two parameters
also control the number of values that are internally stored in the function block in a buffer. The buffer is
statically allocated in the PLC. This means that there is a maximum size to it, which can be controlled with the
library parameter MovingAverageMaxBufferSize.
The relation between buffer size to window size and sampling rate, respectively, is given by
bufferSize := TRUNC(window/samplingRate)
When construction the object this relation should be kept in mind and if MovingAverageMaxBufferSize
is too small, it has to be set accordingly. The parameter defaults to 256, which is sufficient for a lot of applications.
Constructor
FB_init
METHOD FB_init (
[input] bInitRetains : BOOL,
[input] bInCopyCode : BOOL,
[input] cycletime : LREAL,
[input] samples : DINT) : BOOL
The constructor of MovingAverageFilter is used to initialize the amount of values which this function block should Average over. For details see the description of MovingAverageFilter.
Inputs
bInitRetainsBOOLif TRUE, the retain variables are initialized (warm start / cold start)
bInCopyCodeBOOLif TRUE, the instance afterwards gets moved into the copy code (online change)
cycletimeLREALcycletime in seconds, this should be set to the cycle time of the PLC and the cycle that Update is called with, respectively.
samplesDINTnumber of samples, the actual sample count is capped by ParameterList.MovingAverageMaxBufferSize.
Returns
- BOOL
Methods
Output
METHOD Output () : LREAL
Returns the current average of all values that are currently within the window of this filter. Values are added to the function block by calling Append.
Returns
- LREAL
Reset
METHOD Reset ()
Resets the average to zero and removes all values that have previously been Appended to the filter and are still in the filter's window.
Update
METHOD Update (
[input] value : LREAL)
This method is used to add a new value for Average. The function block averages over a specific amount of time, which is initialized when constructing the object.