Key relationships
| Bin frequency | f_k = k · (f_s / N) — k ∈ [0, N/2] |
|---|---|
| Bin resolution | Δf = f_s / N (Hz per bin) |
| Nyquist | f_max unambiguous = f_s / 2 |
| Zero-padding | Interpolates between bins — does NOT add new frequency info |
| Window sum | Multiplies the amplitude — normalize by its coherent gain |
Common window functions
| Window | Main lobe width | Side-lobe level | Use |
|---|---|---|---|
| Rectangular (none) | 2 bins | −13 dB | Transient / narrow spectra |
| Hann | 4 bins | −31 dB | General purpose |
| Hamming | 4 bins | −42 dB | General, lower side-lobes |
| Blackman | 6 bins | −58 dB | Further side-lobe suppression |
| Blackman–Harris | 8 bins | −92 dB | When dynamic range matters |
| Flat-top | 5+ bins | −70 dB | Amplitude accuracy for tonal sig |
| Kaiser (β tunable) | variable | variable | Tunable trade-off |
Gotchas
- Spectral leakage: non-integer-period signals smear across bins — use a window.
- Picket-fence: a tone between bins reads low in amplitude — use flat-top or interpolate.
- Aliasing: frequencies above f_s/2 fold back — anti-alias filter before sampling.
- Inverse FFT: size must match forward. Scale by 1/N depending on library convention.
- Real input: FFT output is symmetric — only need N/2 + 1 bins (rfft).
Worked example: audio at 48 kHz
With fs = 48,000 Hz and N = 4,096 samples, each bin is 48,000 / 4,096 = 11.72 Hz wide, the window spans 4,096 / 48,000 = 85 ms, and the useful range runs from 0 to 24 kHz in 2,049 bins. A 1,000 Hz tone lands at bin 1,000 / 11.72 = 85.33, between bins 85 and 86, so its energy is split between them. With no window the reading can be low by as much as about 3.9 dB (scalloping loss); a Hann window reduces the worst case to about 1.4 dB; a flat-top window reduces it to under 0.1 dB.
To separate two tones 50 Hz apart with a Hann window (main lobe about 4 bins, so tones should be more than about 2 bins apart), you need bins narrower than 25 Hz, so N must exceed 48,000 / 25 = 1,920. Choosing 4,096 gives a margin. To get 1 Hz bins at 48 kHz you must record a full second (N = 48,000), because resolution is the inverse of the observation time, not a property of the algorithm.
Choosing settings
- Pick the window for the question. Hann is a good default. Use flat-top to read the amplitude of a tone, Blackman-Harris to see weak signals beside strong ones, and rectangular only when the signal is exactly periodic in the window.
- Average to reduce noise. Averaging magnitude spectra of many frames (Welch method, typically with 50% overlap on Hann) reduces the variance of the noise floor without changing resolution.
- Power-of-two sizes are fastest, but modern libraries handle other sizes well. Do not zero-pad just to reach a power of two if a library supports your length.
- Convert to dB with the right factor. Amplitude uses 20 × log10; power uses 10 × log10. For a real signal, single-sided amplitude is twice the FFT magnitude divided by N, except at DC and Nyquist.