Design and Implementation of a Second-Order Butterworth Low-Pass Filter in Structured Text
Introduction
To filter a noisy level signal using a second-order Butterworth low-pass filter with a cutoff frequency of 0.01 Hz on an Allen Bradley PLC running at a scan time (sampling rate) of 1 Hz, we need to derive the discrete-time filter coefficients and implement the filter using Structured Text (ST). This guide will walk you through the mathematical derivation of the filter coefficients and provide the corresponding ST code for implementation.
1. Specifications
- Filter Type: Second-order Butterworth low-pass filter
- Cutoff Frequency (( f_c )): 0.01 Hz
- Sampling Rate (( f_s )): 1 Hz (Scan time of 1 Hz implies ( T_s = 1 ) second)
- Sampling Period (( T_s )): ( \frac{1}{f_s} = 1 ) second
2. Continuous-Time Transfer Function
The standard transfer function of a second-order Butterworth low-pass filter is:
[
H(s) = \frac{\omega_c^2}{s^2 + 2\zeta\omega_c s + \omega_c^2}
]
For a Butterworth filter, the damping ratio ( \zeta ) is ( \frac{\sqrt{2}}{2} \approx 0.7071 ). The cutoff angular frequency ( \omega_c ) is:
[
\omega_c = 2\pi f_c
]
Calculating ( \omega_c ):
[
\omega_c = 2\pi (0.01) = 0.06283 \text{ rad/s}
]
Substituting ( \zeta ) and ( \omega_c ) into ( H(s) ):
[
H(s) = \frac{(0.06283)^2}{s^2 + 0.08886 s + (0.06283)^2}
]
3. Discrete-Time Filter Design Using the Bilinear Transform
To convert the continuous-time filter to a discrete-time filter suitable for digital implementation, we'll use the bilinear transform, which substitutes ( s ) with:
[
s = \frac{2}{T_s} \cdot \frac{1 - z^{-1}}{1 + z^{-1}}
]
Given ( T_s = 1 ) second, the substitution becomes:
[
s = 2 \cdot \frac{1 - z^{-1}}{1 + z^{-1}}
]
Substituting ( s ) into ( H(s) ):
First, expand the denominator:
[
D(s) = s^2 + 0.08886 s + (0.06283)^2
]
Substituting ( s ):
[
D(z) = \left(2 \cdot \frac{1 - z^{-1}}{1 + z^{-1}}\right)^2 + 0.08886 \left(2 \cdot \frac{1 - z^{-1}}{1 + z^{-1}}\right) + (0.06283)^2
]
Simplify the expression by finding a common denominator ((1 + z^{-1})^2):
[
D(z) = \frac{4(1 - z^{-1})^2 + 0.17772(1 - z^{-1})(1 + z^{-1}) + 0.003947 \cdot (1 + z^{-1})^2}{(1 + z^{-1})^2}
]
The numerator in ( H(s) ) remains ( (0.06283)^2 ), which in discrete form becomes ( N(z) = 0.003947 \cdot (1 + z^{-1})^2 ), to match the denominator's order.
4. Simplifying and Finding Discrete-Time Coefficients
Expand and Simplify Numerator and Denominator:
[
N(z) = 0.003947 \cdot (1 + 2z^{-1} + z^{-2})
]
Perform polynomial multiplication and collect like terms to express ( D(z) ) as:
[
D(z) = a_0 + a_1 z^{-1} + a_2 z^{-2}
]
Compute Coefficients:
After simplifying, you'll find:
- ( a_0 = 4.1812 )
- ( a_1 = -7.9921 )
- ( a_2 = 3.8267 )
- ( b_0 = 0.003947 )
- ( b_1 = 0.007895 )
- ( b_2 = 0.003947 )
5. Normalizing the Coefficients
Divide all coefficients by ( a_0 ) to normalize ( a_0 ) to 1:
[
\begin{align*}
\tilde{a}_0 &= 1 \
\tilde{a}_1 &= \frac{a_1}{a_0} = \frac{-7.9921}{4.1812} = -1.9112 \
\tilde{a}_2 &= \frac{a_2}{a_0} = \frac{3.8267}{4.1812} = 0.91498 \
\tilde{b}_0 &= \frac{b_0}{a_0} = \frac{0.003947}{4.1812} = 0.00094399 \
\tilde{b}_1 &= \frac{b_1}{a_0} = \frac{0.007895}{4.1812} = 0.001888 \
\tilde{b}_2 &= \frac{b_2}{a_0} = 0.00094399
\end{align*}
]
6. Discrete-Time Difference Equation
The standard form of the difference equation for an IIR filter is:
[
y[n] = -\tilde{a}_1 y[n-1] - \tilde{a}_2 y[n-2] + \tilde{b}_0 x[n] + \tilde{b}_1 x[n-1] + \tilde{b}_2 x[n-2]
]
Substitute the normalized coefficients:
[
y[n] = 1.9112 y[n-1] - 0.91498 y[n-2] + 0.00094399 x[n] + 0.001888 x[n-1] + 0.00094399 x[n-2]
]
7. Structured Text Implementation
Variables Declaration:
VAR
x0 : REAL; // Current input sample x[n]
x1 : REAL; // Previous input sample x[n-1]
x2 : REAL; // Input sample x[n-2]
y0 : REAL; // Current output sample y[n]
y1 : REAL; // Previous output sample y[n-1]
y2 : REAL; // Output sample y[n-2]
InputSignal : REAL; // Input signal variable
FilteredSignal : REAL; // Output signal variable
FirstScan : BOOL; // Boolean flag for first scan initialization
END_VAR
Constants Declaration:
VAR CONSTANT
a1 : REAL := 1.9112;
a2 : REAL := -0.91498;
b0 : REAL := 0.00094399;
b1 : REAL := 0.001888;
b2 : REAL := 0.00094399;
END_VAR
Filter Implementation Code:
// Initialization block (executed only once during the first scan)
IF FirstScan THEN
x0 := 0.0;
x1 := 0.0;
x2 := 0.0;
y0 := 0.0;
y1 := 0.0;
y2 := 0.0;
FirstScan := FALSE; // Reset the first scan flag after initialization
END_IF
// Read the current input sample
x0 := InputSignal; // Assign the current input signal to x0
// Compute the current output sample using the difference equation
y0 := a1 * y1 + a2 * y2 + b0 * x0 + b1 * x1 + b2 * x2;
// Update the output variable
FilteredSignal := y0;
// Shift the input and output samples for the next iteration
x2 := x1;
x1 := x0;
y2 := y1;
y1 := y0;
8. Notes on Implementation
- First Scan Initialization: It's essential to initialize the previous input and output samples to zero on the first scan to prevent undefined behavior.
- Sampling Rate Consistency: Ensure that the PLC's scan rate matches the designed sampling rate (( f_s = 1 ) Hz) for accurate filter performance.
- Variable Types: Use appropriate data types (e.g.,
REAL or FLOAT) to handle fractional coefficients.
- Signal Variables: Replace
InputSignal and FilteredSignal with the actual variable names used in your PLC program.
- Handling Filter Startup: The filter requires at least two past input and output samples. During startup, the initial conditions are zero, which may cause the filter to take a few iterations to settle.
Conclusion
By deriving the discrete-time filter coefficients using the bilinear transform and implementing the corresponding difference equation in Structured Text, we've designed a second-order Butterworth low-pass filter suitable for an Allen Bradley PLC with a scan time of 1 Hz. This filter will effectively attenuate frequencies higher than the 0.01 Hz cutoff frequency, thus reducing noise in the level signal while preserving the desired signal components.
References
- Oppenheim, A.V., & Schafer, R.W. (2010). Discrete-Time Signal Processing. Pearson.
- IEC 61131-3 Standard for Programmable Controllers - Programming Languages.