s7-SCL-scripts/Exponential-Moving-Average.txt

27 lines
587 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#Faster response than SMA, no large buffer needed. The alpha parameter (0.01.0) controls smoothing — lower = more smoothing.
FUNCTION_BLOCK FB_EMA
VAR_INPUT
xEnable : BOOL;
rInput : REAL;
rAlpha : REAL := 0.1; // Smoothing factor: 0 = max smooth, 1 = no filter
END_VAR
VAR_OUTPUT
rOutput : REAL;
END_VAR
VAR
xFirst : BOOL := TRUE;
END_VAR
IF NOT xEnable THEN
xFirst := TRUE;
rOutput := rInput;
RETURN;
END_IF;
IF xFirst THEN
rOutput := rInput;
xFirst := FALSE;
ELSE
rOutput := rAlpha * rInput + (1.0 - rAlpha) * rOutput;
END_IF;