How to Design FIR Filter: Simple Steps and Example
To design a
FIR filter, first decide the filter type and specifications like cutoff frequency and order. Then use a window method or optimization technique to calculate filter coefficients, which define the filter's behavior.Syntax
The basic syntax for designing a FIR filter using the window method in Python with scipy.signal is:
firwin(numtaps, cutoff, window='hamming', pass_zero=True, fs=sample_rate)
Where:
numtaps: Number of filter coefficients (filter order + 1)cutoff: Cutoff frequency or frequencies in Hzwindow: Type of window to shape the filter (e.g., 'hamming')pass_zero: True for lowpass, False for highpassfs: Sampling frequency of the signal
python
from scipy.signal import firwin # Design a lowpass FIR filter numtaps = 51 # filter length cutoff = 1000 # cutoff frequency in Hz fs = 8000 # sampling frequency in Hz coefficients = firwin(numtaps, cutoff, window='hamming', pass_zero=True, fs=fs)
Example
This example shows how to design a lowpass FIR filter with a cutoff frequency of 1000 Hz, sampling rate 8000 Hz, and 51 taps. It also plots the frequency response to visualize the filter.
python
import numpy as np import matplotlib.pyplot as plt from scipy.signal import firwin, freqz # Filter parameters numtaps = 51 cutoff = 1000 fs = 8000 # Design filter coefficients = firwin(numtaps, cutoff, window='hamming', pass_zero=True, fs=fs) # Frequency response w, h = freqz(coefficients, worN=8000, fs=fs) # Plot plt.plot(w, 20 * np.log10(abs(h)), 'b') plt.title('Frequency Response of FIR Filter') plt.xlabel('Frequency (Hz)') plt.ylabel('Gain (dB)') plt.grid(True) plt.show()
Output
A plot window showing the frequency response curve with a clear cutoff near 1000 Hz and attenuation above cutoff.
Common Pitfalls
- Choosing too few taps results in poor filter sharpness and more ripple.
- Not matching the cutoff frequency to the sampling rate causes wrong filtering.
- Using the wrong window type can increase side lobes and ripple.
- For highpass filters,
pass_zeromust be set toFalse.
python
from scipy.signal import firwin # Wrong: cutoff frequency higher than Nyquist frequency numtaps = 51 cutoff = 5000 # Wrong if fs=8000, Nyquist=4000 fs = 8000 # This will cause incorrect filter design coefficients_wrong = firwin(numtaps, cutoff, window='hamming', pass_zero=True, fs=fs) # Right: cutoff less than Nyquist frequency cutoff_correct = 3000 coefficients_right = firwin(numtaps, cutoff_correct, window='hamming', pass_zero=True, fs=fs)
Quick Reference
Tips for FIR filter design:
- Number of taps controls filter sharpness and delay.
- Cutoff frequency must be less than half the sampling rate (Nyquist).
- Window choice affects ripple and transition width.
- Use
freqzto check frequency response visually.
Key Takeaways
Design FIR filters by choosing filter order, cutoff frequency, and window type carefully.
Ensure cutoff frequency is below Nyquist frequency (half the sampling rate).
Use window methods like Hamming to control ripple and transition sharpness.
Visualize filter response with frequency plots to verify design.
Set pass_zero parameter correctly for lowpass or highpass filters.