ENع
→FIR by windowing
الدرس 40 · Digital filter design

FIR design by windowing

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

18 دقيقة11 خطوة5 أسئلةAdvanced
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 40 · تفاعلي
01 / 11

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)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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)
  2. 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 causal
  3. Delay 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 forever
  4. Keep 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 elsewhere
  5. Cutting 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)|
  6. 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 smaller
  7. A 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)
  8. 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.0040
  9. Flat 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 transition
  10. Test 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 samples
  11. The 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

اختبار يتاح بعد الخطوة الأخيرة.