Embedded commonly used algorithm - second-order IIR low-pass filter

The second-order IIR low-pass filter algorithm is a circuit analog filter that eliminates noise by attenuating the signal of the high-frequency part. This filter uses a second-order difference equation to describe the change of the signal, so it is called a second-order IIR low-pass filter.

Specifically, the second-order IIR low-pass filter eliminates noise by attenuating the high-frequency portion of the signal. The signal in the high frequency part refers to the signal whose frequency is higher than the cutoff frequency, and the cutoff frequency refers to the frequency at which the attenuation of the high frequency part signal by the filter starts.

The difference equation for a second-order IIR low-pass filter is

y[n] = a0 * x[n] + a1 * x[n-1] + a2 * x[n-2] - b1 * y[n-1] - b2 * y[n-2]

Where x[n] is the input signal, y[n] is the output signal, and a0, a1, a2, b1, b2 are coefficients.

To determine these coefficients, the characteristic equation of the filter needs to be used. Here we use the characteristic equation of the Butterworth low-pass filter to determine the coefficients. The characteristic equation of the Butterworth low-pass filter is

(s^2 + sqrt(2) * s * Wn + Wn^2) / (s^2 + s * Wn / Q + Wn^2)

Where s is the variable of Laplace transform, Wn is the cut-off frequency, and Q is the frequency bandwidth.

The following is a code example for a second-order IIR low-pass filter written in C:

#include <stdio.h>
#define PI 3.14159265

void lowpass(float input, float* output_prev, float* prev_prev_input, float* prev_prev_output, float a0, float a1, float a2, float b1, float b2) {
    
    
    float output = a0 * input + a1 * (*prev_prev_input) + a2 * (*prev_prev_output) - b1 * (*output_prev) - b2 * (*prev_prev_output);
    *prev_prev_input = *output_prev;
    *prev_prev_output = *output_prev;
    *output_prev = output;
}

int main() {
    
    
    float Wn = 1000; // 1000 Hz cutoff frequency
    float Q = 1;
    float a0, a1, a2, b1, b2;
    a0 = (Wn*Wn) / (1 + (sqrt(2)*Wn/Q) + (Wn*Wn));
    a1 = 2 * a0;
    a2 = a0;
    b1 = 2 * a0 * (1 - (Wn*Wn)) / (1 + (sqrt(2)*Wn/Q) + (Wn*Wn));
    b2 = (1 - (sqrt(2)*Wn/Q) + (Wn*Wn)) / (1 + (sqrt(2)*Wn/Q) + (Wn*Wn));
    float input = 0.5; // input signal
    float output_prev = 0; // previous output
    float prev_prev_input = 0;
    float prev_prev_output = 0;
    lowpass(input, &output_prev, &prev_prev_input, &prev_prev_output, a0, a1, a2, b1, b2);
    printf("%f", output_prev);
    return 0;
}

This code defines a lowpass() function that accepts the input signal, the last output, the last input, the last output and a0, a1, a2, b1, b2 coefficients as parameters. In the function, the new output value is calculated according to the difference equation of the second-order IIR low-pass filter.

In the main() function, we set the cutoff frequency to 1000 Hz and the bandwidth to 1. The a0, a1, a2, b1, b2 coefficients were then determined using the characteristic equation of the Butterworth low-pass filter. We set the input signal to 0.5, the last output and the last input and output are both 0. Then call the lowpass() function, and print out the new output.

The advantage of using a second-order IIR low-pass filtering algorithm is that it has a higher order and therefore provides a higher stop-band to pass-band ratio. However, since the second-order difference equation has two lookaheads, more storage space is required to store the input and output of the last and last time. And this filter will affect the time-domain performance of the signal, so it needs to be weighed according to the specific application scenario.

Overall, the second-order IIR low-pass filtering algorithm is an effective filtering method that can be used when a high stop-band and pass-band ratio is required. It is widely used in signal processing, audio processing and other fields.

Guess you like

Origin blog.csdn.net/sorcererr/article/details/128701063