Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

You are a process engineer trying to design a filter for noisy signal. You want to use a…

  1. User

    You are a process engineer trying to design a filter for noisy signal. You want to use a Butterworth filter in an Allen Bradley PLC. You have settled on a first order low pass filter with a cutoff frequency of 0.00245hz 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.

  2. Assistant

    To design a first-order low-pass Butterworth filter for your Allen Bradley PLC with a scan time of 1 Hz and a cutoff frequency of 0.00245 Hz, we'll derive the discrete-time filter equation and provide Structured Text code for implementation.

    Derivation of the Filter Equation:

    1. Continuous-Time Transfer Function:

      The continuous-time transfer function of a first-order low-pass Butterworth filter is: [ H(s) = \frac{\omega_c}{s + \omega_c} ] where (\omega_c = 2\pi f_c) is the cutoff angular frequency.

    2. Calculate (\omega_c):

      Given (f_c = 0.00245) Hz, [ \omega_c = 2\pi f_c = 2\pi \times 0.00245 \approx 0.015394\ \text{rad/s} ]

    3. Discretization Using the Bilinear Transform:

      We'll convert the continuous-time transfer function to a discrete-time transfer function using the bilinear (Tustin) transform: [ s = \frac{2}{T} \cdot \frac{1 - z^{-1}}{1 + z^{-1}} ] where (T = 1) s is the sampling period (since the scan time is 1 Hz).

    4. Discrete-Time Transfer Function:

      Substitute (s) into (H(s)): [ H(z) = \frac{\omega_c}{\frac{2}{T} \cdot \frac{1 - z^{-1}}{1 + z^{-1}} + \omega_c} ] Simplify the transfer function to get: [ H(z) = \frac{b_0 + b_1 z^{-1}}{a_0 + a_1 z^{-1}} ] where: [ \begin{align*} b_0 &= \omega_c \ b_1 &= \omega_c \ a_0 &= \frac{2}{T} + \omega_c \ a_1 &= -\frac{2}{T} + \omega_c \end{align*} ]

    5. Calculate Coefficients:

      Substitute the known values: [ \begin{align*} a_0 &= 2 + 0.015394 = 2.015394 \ a_1 &= -2 + 0.015394 = -1.984606 \ b_0 &= b_1 = 0.015394 \end{align*} ]

    6. Difference Equation:

      The corresponding difference equation is: [ y[n] = \frac{b_0 x[n] + b_1 x[n-1] - a_1 y[n-1]}{a_0} ] Plug in the coefficients: [ y[n] = \frac{0.015394 (x[n] + x[n-1]) + 1.984606 y[n-1]}{2.015394} ]

    7. Simplify Coefficients:

      Compute the constants: [ \begin{align*} C_1 &= \frac{0.015394}{2.015394} \approx 0.007639 \ C_2 &= \frac{1.984606}{2.015394} \approx 0.984722 \end{align*} ]

      The simplified difference equation becomes: [ y[n] = C_1 (x[n] + x[n-1]) + C_2 y[n-1] ]

    8. Simplified Implementation Using Exponential Averaging:

      Alternatively, for a first-order low-pass filter, the exponential averaging formula is: [ y[n] = \alpha x[n] + (1 - \alpha) y[n-1] ] where: [ \alpha = 1 - e^{-2\pi f_c T} = 1 - e^{-0.015394} \approx 0.015278 ]

    Structured Text Code Implementation:

    Using the simplified exponential averaging method for implementation efficiency, the Structured Text code is:

    (* Filter Constants *)
    CONSTANT
        alpha : REAL := 0.015278;        (* Smoothing factor α *)
    END_CONSTANT
    
    (* Persistent Variables *)
    VAR
        x      : REAL;                   (* Current input signal *)
        y      : REAL;                   (* Current output signal *)
        y_prev : REAL := 0.0;            (* Previous output signal, initialized to zero *)
    END_VAR
    
    (* Filter Calculation *)
    y := alpha * x + (1.0 - alpha) * y_prev;
    
    (* Update Previous Output *)
    y_prev := y;
    

    Notes:

    • Variables:

      • x: Current input value (the signal to be filtered).
      • y: Current output value (the filtered signal).
      • y_prev: Previous output value, retained between scans for continuity.
    • Constants:

      • alpha: Smoothing factor calculated from the cutoff frequency and sampling period.
    • Implementation Details:

      • Ensure y_prev retains its value between scans. In some PLC systems, you might need to declare it with the RETENTIVE or PERSISTENT keyword.
      • Initialize y_prev appropriately. If you don't have a good initial estimate, initializing it to zero is common.
    • Usage:

      • Place the code in a cyclic task that runs at the scan rate of 1 Hz.
      • Update the input x with your signal value each scan.
      • Read the output y for the filtered signal.

    Explanation:

    This code implements a first-order low-pass filter using exponential averaging, which is suitable for discrete-time systems like PLCs. The smoothing factor alpha determines how much new input affects the output compared to the previous output, effectively filtering out high-frequency noise and allowing low-frequency signals (below the cutoff frequency) to pass.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.