0
0
Signal Processingdata~5 mins

IIR filter structure (feedback loops) in Signal Processing

Choose your learning style9 modes available
Introduction

An IIR filter uses past outputs to shape the current output. This feedback loop helps create filters that can be very efficient and effective.

When you want to remove noise from a sound recording in real time.
When designing a simple equalizer for audio devices.
When smoothing sensor data that changes quickly but needs to stay responsive.
When you need a filter that uses less memory and computation than other types.
When you want to create a filter with a sharp cutoff using fewer coefficients.
Syntax
Signal Processing
y[n] = (b0 * x[n]) + (b1 * x[n-1]) + ... + (bM * x[n-M]) - (a1 * y[n-1]) - ... - (aN * y[n-N])

x[n] is the current input sample.

y[n] is the current output sample.

The b coefficients multiply input samples, and the a coefficients multiply past output samples (feedback).

Examples
This example uses one past input and one past output to calculate the current output.
Signal Processing
y[n] = 0.5 * x[n] + 0.5 * x[n-1] - 0.3 * y[n-1]
This uses two past inputs and two past outputs for a more complex filter.
Signal Processing
y[n] = 0.2 * x[n] + 0.1 * x[n-1] + 0.1 * x[n-2] - 0.4 * y[n-1] - 0.1 * y[n-2]
Sample Program

This code creates a step input signal and applies a simple IIR filter using the difference equation with feedback loops. It then plots both the input and filtered output to show how the filter smooths the step.

Signal Processing
import numpy as np
import matplotlib.pyplot as plt

# Define input signal: a step signal
x = np.zeros(50)
x[10:] = 1  # Step starts at sample 10

# Coefficients for a simple IIR filter
b = [0.5, 0.5]  # feedforward coefficients
a = [1.0, -0.3]  # feedback coefficients (a[0] is always 1)

# Initialize output array
y = np.zeros_like(x)

# Apply IIR filter using difference equation
for n in range(len(x)):
    # Feedforward part
    ff = 0
    for i in range(len(b)):
        if n - i >= 0:
            ff += b[i] * x[n - i]
    # Feedback part
    fb = 0
    for j in range(1, len(a)):
        if n - j >= 0:
            fb += a[j] * y[n - j]
    y[n] = ff - fb

# Plot input and output
plt.plot(x, label='Input x[n]')
plt.plot(y, label='Output y[n]')
plt.title('Simple IIR Filter with Feedback Loop')
plt.xlabel('Sample n')
plt.ylabel('Amplitude')
plt.legend()
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

The feedback loop means the output depends on past outputs, so the filter can 'remember' past inputs.

Make sure the first feedback coefficient a[0] is 1 for the standard form.

Improper coefficients can make the filter unstable, causing outputs to grow without bound.

Summary

An IIR filter uses both past inputs and past outputs to calculate the current output.

Feedback loops allow the filter to be efficient and have sharp responses.

Understanding the difference equation helps you design and implement these filters.