Bird
Raised Fist0
Signal Processingdata~7 mins

IIR vs FIR filter comparison in Signal Processing

Choose your learning style10 modes available
Introduction

Filters help clean or change signals by removing unwanted parts. IIR and FIR are two common filter types with different ways to do this.

When you want a simple filter that uses less memory and runs faster.
When you need a filter with exact control over the signal shape.
When you want a filter that is always stable and easy to design.
When you want to save computing power in real-time signal processing.
When you want to avoid delay in the filtered signal.
Syntax
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.

Examples
This IIR filter uses one past output and two input values.
Signal Processing
IIR example: y[n] = 0.5 x[n] + 0.5 x[n-1] - 0.3 y[n-1]
This FIR filter uses three input values and no feedback.
Signal Processing
FIR example: y[n] = 0.3 x[n] + 0.3 x[n-1] + 0.4 x[n-2]
Sample Program

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.

Signal Processing
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])}')
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. Which of the following is a key characteristic of an IIR filter compared to an FIR filter?
easy
A. It uses only past inputs and is always stable
B. It uses feedback and can be unstable
C. It always has linear phase response
D. It requires more coefficients than FIR filters

Solution

  1. Step 1: Understand IIR filter structure

    IIR filters use feedback from output to input, which can cause instability.
  2. Step 2: Compare with FIR filters

    FIR filters use only past inputs, making them always stable and with linear phase.
  3. Final Answer:

    It uses feedback and can be unstable -> Option B
  4. Quick Check:

    IIR feedback = unstable possibility [OK]
Hint: IIR uses feedback; FIR uses only inputs [OK]
Common Mistakes:
  • Confusing stability of IIR and FIR
  • Thinking FIR uses feedback
  • Assuming IIR always has linear phase
2. Which of the following is the correct way to describe an FIR filter in code comments?
easy
A. // FIR filter uses feedback from output
B. // FIR filter requires infinite impulse response
C. // FIR filter can be unstable due to feedback
D. // FIR filter uses only current and past inputs

Solution

  1. Step 1: Recall FIR filter definition

    FIR filters use only current and past input values, no feedback.
  2. 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.
  3. Final Answer:

    // FIR filter uses only current and past inputs -> Option D
  4. Quick Check:

    FIR = inputs only, no feedback [OK]
Hint: FIR = inputs only, no feedback [OK]
Common Mistakes:
  • Writing that FIR uses feedback
  • Confusing IIR and FIR definitions
  • Saying FIR can be unstable
3. What will be the output length of an FIR filter applied to a signal of length 100 with filter length 5?
medium
A. 104
B. 96
C. 100
D. 105

Solution

  1. Step 1: Understand FIR filter output length

    FIR filter output length = input length + filter length - 1.
  2. Step 2: Calculate output length

    100 + 5 - 1 = 104.
  3. Final Answer:

    104 -> Option A
  4. Quick Check:

    Output length = input + filter - 1 = 104 [OK]
Hint: Output length = input + filter length - 1 [OK]
Common Mistakes:
  • Using input length as output length
  • Subtracting filter length instead of adding
  • Confusing IIR and FIR output lengths
4. Given this Python code snippet for an IIR filter, identify the error:
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 y
medium
A. Output list y is not initialized
B. Incorrect use of coefficients a and b
C. Incorrect results when n=0 accessing x[n-1] and y[n-1]
D. Function returns input x instead of output y

Solution

  1. 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.
  2. Step 2: Identify error type

    This causes incorrect values because negative indices access last elements, not previous samples.
  3. Final Answer:

    Incorrect results when n=0 accessing x[n-1] and y[n-1] -> Option C
  4. Quick Check:

    Negative index at start causes incorrect results [OK]
Hint: Check indexing at loop start for negative indices [OK]
Common Mistakes:
  • Ignoring negative index issue
  • Thinking coefficients are wrong
  • Assuming output list is uninitialized
5. You want to design a filter for a real-time audio system that must be stable and have linear phase. Which filter type should you choose and why?
hard
A. FIR filter, because it is always stable and has linear phase
B. FIR filter, because it uses feedback for sharp filtering
C. IIR filter, because it guarantees linear phase
D. IIR filter, because it uses fewer coefficients and is efficient

Solution

  1. Step 1: Identify requirements

    The filter must be stable and have linear phase for audio quality.
  2. 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.
  3. Final Answer:

    FIR filter, because it is always stable and has linear phase -> Option A
  4. Quick Check:

    Stability + linear phase = FIR filter [OK]
Hint: Choose FIR for stability and linear phase [OK]
Common Mistakes:
  • Choosing IIR for linear phase
  • Ignoring stability requirement
  • Thinking FIR uses feedback
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard