Filters help clean or change signals by removing unwanted parts. IIR and FIR are two common filter types with different ways to do this.
IIR vs FIR filter comparison in Signal Processing
IIR filter: y[n] = \sum_{k=0}^{M} b_k x[n-k] - \sum_{k=1}^{N} a_k y[n-k] FIR filter: y[n] = \sum_{k=0}^{M} b_k x[n-k]
IIR filters use past outputs (feedback), so they can be more efficient but may be unstable.
FIR filters use only current and past inputs (no feedback), so they are always stable and have linear phase.
IIR example: y[n] = 0.5 x[n] + 0.5 x[n-1] - 0.3 y[n-1]
FIR example: y[n] = 0.3 x[n] + 0.3 x[n-1] + 0.4 x[n-2]
This program creates a signal with low and high frequencies. It applies a simple FIR low-pass filter and an IIR low-pass filter. Then it shows the filtered signals and prints part of their frequency responses.
import numpy as np import matplotlib.pyplot as plt from scipy.signal import lfilter, freqz # Create a sample signal: sum of two sine waves fs = 500 # Sampling frequency T = 1.0 # seconds n = int(T * fs) t = np.linspace(0, T, n, endpoint=False) signal = np.sin(2 * np.pi * 5 * t) + 0.5 * np.sin(2 * np.pi * 50 * t) # FIR filter coefficients (low-pass) fir_coeff = np.ones(15) / 15 fir_filtered = lfilter(fir_coeff, 1.0, signal) # IIR filter coefficients (Butterworth low-pass) iir_b = [0.0675, 0.1349, 0.0675] iir_a = [1.0, -1.1430, 0.4128] iir_filtered = lfilter(iir_b, iir_a, signal) # Frequency response w_fir, h_fir = freqz(fir_coeff, worN=8000) w_iir, h_iir = freqz(iir_b, iir_a, worN=8000) # Plot results plt.figure(figsize=(12, 8)) plt.subplot(3,1,1) plt.plot(t, signal) plt.title('Original Signal') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.subplot(3,1,2) plt.plot(t, fir_filtered, 'g') plt.title('FIR Filtered Signal') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.subplot(3,1,3) plt.plot(t, iir_filtered, 'r') plt.title('IIR Filtered Signal') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.tight_layout() plt.show() # Print frequency response summary print(f'FIR filter - first 5 frequency response magnitudes: {abs(h_fir[:5])}') print(f'IIR filter - first 5 frequency response magnitudes: {abs(h_iir[:5])}')
IIR filters can be unstable if not designed carefully.
FIR filters usually need more coefficients (longer) to get sharp cutoffs.
FIR filters have linear phase, meaning no signal distortion in time.
IIR filters use feedback and are efficient but can be unstable.
FIR filters use only input values, are always stable, and have linear phase.
Choose FIR for exact phase and stability, IIR for efficiency and fewer coefficients.