ENع
→Leakage and windowing
الدرس 32 · From the DTFT to the FFT

Leakage and windowing

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

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

The DFT only ever sees one block of samples, and it treats that block as if it repeated forever. When a tone fits a whole number of cycles in the block, it lands on one bin. When it does not, the repeated block has a seam, and the tone's energy leaks into every bin, enough to bury a quiet tone. Follow a 750 Hz and a 875 Hz tone through a 32-sample FFT, then taper the block with a Hann window and watch the quiet 2500 Hz tone come back, at the price of a wider peak. It builds on the lessons on the DTFT, the DFT, zero-padding and the FFT, and on decibels.

ستتعلّم

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

شكل 32 · تفاعلي
01 / 10

One block of 32 samples

A loud 750 Hz tone plus a quiet 2500 Hz tone, 100 times smaller, are sampled 8000 times a second. We keep one block of N = 32 samples (4 ms). Its DFT has a bin every fs/N = 250 Hz.

fs = 8000 samples per second
N  = 32 samples  ->  32 / 8000 s = 4 ms

x[n] = cos(2π·750·n/fs) + 0.01·cos(2π·2500·n/fs)

bin spacing  fs / N = 8000 / 32 = 250 Hz
bins 0 .. 16: 0 Hz .. 4000 Hz (fs/2)

buffer: x[0] x[1] ... x[30] x[31]
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. One block of 32 samples

    A loud 750 Hz tone plus a quiet 2500 Hz tone, 100 times smaller, are sampled 8000 times a second. We keep one block of N = 32 samples (4 ms). Its DFT has a bin every fs/N = 250 Hz.

    fs = 8000 samples per second
    N  = 32 samples  ->  32 / 8000 s = 4 ms
    
    x[n] = cos(2π·750·n/fs) + 0.01·cos(2π·2500·n/fs)
    
    bin spacing  fs / N = 8000 / 32 = 250 Hz
    bins 0 .. 16: 0 Hz .. 4000 Hz (fs/2)
    
    buffer: x[0] x[1] ... x[30] x[31]
  2. The DFT sees a loop

    The DFT treats the block as one period of a signal that repeats forever. 750 Hz fits exactly 3 whole cycles in 4 ms, so the end of the block flows smoothly into its start.

    750 Hz × 4 ms = 3 cycles (a whole number)
    750 / 250 = bin 3
    
    the DFT's view: x[n + 32] = x[n]
      ... | block | block | block | ...
    
    plot: the block (n 0 .. 31) and the start
    of its repeat (n 32 .. 47), shaded
  3. On a bin: two clean lines

    750 Hz is bin 3 and 2500 Hz is bin 10. The FFT shows the loud tone at 0 dB and the quiet one at −40 dB (100 times smaller). Every other bin is zero, so both tones are easy to see.

    level of bin k:  A = 2·|X[k]| / Σw   (Σw = 32)
                     in dB: 20·log10(A)
    
    bin 3  (750 Hz):  0.0 dB   loud tone
    bin 10 (2500 Hz): −40.0 dB   quiet tone
    every other bin: 0 (below the plot floor)
    
    thin curve: the DTFT of the block;
    the bins are its samples every 250 Hz
  4. Between bins: a seam

    Retune the loud tone to 875 Hz, bin 3.5, halfway between two bins. Now 3.5 cycles fit in the block. Where the loop joins end to start, the wave jumps from −1 back to +1: a seam.

    875 Hz × 4 ms = 3.5 cycles
    875 / 250 = bin 3.5
    
    where the wave would be at n = 32:
      cos(2π·3.5) = −1.00
    start of the next block (n = 0):
      cos(0)      = 1
    
    the loop has a jump of about 2
    at every block boundary
  5. Leakage: energy in every bin

    A jump needs many frequencies to build it, so the tone's energy leaks into every bin. Its biggest bin reads only −3.4 dB, and bins far away still hold −24.1 dB. This spreading is spectral leakage.

    rectangle window (all ones), 875 Hz:
    bin 3: −4.5 dB   bin 4: −3.4 dB
      (the tone sits between them)
    bin 0 and bin 16: −24.1 dB
    
    rectangle side lobes: first one ≈13 dB down,
    falling about 6 dB per octave
      (octave = each doubling of distance from the peak)
    more samples do not lower the peak leakage
  6. Leakage hides the quiet tone

    Bin 10 should read −40 dB, the quiet tone. But leakage from the loud tone alone already puts −21.9 dB there, about 8 times more. Bin 10 reads −21.1 dB, and the quiet tone is buried.

    at bin 10 (2500 Hz):
      quiet tone alone       −40.0 dB
      loud tone's leakage    −21.9 dB
      both together          −21.1 dB
    
    −21.9 dB − (−40 dB) = 18.1 dB
      -> 8 times bigger in amplitude
    no peak at bin 10: the quiet tone is lost
  7. The Hann window tapers the ends

    Cutting out a block already used a rectangle window: all ones. Instead, multiply each sample by the Hann window w[n] = 0.5 − 0.5·cos(2πn/N): near 0 at both ends, 1 in the middle. The seam is gone.

    w[n] = 0.5 − 0.5·cos(2π·n/32),  n = 0 .. 31
      w[0] = 0   w[16] = 1   w[31] = 0.0096
    
    windowed sample: x[n]·w[n]
      x[0]·w[0]   = 0
      x[31]·w[31] = −0.0075
    
    both ends of the loop sit near 0: no jump
    Σw = 16 (the rectangle's is 32)
  8. Leakage falls away

    Away from the peak, leakage now drops fast. The Hann side lobes (small humps beside the peak) start 31.5 dB down and fall 18 dB per octave (each doubling of distance from the peak). Bin 10 reads −39.9 dB: the quiet tone is back.

    Hann window, 875 Hz:
    bin 3: −1.4 dB   bin 4: −1.4 dB
    bin 9: −45.3 dB   bin 10: −39.9 dB   bin 11: −45.9 dB
    bin 14: −73.9 dB
    
    loud tone's leakage at bin 10: −58.0 dB
      (was −21.9 dB with the rectangle)
    bin 10 is a peak again
  9. The price: a wider peak

    Nothing is free. The Hann main lobe (the central peak) is 4 bins wide at its base, twice the rectangle's 2 bins, so tones close together blur sooner. Pick the window for the job: sharp peaks or low leakage.

    main lobe width at the base (first zeros):
      rectangle  2 bins = 500 Hz
      Hann       4 bins = 1000 Hz
    
    first side lobe:  rectangle ≈−13 dB, Hann −31.5 dB
    roll-off:         6 vs 18 dB per octave
    
    longer blocks (bigger N) narrow both lobes
  10. Windowing in Python

    With numpy, windowing the block is one line and the spectrum another. Dividing by w.sum() = 16, not 32, makes up for the window's halved gain, so a tone on a bin still reads 0 dB.

    import numpy as np
    n = np.arange(32)
    x = np.cos(2*np.pi*875*n/8000)
    x += 0.01*np.cos(2*np.pi*2500*n/8000)
    w = 0.5 - 0.5*np.cos(2*np.pi*n/32)
    A = 2*abs(np.fft.rfft(x*w))/w.sum()
    
    >>> np.round(20*np.log10(A[[3, 4, 10]]), 1)
    array([-1.4, -1.4, -39.9])
    >>> len(A)
    17
    
    same w: scipy.signal.windows.hann(32, sym=False)
    (np.hanning(32) is the symmetric form: n/31)

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