You are a process engineer trying to design a filter for noisy level signal. You want to…
- User
You are a process engineer trying to design a filter for noisy level signal. You want to use a Butterworth filter in and Allen Bradley PLC. You have settled on an second order filter with a cutoff frequency of 0.01hz running on a PLC with a scan time of 1hz. Derive the formula and the code so that the filter can be implemented in Structured Text. To design and implement a second-order Butterworth low-pass filter with a cutoff frequency of 0.01 Hz on an Allen Bradley PLC with a scan time of 1 Hz, we need to derive the discrete-time filter coefficients and then translate them into Structured Text code suitable for the PLC.
- Assistant
To design and implement a second-order Butterworth low-pass filter with a cutoff frequency of 0.01 Hz on an Allen Bradley PLC with a scan time (sampling rate) of 1 Hz, we'll need to derive the digital filter coefficients and write the corresponding Structured Text code.
1. Deriving the Filter Coefficients
Step 1: Define the Analog Butterworth Filter
A second-order analog Butterworth low-pass filter has the transfer function:
[ H(s) = \frac{\omega_c^2}{s^2 + \sqrt{2} \omega_c s + \omega_c^2} ]
where (\omega_c = 2\pi f_c) is the cutoff angular frequency.
Given (f_c = 0.01, \text{Hz}), we have:
[ \omega_c = 2\pi \times 0.01, \text{Hz} = 0.062832, \text{rad/s} ]
Step 2: Pre-warp the Cutoff Frequency
To compensate for frequency warping in the bilinear transform, pre-warp the cutoff frequency:
[ \Omega_c = 2 \tan\left( \frac{\omega_c T}{2} \right) ]
Since the sampling period (T = \frac{1}{f_s} = 1, \text{s}), we get:
[ \Omega_c = 2 \tan\left( \frac{0.062832 \times 1}{2} \right) = 0.062853, \text{rad/s} ]
Step 3: Substitute in the Transfer Function
Update the analog transfer function with the pre-warped frequency:
[ H(s) = \frac{\Omega_c^2}{s^2 + \sqrt{2} \Omega_c s + \Omega_c^2} ]
Step 4: Apply the Bilinear Transform
The bilinear transform maps the analog transfer function to a digital one by substituting:
[ s = \frac{2}{T} \cdot \frac{1 - z^{-1}}{1 + z^{-1}} ]
Simplify (s) since (T = 1, \text{s}):
[ s = 2 \cdot \frac{1 - z^{-1}}{1 + z^{-1}} ]
Substitute (s) into the analog transfer function and simplify to get the digital transfer function (H(z)):
[ H(z) = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2}}{1 - a_1 z^{-1} - a_2 z^{-2}} ]
Step 5: Compute the Coefficients
After substitution and simplification, you'll find the coefficients as:
-
Coefficients of the numerator (scaled by (\Omega_c^2)): [ b_0 = 0.000944691 ] [ b_1 = 0.001889383 ] [ b_2 = 0.000944691 ]
-
Coefficients of the denominator: [ a_1 = 1.911197 ] [ a_2 = -0.914975 ]
2. Writing the Difference Equation
The difference equation for the digital filter is:
[ y[n] = b_0 x[n] + b_1 x[n-1] + b_2 x[n-2] + a_1 y[n-1] + a_2 y[n-2] ]
Where:
- ( x[n] ) is the current input.
- ( x[n-1], x[n-2] ) are the previous inputs.
- ( y[n-1], y[n-2] ) are the previous outputs.
3. Implementing in Structured Text
Here is the Structured Text code for the filter:
// Declare variables VAR RETAIN x0 : REAL := 0.0; // Current input x1 : REAL := 0.0; // Previous input x[n-1] x2 : REAL := 0.0; // Previous input x[n-2] y0 : REAL := 0.0; // Current output y1 : REAL := 0.0; // Previous output y[n-1] y2 : REAL := 0.0; // Previous output y[n-2] END_VAR // Coefficients (constants) CONSTANT b0 : REAL := 0.000944691; b1 : REAL := 0.001889383; b2 : REAL := 0.000944691; a1 : REAL := 1.911197; a2 : REAL := -0.914975; END_CONSTANT // Main filter computation // Shift previous inputs x2 := x1; x1 := x0; // Read the current input (replace 'GetCurrentInputValue()' with actual input variable) x0 := GetCurrentInputValue(); // Compute the current output y0 := b0 * x0 + b1 * x1 + b2 * x2 + a1 * y1 + a2 * y2; // Shift previous outputs y2 := y1; y1 := y0; // Output the filtered value (replace 'SetFilteredOutput(y0)' with actual output operation) SetFilteredOutput(y0);Explanation:
-
Variables:
x0,x1,x2: Hold the current and previous two input samples.y0,y1,y2: Hold the current and previous two output samples.- Use
VAR RETAINto preserve variable values between scans.
-
Coefficients:
b0,b1,b2: Numerator coefficients calculated earlier.a1,a2: Denominator coefficients calculated earlier.
-
Main Computation:
- Shift the input samples to make room for the new sample.
- Read the new input sample into
x0. - Compute the new output
y0using the difference equation. - Shift the output samples.
- Output
y0as the filtered output.
Remember: Replace
GetCurrentInputValue()andSetFilteredOutput(y0)with the actual variable names or functions used to read inputs and write outputs in your PLC program.This implementation ensures that the filter retains its state between scans and processes input data according to the derived Butterworth filter coefficients, providing the desired low-pass filtering effect at the specified cutoff frequency.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.