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.
Practice
Solution
Step 1: Understand IIR filter structure
IIR filters use feedback from output to input, which can cause instability.Step 2: Compare with FIR filters
FIR filters use only past inputs, making them always stable and with linear phase.Final Answer:
It uses feedback and can be unstable -> Option BQuick Check:
IIR feedback = unstable possibility [OK]
- Confusing stability of IIR and FIR
- Thinking FIR uses feedback
- Assuming IIR always has linear phase
Solution
Step 1: Recall FIR filter definition
FIR filters use only current and past input values, no feedback.Step 2: Check each comment
// FIR filter uses only current and past inputs correctly states FIR uses only inputs; others incorrectly mention feedback or instability.Final Answer:
// FIR filter uses only current and past inputs -> Option DQuick Check:
FIR = inputs only, no feedback [OK]
- Writing that FIR uses feedback
- Confusing IIR and FIR definitions
- Saying FIR can be unstable
Solution
Step 1: Understand FIR filter output length
FIR filter output length = input length + filter length - 1.Step 2: Calculate output length
100 + 5 - 1 = 104.Final Answer:
104 -> Option AQuick Check:
Output length = input + filter - 1 = 104 [OK]
- Using input length as output length
- Subtracting filter length instead of adding
- Confusing IIR and FIR output lengths
def iir_filter(x, a, b):
y = [0]*len(x)
for n in range(len(x)):
y[n] = b[0]*x[n] + b[1]*x[n-1] - a[1]*y[n-1]
return ySolution
Step 1: Analyze loop indexing
At n=0, accessing x[n-1] and y[n-1] means x[-1] and y[-1], which may cause unintended behavior or errors.Step 2: Identify error type
This causes incorrect values because negative indices access last elements, not previous samples.Final Answer:
Incorrect results when n=0 accessing x[n-1] and y[n-1] -> Option CQuick Check:
Negative index at start causes incorrect results [OK]
- Ignoring negative index issue
- Thinking coefficients are wrong
- Assuming output list is uninitialized
Solution
Step 1: Identify requirements
The filter must be stable and have linear phase for audio quality.Step 2: Match filter properties
FIR filters are always stable and have linear phase; IIR filters may be unstable and usually do not have linear phase.Final Answer:
FIR filter, because it is always stable and has linear phase -> Option AQuick Check:
Stability + linear phase = FIR filter [OK]
- Choosing IIR for linear phase
- Ignoring stability requirement
- Thinking FIR uses feedback
Ready to prove it?
Pass the test and this pattern moves unknown
