ENع
→Zero-padding and resolution
الدرس 29 · From the DTFT to the FFT

Zero-padding and frequency resolution

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

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

Adding zeros to a record before the DFT gives you more bins, but does it let you see more? Follow two tones only 250 Hz apart. A 16-sample record shows one bump. Padding it to 64 points draws that bump in finer detail, yet it never splits. Only a longer record, 64 real samples, shows two peaks. You will separate bin spacing (fs/L, set by the DFT length) from resolution (about 2·fs/N, set by the real data) and do both in numpy. It builds on the lessons on the DTFT and the DFT.

ستتعلّم

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

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

A short record: 16 samples

Two tones, 1000 Hz and 1250 Hz, play together. A converter samples them 8000 times a second and keeps N = 16 samples: a record 2 ms long. Can its spectrum show two tones, only 250 Hz apart?

x[n] = cos(2π·1000·n/fs) + cos(2π·1250·n/fs)
fs = 8000 samples per second
N  = 16 samples

record length = N / fs = 16 / 8000 s
              = 2 ms

the two tones are 1250 − 1000 = 250 Hz apart
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A short record: 16 samples

    Two tones, 1000 Hz and 1250 Hz, play together. A converter samples them 8000 times a second and keeps N = 16 samples: a record 2 ms long. Can its spectrum show two tones, only 250 Hz apart?

    x[n] = cos(2π·1000·n/fs) + cos(2π·1250·n/fs)
    fs = 8000 samples per second
    N  = 16 samples
    
    record length = N / fs = 16 / 8000 s
                  = 2 ms
    
    the two tones are 1250 − 1000 = 250 Hz apart
  2. The 16-point DFT: bins 500 Hz apart

    The DFT turns 16 samples into 16 values X[k]. Bin k sits at k·fs/N, so the bins are fs/N = 500 Hz apart. The tallest is 1000 Hz (0.63); 1500 Hz reads 0.34. One bump, not two.

    bin k  ->  frequency k·fs/N
    bin spacing  fs/N = 8000 / 16 = 500 Hz
    
    heights: |X[k]| / N
     k = 0   0 Hz     0.06
     k = 1   500 Hz   0.09
     k = 2   1000 Hz  0.63
     k = 3   1500 Hz  0.34
     k = 4   2000 Hz  0.13
    
    (only k = 0 .. N/2 shown: a real signal's
     spectrum mirrors above fs/2)
  3. The bins are samples of a curve

    The 16 DFT values are samples of a smooth curve: the DTFT of this 16-sample record. Bins every 500 Hz are a coarse look. The 1250 Hz tone falls between bins 2 and 3, so no bin sits on it.

    DTFT of the record:
      X(f) = Σ x[n]·e^(−j2πfn/fs),  n = 0 .. 15
      defined for EVERY f
    
    DFT: the same sum at f = k·fs/N only
      X[k] = X(k·fs/N)
    
    1250 Hz / 500 Hz = 2.5
      -> halfway between bin 2 and bin 3
  4. Zero-padding: append zeros

    Append 48 zeros so the list holds 64 values. That is zero-padding. The zeros carry no news about the tones: the real record is still 16 samples, 2 ms. But a 64-point DFT has bins every 125 Hz.

    before: x[0] .. x[15]           16 values
    after:  x[0] .. x[15], 0, 0 ... 0  64 values
                          (48 zeros)
    
    bin spacing  fs/L = 8000 / 64 = 125 Hz
    (L = DFT length, now 64)
    
    signal information: unchanged
  5. More bins on the same curve

    The 64 bins land exactly on the same curve, four times as dense. Now the top of the bump shows: bin 9 at 1125 Hz, height 0.70. Zero-padding interpolates the spectrum. It fills in between the old bins.

    64-point DFT of 16 samples + 48 zeros
    heights: |X[k]| / 16   (16 real samples)
    
     k = 8   1000 Hz  0.63   (= old bin 2)
     k = 9   1125 Hz  0.70   <- new, the top
     k = 10  1250 Hz  0.66
     k = 12  1500 Hz  0.34   (= old bin 3)
    
    every 4th new bin is an old bin
  6. Zeros cannot split the tones

    Pad to 1024 or a million points: the dots only trace this curve more finely. It has one hump, peak 1139 Hz. Each tone's lobe is 2·fs/N = 1000 Hz wide at its base, so tones 250 Hz apart blur together.

    main lobe of one tone, N real samples:
      width at the base = 2·fs/N
      N = 16:  2 × 8000 / 16 = 1000 Hz
    
    two lobes 250 Hz apart -> one hump
      top of the curve: 1139 Hz, 0.70
    
    more zeros: more dots on THIS curve
                the curve itself never changes
  7. Resolution needs a longer record

    Two tones show as two peaks when they are about one lobe width apart: 250 Hz ≥ 2·fs/N. So N ≥ 2 × 8000 / 250 = 64 real samples, 8 ms of signal. Resolution comes from recording longer, not from zeros.

    resolvable (rectangular window):
      spacing ≥ 2·fs/N
    
      250 ≥ 2 × 8000 / N
      N ≥ 64 samples
      time = N / fs = 8 ms
    
    bin spacing   fs/L : set by the DFT length L
                         (zeros help)
    resolution    2·fs/N: set by N real samples
                         (only data helps)
  8. Record 64 real samples

    The converter keeps recording: 64 samples, 8 ms. The zeros are gone; every one of the 64 values is real signal. The DFT length is 64 again, so the bins are again 125 Hz apart.

    N = 64 real samples
    record length = 64 / 8000 s = 8 ms
    
    bin spacing  fs/N  = 125 Hz   (same as padded)
    main lobe    2·fs/N = 250 Hz   (was 1000 Hz)
  9. Two peaks at last

    Now the curve has two peaks. The bins at 1000 Hz and 1250 Hz read 0.50, with a dip to 0.00 at 1125 Hz between them. Same bin spacing as the padded DFT, but four times the data. White: the old curve.

    64-point DFT of 64 real samples
    heights: |X[k]| / 64
    
     k = 8   1000 Hz  0.50
     k = 9   1125 Hz  0.00
     k = 10  1250 Hz  0.50
    
    1000 Hz × 8 ms = 8 cycles,  1250 Hz × 8 ms = 10:
    both tones sit exactly on bins
    
    the curve's own tops: 987 Hz and 1268 Hz
    (each lobe leans on its neighbour's)
  10. Zero-padding in numpy

    In numpy, np.fft.rfft(x, n=64) pads x with zeros to 64 points before the FFT. np.fft.rfftfreq(64, 1/fs) gives each bin's frequency: 0, 125, 250 ... 4000 Hz. Padding is one argument; resolution means recording longer.

    import numpy as np
    fs = 8000
    n = np.arange(16)
    x = np.cos(2*np.pi*1000*n/fs) + np.cos(2*np.pi*1250*n/fs)
    X16 = np.fft.rfft(x)
    X64 = np.fft.rfft(x, n=64)
    f = np.fft.rfftfreq(64, 1/fs)
    
    >>> len(X16), len(X64)
    (9, 33)
    >>> f[np.argmax(abs(X64))]
    1125.0
    
    (Python 3 with numpy)

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