How to Design FIR Filter Using Hamming Window - Simple Guide
To design a
FIR filter using the Hamming window, first define the ideal filter's impulse response, then multiply it by the Hamming window to reduce ripples. This method smooths the filter coefficients and controls side lobes for better frequency response.Syntax
The basic steps to design a FIR filter using the Hamming window are:
- Define the filter order
Nand cutoff frequencyfc. - Calculate the ideal impulse response
h_ideal[n]for a low-pass filter. - Create the Hamming window
w[n]of lengthN+1. - Multiply the ideal response by the window:
h[n] = h_ideal[n] * w[n].
python
N = 50 # Filter order fc = 0.3 # Normalized cutoff frequency (0 to 0.5) n = np.arange(N+1) # Ideal impulse response for low-pass filter h_ideal = 2 * fc * np.sinc(2 * fc * (n - N/2)) # Hamming window w = 0.54 - 0.46 * np.cos(2 * np.pi * n / N) # Windowed filter coefficients h = h_ideal * w
Example
This example shows how to design a low-pass FIR filter with cutoff frequency 0.3 (normalized) and order 50 using the Hamming window. It plots the filter coefficients and frequency response.
python
import numpy as np import matplotlib.pyplot as plt from scipy.signal import freqz N = 50 # Filter order fc = 0.3 # Normalized cutoff frequency (0 to 0.5) n = np.arange(N+1) # Ideal impulse response of low-pass filter h_ideal = 2 * fc * np.sinc(2 * fc * (n - N/2)) # Hamming window w = 0.54 - 0.46 * np.cos(2 * np.pi * n / N) # Windowed filter coefficients h = h_ideal * w # Frequency response w_freq, h_freq = freqz(h, worN=8000) # Plot filter coefficients plt.figure(figsize=(12,5)) plt.subplot(1,2,1) plt.stem(n, h, use_line_collection=True) plt.title('FIR Filter Coefficients (Hamming Window)') plt.xlabel('Sample') plt.ylabel('Amplitude') # Plot frequency response plt.subplot(1,2,2) plt.plot(w_freq/np.pi, 20 * np.log10(np.abs(h_freq))) plt.title('Frequency Response') plt.xlabel('Normalized Frequency (×π rad/sample)') plt.ylabel('Magnitude (dB)') plt.grid(True) plt.tight_layout() plt.show()
Output
A plot showing the FIR filter coefficients as stem plot and the frequency response in dB with a clear low-pass shape and smooth roll-off.
Common Pitfalls
Common mistakes when designing FIR filters with the Hamming window include:
- Using an incorrect cutoff frequency scale (should be normalized between 0 and 0.5).
- Not centering the ideal impulse response around
N/2, which causes phase distortion. - Forgetting to multiply by the window, which leads to high ripple in the frequency response.
- Choosing too low filter order, resulting in poor frequency selectivity.
python
import numpy as np # Wrong: cutoff frequency not normalized fc_wrong = 3000 # Should be normalized # Wrong: impulse response not centered N = 50 n = np.arange(N+1) h_ideal_wrong = 2 * 0.3 * np.sinc(2 * 0.3 * n) # Missing centering # Correct approach fc = 0.3 h_ideal_correct = 2 * fc * np.sinc(2 * fc * (n - N/2))
Quick Reference
Steps to design FIR filter using Hamming window:
- Choose filter order
N(higher means sharper cutoff). - Set normalized cutoff frequency
fc(0 to 0.5). - Calculate ideal impulse response:
h_ideal[n] = 2 * fc * sinc(2 * fc * (n - N/2)). - Generate Hamming window:
w[n] = 0.54 - 0.46 * cos(2πn/N). - Multiply:
h[n] = h_ideal[n] * w[n]. - Use
h[n]as FIR filter coefficients.
Key Takeaways
Design FIR filters by multiplying the ideal impulse response with the Hamming window to reduce ripples.
Always center the ideal impulse response around half the filter order to maintain linear phase.
Normalize cutoff frequency between 0 and 0.5 before designing the filter.
Higher filter order improves frequency selectivity but increases computation.
The Hamming window smooths coefficients and reduces side lobes in frequency response.