FIR design by windowing
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
The moving average smooths, but you cannot tell it where to stop. The window method can: say what gain you want at every frequency, turn that into an impulse response, and cut it down to a filter you can run. Follow one design from start to finish: a 1000 Hz lowpass at 8000 samples a second with 21 taps. The ideal sinc is delayed and cut short (a rectangle window), which leaves 7.3% overshoot and only −19.6 dB of stopband: the Gibbs ripples. A Hamming window tapers the taps and pushes the stopband below −49.8 dB, at the price of a wider transition. Then the design runs in four lines of Python with numpy. It builds on the DTFT, frequency response and windowing lessons, and on the Circuits lessons on filters and Bode plots.
Say what you want
We sample at 8000 Hz and want a lowpass filter: keep everything below 1000 Hz, remove everything above. The ideal answer is a brick wall: gain 1 up to the cutoff, gain 0 after it.
fs = 8000 samples per second
highest frequency: fs/2 = 4000 Hz
ask: gain 1 for 0 .. 1000 Hz (passband)
gain 0 for 1000 .. 4000 Hz (stopband)
cutoff as an angle per sample:
ωc = 2π·1000 / 8000 = π/4 rad
plots: gain against frequency,
plain (0 .. 1.2) and in decibels (dB)اقرأ الدرس خطوة بخطوة
Say what you want
We sample at 8000 Hz and want a lowpass filter: keep everything below 1000 Hz, remove everything above. The ideal answer is a brick wall: gain 1 up to the cutoff, gain 0 after it.
fs = 8000 samples per second highest frequency: fs/2 = 4000 Hz ask: gain 1 for 0 .. 1000 Hz (passband) gain 0 for 1000 .. 4000 Hz (stopband) cutoff as an angle per sample: ωc = 2π·1000 / 8000 = π/4 rad plots: gain against frequency, plain (0 .. 1.2) and in decibels (dB)The ideal filter is a sinc
Run the brick wall backwards through the inverse DTFT (the DTFT lesson, run in reverse) and you get its impulse response: hi[m] = sin(ωc·m)/(π·m), a sinc. Its middle value is ωc/π = 0.25. It never ends, in either direction, so no machine can run it.
hi[m] = sin(ωc·m) / (π·m), hi[0] = ωc/π with ωc = π/4: hi[0] = 0.2500 hi[±1] = sin(π/4)/π = 0.2251 hi[±2] = sin(π/2)/2π = 0.1592 hi[±4] = sin(π)/4π = 0 m runs from −∞ to +∞ m < 0 needs future inputs: not causalDelay it by 10 samples
A filter cannot use inputs that have not arrived. So slide the sinc right by 10 samples: hd[n] = hi[n − 10]. The peak now sits at n = 10. A delay only adds a 1.25 ms lag; it leaves the gain alone.
hd[n] = hi[n − 10] peak at n = 10 delay 10 samples = 10 / 8000 s = 1.25 ms a delay multiplies H by e^(−jω·10): |e^(−jω·10)| = 1, the gain is unchanged the phase falls in a straight line (linear phase: next lesson) but the tails still run foreverKeep 21 taps
Keep only n = 0 to 20: 21 numbers, called taps. Throwing the rest away is the same as multiplying by a rectangle window, 1 inside and 0 outside. Now it is a real FIR filter.
hd[n] = hi[n − 10], n = 0 .. 20 hd[0] = 0.0318 hd[4] = −0.0531 hd[8] = 0.1592 hd[9] = 0.2251 hd[10] = 0.2500 symmetric: hd[20 − n] = hd[n] rectangle window: r[n] = 1 for 0 ≤ n ≤ 20 r[n] = 0 elsewhereCutting makes ripples
The 21-tap response wiggles. It overshoots to 1.073 near 643 Hz (7.3% too high), and in the stopband its biggest ripple is only −19.6 dB: about one tenth of the input gets through. These ripples come from the sharp cut.
21 taps, rectangle window: biggest gain 1.073 at 643 Hz gain at 1000 Hz 0.52 (about −6 dB) first zero 1221 Hz worst stopband −19.6 dB at 1367 Hz (10^(−19.6/20) = 0.104) |H(f)| = |Σ hd[n]·e^(−j2πfn/fs)|More taps do not fix it
Multiplying by a rectangle in time smears the brick wall with the rectangle's spectrum, whose side lobes are big. With 61 taps the edge gets steeper, but the overshoot grows to 8.4%. It heads for about 9%, never zero: the Gibbs phenomenon.
time: h = hd × r (multiply) frequency: H = Hideal ⊛ R (smear) overshoot, rectangle window: 21 taps 7.3% 61 taps 8.4% many taps -> about 8.95% (Gibbs) more taps: ripples narrower, not smallerA gentler window: Hamming
Instead of a hard cut, fade the taps out. The Hamming window w[n] = 0.54 − 0.46·cos(2πn/20) is 0.08 at both ends and 1 in the middle. Its spectrum has much smaller side lobes than the rectangle's.
w[n] = 0.54 − 0.46·cos(2π·n/20), n = 0 .. 20 w[0] = 0.08 w[4] = 0.3979 w[8] = 0.9121 w[10] = 1.0000 symmetric: w[20 − n] = w[n] same as numpy.hamming(21)Multiply tap by tap
Each tap is multiplied by its window value: h[n] = hd[n]·w[n]. The middle stays almost whole; the ends shrink. For example h[4] = −0.0531 × 0.3979 = −0.0211. These 21 numbers are the filter.
h[n] = hd[n] · w[n] h[0] = 0.0318 × 0.08 = 0.0025 h[4] = −0.0531 × 0.3979 = −0.0211 h[8] = 0.1592 × 0.9121 = 0.1452 h[10] = 0.2500 × 1.0000 = 0.2500 still symmetric: h[20 − n] = h[n] sum of all taps = gain at 0 Hz = 1.0040Flat passband, deep stopband
Now the gain never rises above 1.004 and the stopband stays below −49.8 dB, against −19.6 dB before. The price: a wider transition. The gain first reaches zero at 1743 Hz instead of 1221 Hz.
rectangle Hamming biggest gain 1.073 1.004 gain at 1000 Hz 0.52 0.50 first zero 1221 Hz 1743 Hz worst stopband −19.6 dB −49.8 dB textbook values for long filters: rectangle about −21 dB, Hamming about −53 dB longer filter -> narrower transitionTest it on two tones
Load the taps into the filter and feed it two tones. 500 Hz comes out at −0.31 dB, almost untouched. 2000 Hz comes out at −49.8 dB: about 300 times smaller in amplitude. The filter does what we asked.
y[n] = Σ h[k]·x[n − k], k = 0 .. 20 Hamming design: 500 Hz: −0.31 dB (gain 0.965) 2000 Hz: −49.8 dB (gain 0.0032) rectangle design, for comparison: 500 Hz: 0.22 dB 2000 Hz: −31.7 dB output lags the input by 10 samplesThe whole design in Python
In Python 3 with numpy, the design is four lines. np.sinc(x) means sin(πx)/(πx), so 0.25*np.sinc(0.25*m) is the ideal sinc. scipy.signal.firwin(21, 1000, fs=8000) does the same, then scales the gain at 0 Hz to exactly 1.
import numpy as np n = np.arange(21) hd = 0.25*np.sinc(0.25*(n-10)) w = np.hamming(21) h = hd*w >>> np.round(h[[0, 4, 8, 10]], 4) array([ 0.0025, -0.0211, 0.1452, 0.25 ]) >>> print(np.round(h.sum(), 4)) 1.004 firwin = window method, Hamming by default, then h / h.sum() so the gain at 0 Hz is 1
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01An ideal lowpass filter has cutoff ωc = π/4 rad per sample. What is the middle tap of its impulse response, hi[0]?
hi[m] = sin(ωc·m)/(π·m); as m → 0 this tends to ωc/π = (π/4)/π = 0.25. It equals the fraction of the band from 0 to π that the filter passes.
02Why is the ideal sinc delayed by (L − 1)/2 samples before it is cut short?
The sinc has values for negative n, which would need future inputs. Shifting it right by 10 samples puts all 21 kept taps at n = 0 .. 20. A delay leaves the gain unchanged and only adds a straight-line phase.
03A rectangle-windowed design overshoots by 7.3% with 21 taps. What happens with 61 taps?
This is the Gibbs phenomenon: cutting the sinc off sharply always leaves an overshoot that tends to about 9% of the jump. More taps squeeze the ripples toward the edge but do not shrink them.
04Switching from the rectangle window to the Hamming window with the same 21 taps…
The tapered window has much smaller side lobes (stopband −19.6 dB → −49.8 dB here) but a wider main lobe, so the gain first reaches zero at 1743 Hz instead of 1221 Hz.
05In numpy, which line gives the 21 delayed ideal taps for ωc = π/4?
np.sinc(x) is sin(πx)/(πx), so 0.25·sinc(0.25·m) = sin(πm/4)/(πm). The (n − 10) does the delay. The third option divides by zero at n = 0 and has no delay.
المسرد
- FIR filter
- A finite impulse response filter: the output is a weighted sum of the last L inputs, y[n] = Σ h[k]·x[n − k]. The weights h[k] are the taps.
- Tap
- One coefficient h[k] of an FIR filter. A filter with L taps uses the current input and the L − 1 before it.
- Ideal lowpass
- A brick-wall response: gain 1 below the cutoff ωc, 0 above. Its impulse response is the sinc sin(ωc·n)/(π·n), which never ends.
- Window method
- FIR design by taking an ideal impulse response, delaying it, and multiplying it by a finite window w[n].
- Rectangle window
- Keeping L samples unchanged and dropping the rest: w[n] = 1 inside, 0 outside. It gives the sharpest edge but the biggest ripples.
- Hamming window
- w[n] = 0.54 − 0.46·cos(2πn/(L − 1)): 0.08 at the ends, 1 in the middle. It trades a wider transition for a much lower stopband.
- Gibbs phenomenon
- The overshoot near a jump when a response is cut off sharply; it tends to about 9% of the jump and does not shrink with more terms.
- Transition band
- The frequencies between the passband and the stopband where the gain falls; its width is set by the window's main lobe.
الملاحظات والمصادر
- The sample rate, the 1000 Hz cutoff and the 21 taps are a chosen example; every tap, gain and dB figure is computed from this model with the DTFT sum on a 1 Hz grid, and the Python card was checked with numpy. Gains are plain ratios of output to input amplitude; dB = 20·log10(gain).
- Oppenheim & Schafer's Table 7.2 lists peak stopband errors of about −21 dB (rectangle) and −53 dB (Hamming); those are for long filters. With only 21 taps this design measures −19.6 dB and −49.8 dB.
- scipy.signal.firwin uses the same window method with the Hamming window by default, then divides by the sum of the taps (1.0040 here), so its taps are about 0.4% smaller than h[n] on the card.
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed., §7.2 FIR design by windowing, Table 7.2 (course text listed on MIT OCW 6.341 readings)
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim): FIR and IIR filter design
- MIT OpenCourseWare 6.003 Signals and Systems (D. Freeman, Fall 2011)
- J. O. Smith III, Spectral Audio Signal Processing (CCRMA, Stanford): The Window Method for FIR Filter Design
- Gibbs phenomenon (Wikipedia): the overshoot tends to about 9% of the jump and does not shrink with more terms
- Window function (Wikipedia): the Hamming window, a0 ≈ 0.54, and the window method for FIR filters
- NumPy documentation: numpy.sinc (normalized, sin(πx)/(πx))
- NumPy documentation: numpy.hamming
- SciPy documentation: scipy.signal.firwin (window method, Hamming by default, scale to unit gain)
اختبار يتاح بعد الخطوة الأخيرة.