Zero-padding and frequency resolution
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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اقرأ الدرس خطوة بخطوة
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 apartThe 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)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 3Zero-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: unchangedMore 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 binZeros 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 changesResolution 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)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)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)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)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A record of 16 samples at 8000 samples per second is zero-padded to 64 points. How far apart are the bins of the 64-point DFT?
The bin spacing is fs/L with L the DFT length: 8000 / 64 = 125 Hz. Without padding it was 8000 / 16 = 500 Hz.
02What does zero-padding do to the spectrum of a record?
The zeros add no information, so the DTFT of the record is unchanged. A longer DFT only evaluates that same curve at more frequencies: interpolation. Peak width, side lobes and the fs/2 limit stay where they were.
03Two tones are 250 Hz apart and fs = 8000 samples/s. Using the rule "separation ≥ 2·fs/N", how many real samples do you need?
N ≥ 2·fs / separation = 2 × 8000 / 250 = 64 samples, that is 8 ms of signal. Zeros do not count: they are not data.
04You recorded only 2 ms of the two tones. Which change can reveal two separate peaks?
Resolution is set by how long the real record is: the main lobe is 2·fs/N wide. Going from 16 to 64 real samples narrows it from 1000 Hz to 250 Hz. Padding and bigger FFT sizes only add dots on the same blurred curve.
05In numpy, what does np.fft.rfft(x, n=64) do when x has 16 values?
The numpy documentation: if n is larger than the input, the input is padded with zeros. rfft returns only the non-negative frequencies, n//2 + 1 = 33 values.
المسرد
- Zero-padding
- Appending zeros to a record before taking its DFT, so the DFT is longer. It adds bins, not information.
- Record length
- How much real signal you captured: N samples, or N / fs seconds.
- Bin spacing
- The distance in Hz between DFT bins, fs/L, where L is the DFT length (real samples plus any zeros).
- Frequency resolution
- How close two tones can be and still show as two peaks. With a plain (rectangular) record it is about 2·fs/N, set by the N real samples.
- Main lobe
- The central hump that one tone makes in the spectrum of a finite record. With N real samples its base is 2·fs/N wide.
- DTFT of a record
- The smooth spectrum X(f) defined at every frequency; the DFT values are samples of it at f = k·fs/L.
- Spectral interpolation
- Filling in values between known points of a spectrum. Zero-padding does exactly this for the DTFT.
الملاحظات والمصادر
- Every number is computed from this model: two tones of amplitude 1 at 1000 Hz and 1250 Hz, sampled at 8000 samples a second. Heights are |X| divided by the number of real samples, so a tone that stands alone reads 0.5. The plot stops at 2500 Hz; above it, up to fs/2 = 4000 Hz, there is only small ripple.
- "About one main-lobe width, 2·fs/N" is the rectangular-window rule from J. O. Smith's Spectral Audio Signal Processing. It is a rule of thumb: tapered windows (the Windowing lesson) trade a wider lobe for lower side lobes.
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed., Prentice Hall 1999 (course text listed on MIT OCW 6.341 readings)
- A. V. Oppenheim, A. S. Willsky, S. H. Nawab, Signals and Systems, 2nd ed., Pearson (ch. 5: the discrete-time Fourier transform)
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim): DFT and FFT
- MIT OpenCourseWare 6.003 Signals and Systems (D. Freeman, Fall 2011)
- J. O. Smith III, Mathematics of the DFT (CCRMA, Stanford): Zero Padding
- J. O. Smith III, Mathematics of the DFT (CCRMA, Stanford): Zero Padding Theorem (Spectral Interpolation)
- J. O. Smith III, Spectral Audio Signal Processing (CCRMA, Stanford): Resolving Sinusoids (main lobe 2·fs/M)
- Discrete Fourier transform (Wikipedia): the DFT samples the DTFT; zero-padding
- NumPy documentation: numpy.fft.rfft (n larger than the input pads with zeros)
- NumPy documentation: numpy.fft.rfftfreq
اختبار يتاح بعد الخطوة الأخيرة.