Zero Padding and Frequency Resolution
Appending zeros to a signal before the FFT gives more frequency bins. It is tempting to think this improves resolution. It does not: it interpolates the same underlying spectrum on a finer grid. True resolution, the ability to separate close frequencies, depends only on how long you observed the signal. Telling the two apart is one of the most practical lessons in spectral analysis.
Two different "resolutions"
- Bin spacing : how finely the spectrum is sampled. Zero padding shrinks it.
- Resolution about : the width of the spectral peak of a tone observed for seconds. Only more data shrinks it.
Zero padding evaluates the DTFT (the continuous-frequency spectrum of the finite data) at more points; the shape of that curve is fixed by the data.
import numpy as np
fs = 100
t = np.arange(0, 0.5, 1 / fs) # 50 samples: 0.5 s of data
x = np.sin(2 * np.pi * 21.3 * t) # off-bin tone (bins are 2 Hz apart)
for nfft in (50, 200, 2000):
A = np.abs(np.fft.rfft(x, n=nfft))
f = np.fft.rfftfreq(nfft, 1 / fs)
k = np.argmax(A)
half = f[A >= A[k] / 2] # -6 dB width of the main peak
half = half[np.abs(half - f[k]) < 5]
print(f"nfft {nfft:5d}: bin spacing {fs / nfft:5.2f} Hz, peak at {f[k]:6.2f} Hz, -6 dB width {half.max() - half.min():.2f} Hz")nfft 50: bin spacing 2.00 Hz, peak at 22.00 Hz, -6 dB width 2.00 Hz
nfft 200: bin spacing 0.50 Hz, peak at 21.50 Hz, -6 dB width 2.00 Hz
nfft 2000: bin spacing 0.05 Hz, peak at 21.30 Hz, -6 dB width 2.40 Hz
Padding locates the peak more precisely (21.30 Hz instead of the nearest bin, 22 Hz), but the peak itself does not get narrower: on the finest grid its -6 dB width is about 2.4 Hz, of the order of Hz. (The coarse grids are too coarse to show the width at all.)
Resolving two tones
Two tones 1 Hz apart, observed for 0.5 s ( Hz), cannot be separated however much you pad. Observing for 2 s can:
import numpy as np
fs = 100
def count_peaks(duration, nfft):
t = np.arange(0, duration, 1 / fs)
x = np.sin(2 * np.pi * 20 * t) + np.sin(2 * np.pi * 21 * t)
A = np.abs(np.fft.rfft(x, n=nfft))
f = np.fft.rfftfreq(nfft, 1 / fs)
band = (f > 17) & (f < 25)
a = A[band]
return [round(float(fv), 2) for fv, i in zip(f[band][1:-1], range(1, len(a) - 1))
if a[i] > a[i - 1] and a[i] > a[i + 1] and a[i] > 0.5 * a.max()]
print("0.5 s, padded to 8192:", count_peaks(0.5, 8192))
print("2.0 s, no padding: ", count_peaks(2.0, 200))
print("2.0 s, padded to 8192:", count_peaks(2.0, 8192))0.5 s, padded to 8192: [20.5]
2.0 s, no padding: [20.0, 21.0]
2.0 s, padded to 8192: [19.93, 21.06]
When to pad
- To make the FFT length fast (a power of two or
next_fast_len). - To avoid wrap-around in fast convolution.
- To read off peak frequencies more precisely, or to make spectrum plots smooth.
- Never as a substitute for a longer recording.
In software
np.fft.rfft(x, n=nfft)pads automatically whennfft > len(x).scipy.signal.periodogram(x, fs, nfft=...)andwelch(..., nfft=...)accept padding; thenpersegparameter controls true resolution.
Exercises
Exercise 1: Bin spacing versus resolution
You record 0.1 s at 48 kHz and pad to 65536 points. What are the bin spacing and the true resolution?
Answer
Bin spacing about 0.73 Hz; resolution about 10 Hz.
Solution
Bin spacing Hz. The data last s, so tones closer than roughly Hz merge.
Exercise 2: Padding in the time domain
What does zero padding in the frequency domain (then inverse FFT) do to a signal?
Answer
It interpolates the signal to a higher sampling rate (band-limited interpolation).
Solution
By duality, padding the spectrum with zeros at high frequencies and inverting gives more time samples of the same band-limited periodic signal: the sinc interpolation of Reconstruction. scipy.signal.resample works this way.
Exercise 3: Estimate a frequency precisely (code)
Estimate the frequency of a 0.25 s recording of a 13.37 Hz tone at Hz by padding to 65536 points.
Answer
13.38 Hz (the true value is 13.37 Hz), even though the bin spacing without padding is 4 Hz.
Solution
import numpy as np
fs = 200
t = np.arange(0, 0.25, 1 / fs)
x = np.cos(2 * np.pi * 13.37 * t)
A = np.abs(np.fft.rfft(x, n=65536))
print(round(float(np.fft.rfftfreq(65536, 1 / fs)[np.argmax(A)]), 2))13.38
For a single clean tone the peak of the padded spectrum is an excellent estimator; with two close tones or noise it degrades.
Test yourself
Zero padding before an FFT…
- improves frequency resolution
- samples the same spectrum on a finer grid
- removes noise
- reduces leakage
Frequency resolution is approximately…
- , with the observation time
A good reason to zero pad is…
- to make two merged tones separate
- to avoid wrap-around in FFT convolution
- to increase the sampling rate of the data
- to remove aliasing