---
title: Spectral analysis with windows
heading: The DFT of a windowed sinusoid, leakage and scalloping, resolution, zero padding, coherent gain and equivalent noise bandwidth, and reading amplitudes off a spectrum
eyebrow: S7 · Lesson 29 · Spectral analysis with windows
xp: 50
---

::: hook
Point an FFT at a signal and it returns numbers that look like a spectrum, but they're the spectrum of a finite, windowed piece of the signal, sampled at N frequencies. A tone that doesn't fall exactly on a bin smears across many of them, a strong tone's sidelobes can bury a weak one nearby, and the peak reads up to 4 dB low depending on where the tone happens to sit. Every spectrum analyzer and every FFT plot is shaped by these effects. This lesson quantifies them so the numbers an FFT gives can be read as measurements, with known errors.
:::

::: play
- The 32-point DFT magnitudes (stems, in dB) of a tone at bin 8 + d, with a rectangular window and with a Hann window (positive-frequency component, normalized to each window's on-bin peak).
- With d = 0 the rectangular window shows one clean bin, but move the tone between bins and its energy leaks across the whole spectrum, falling off slowly.
- The Hann window always smears over three bins but its leakage dies fast. At d = 0.5 the rectangular peak drops 3.92 dB and the Hann peak only 1.42 dB: the scalloping loss.
:::
:::

```sim plot
view: graph
x: k = 0..16
y: X = -80..2 dB
slider: d = 0..1 = 0.5 step 0.05
plot: 20*log(abs(sin(pi*(k - 8 - d + 1e-9))/(32*sin(pi*(k - 8 - d + 1e-9)/32)))) | stems | label: rectangular window
plot: 20*log(abs((0.5*sin(pi*(k - 8 - d + 1e-9))/(32*sin(pi*(k - 8 - d + 1e-9)/32)) + 0.25*sin(pi*(k - 9 - d + 1e-9))/(32*sin(pi*(k - 9 - d + 1e-9)/32)) + 0.25*sin(pi*(k - 7 - d + 1e-9))/(32*sin(pi*(k - 7 - d + 1e-9)/32)))/0.5)) | stems | color: ch2 | label: Hann window
```

::: predict
## A 1024-point FFT at 8 kHz has 7.8 Hz bins. Will zero-padding it to 8192 points let it separate two tones 4 Hz apart?

- [ ] Yes: the bins become 0.98 Hz wide
- [x] No: zero padding interpolates the same smeared spectrum more finely, without narrowing its peaks
- [ ] Yes, but only with a rectangular window
- [ ] Only if the tones have equal amplitude

::: reveal
No. Resolution is set by how long the signal was observed, here 1024/8000 = 128 ms, which gives main lobes about 7.8 Hz wide even for the rectangular window (and twice that for Hann). Zero padding samples that same spectrum more densely, which helps locate a single peak precisely, but two tones closer than the main-lobe width still merge into one lump. Only a longer record separates them.
:::
:::

::: nameit
## The windowed tone

A tone at bin $k_0 + d$ (d the fractional offset) windowed by w[n] gives a DFT equal to the window's spectrum $W$ centered on $k_0 + d$ and sampled at the integer bins. For the rectangular window, $|X[k]| \propto \left|\frac{\sin\pi(k - k_0 - d)}{N\sin(\pi(k - k_0 - d)/N)}\right|$.

- **Leakage**: energy in bins far from the tone, set by the window's sidelobes.
- **Scalloping loss**: the peak's drop when the tone falls between bins: 3.92 dB for the rectangle at d = ½, 1.42 dB for Hann, 0.01 dB for a flat-top window.
- **Resolution**: two tones separate only if they're further apart than about the main-lobe half-width, which is 1 bin for the rectangle and 2 bins for Hann and Hamming. That's $f_s/N$ or $2f_s/N$ in hertz: the record length T = N/f_s sets it.
- **Zero padding** to M > N points samples the same spectrum at spacing $f_s/M$: finer interpolation, no better resolution.

## Reading amplitudes and noise

- **Coherent gain** $\text{CG} = \frac{1}{N}\sum w[n]$: 1 for the rectangle, 0.5 for Hann, 0.54 for Hamming. A tone of amplitude A on a bin gives $|X[k]| = \frac{AN}{2}\text{CG}$, so $A = \frac{2|X[k]|}{N\cdot\text{CG}}$.
- **Equivalent noise bandwidth** $\text{ENBW} = N\frac{\sum w^2}{(\sum w)^2}$ bins: 1 for the rectangle, 1.5 for Hann, 1.36 for Hamming, 1.73 for Blackman. Each bin collects noise from ENBW × $f_s/N$ hertz.
- **Processing gain**: a tone rises above white noise by $10\log(N/\text{ENBW})$ dB more than it does in the time domain.
:::

::: example
## A tone falls exactly halfway between two bins. How much lower does its peak read with a rectangular and with a Hann window?

::: solution
1. **Rectangular window.**
   $$\begin{aligned} \frac{\sin(\pi/2)}{N\sin(\pi/2N)} &\approx \frac{2}{\pi} \\ &= 0.637 \end{aligned}$$
   which is −3.92 dB.
2. **Hann window.**
   $$\begin{aligned} G &= 2[0.5(0.637) \\ &\quad + 0.25(0.637) \\ &\quad + 0.25(-0.212)] \\ &= 0.849 \end{aligned}$$
   which is −1.42 dB.
3. **Why Hann loses less.** Its wider main lobe is flatter on top.

**Answer:** the peak reads 3.92 dB low with a rectangular window and 1.42 dB low with a Hann window.
:::
:::

::: example
## A 1024-point Hann-windowed FFT at 48 kHz shows a peak of |X| = 5,000 on a tone's bin, with white noise in the background. What's the tone's amplitude, and how much processing gain does the FFT give?

::: solution
1. **Amplitude.**
   $$\begin{aligned} A &= \frac{2 \times 5000}{1024 \times 0.5} \\ &= 19.5 \end{aligned}$$
2. **Processing gain.**
   $$\begin{aligned} G &= 10\log(1024/1.5) \\ &= 28.3\ \text{dB} \end{aligned}$$
   So a tone 20 dB below the noise in the time domain shows up 8.3 dB above the noise floor in each bin.
3. **Resolution.** Bins are 46.9 Hz apart, and Hann resolves tones about 94 Hz apart.

**Answer:** the tone's amplitude is 19.5, and the FFT gives 28.3 dB of processing gain.
:::
:::

::: example
## A tone sits 20 dB below white noise (SNR −20 dB in the time domain). Will a 4096-point Hann-windowed FFT show it?

::: solution
1. **Processing gain.**
   $$\begin{aligned} G &= 10\log_{10}(N/\text{ENBW}) \\ &= 10\log_{10}(4096/1.5) \\ &= 34.4\ \text{dB} \end{aligned}$$
2. **SNR in the bin.**
   $$\begin{aligned} \text{SNR} &= -20 + 34.4 \\ &= 14.4\ \text{dB} \end{aligned}$$
   That's clearly visible, if the tone stays put for the whole record.
3. **Longer records.** Each doubling of N adds 3 dB, which is how spectrum analyzers dig tones out from under noise.

**Answer:** yes: the tone's bin shows an SNR of 14.4 dB.
:::
:::

::: check
given: d = 0.2 | 0.35 | 0.5; N = 64 | 1024
answer: 20*log(sin(pi*d)/(N*sin(pi*d/N)))
unit: dB
tolerance: 0.02

## A tone sits {d} bin from the nearest bin of an {N}-point rectangular-window DFT. How much does its peak read low?

::: right
Right. $20\log\frac{\sin\pi d}{N\sin(\pi d/N)}$ = {answer} dB.
:::

::: misses
- [10*log(sin(pi*d)/(N*sin(pi*d/N)))] It's an amplitude ratio: 20 log.
- [any] Not yet. The rectangular window's spectrum, d bins off center.
:::
:::

::: check
given: s = 8 | 44.1 | 96; N = 512 | 4096
answer: 2*s*1000/N
unit: Hz
tolerance: 0.5%

## Roughly how close can two equal tones be and still be resolved by an {N}-point Hann-windowed FFT at {s} kHz?

::: right
Right. About two bins: $2f_s/N$ = {answer} Hz.
:::

::: misses
- [s*1000/N] That's the rectangular window's resolution; Hann's main lobe is twice as wide.
- [any] Not yet. Hann's main-lobe half-width is 2 bins.
:::
:::

::: check
given: X = 1000 | 25000; N = 256 | 2048; c = 0.42 | 0.5 | 0.54
answer: 2*X/(N*c)
tolerance: 0.5%

## A tone on a bin shows |X| = {X} in an {N}-point DFT with a window whose coherent gain is {c}. What's its amplitude?

::: right
Right. $A = 2|X|/(N\cdot\text{CG})$ = {answer}.
:::

::: misses
- [X/(N*c)] The cosine's energy splits between two bins: multiply by 2.
- [2*X/N] Correct for the window's coherent gain.
- [any] Not yet. Invert $|X| = \frac{AN}{2}\text{CG}$.
:::
:::

::: check
given: N = 256 | 1024 | 16384; E = 1.36 | 1.5 | 1.73
answer: 10*log(N/E)
unit: dB
tolerance: 0.05

## An {N}-point FFT uses a window with ENBW = {E} bins. What processing gain does it give a tone in white noise?

::: right
Right. $10\log(N/\text{ENBW})$ = {answer} dB.
:::

::: misses
- [10*log(N)] Account for the window's noise bandwidth.
- [any] Not yet. Each bin's noise grows with ENBW.
:::
:::

::: check
given: s = 8 | 48; N = 1024 | 4096; M = 8 | 16
answer: s*1000/(M*N)
unit: Hz
tolerance: 0.5%

## An {N}-point record at {s} kHz is zero-padded by a factor of {M}. What's the spacing of the resulting spectrum's samples?

::: right
Right. $f_s/(MN)$ = {answer} Hz, though the resolution is still set by the original record.
:::

::: misses
- [s*1000/N] Zero padding does shrink the sample spacing; it doesn't improve resolution.
- [any] Not yet. Bin spacing is $f_s$ over the padded length.
:::
:::

::: check
given: d = 1 | 5 | 20; s = 8 | 48
answer: 2*s*1000/d
tolerance: 0.5%

## Two tones {d} Hz apart must be resolved with a Hann window at {s} kHz. How many samples must the record have?

::: right
Right. $2f_s/N \le \Delta f$ gives N ≥ {answer}.
:::

::: misses
- [s*1000/d] That's enough for a rectangular window; Hann needs twice the length.
- [any] Not yet. Resolution $2f_s/N$ must be at most Δf.
:::
:::

::: check
kind: parts
given: N = 1024 | 4096 | 65536; E = 1.36 | 1.5 | 1.73; S = -30 | -10

## A tone is {S} dB below white noise in the time domain and analyzed by an {N}-point FFT with a window of ENBW {E} bins.

::: part
answer: 10*log(N/E)
unit: dB
tolerance: 0.5%

### What processing gain does the FFT give?

::: misses
- [10*log(N)] The window's noise bandwidth costs $10\log_{10}\text{ENBW}$.
- [any] $10\log_{10}(N/\text{ENBW})$.
:::
:::

::: part
answer: S + 10*log(N/E)
unit: dB
tolerance: 0.5%

### What SNR does the tone have in its bin?

::: misses
- [any] Time-domain SNR plus processing gain.
:::
:::
:::

::: check
kind: derivation
vars: N = 64..65536, E = 1..2, c = 0.3..1, X = 1..100000, s = 1000..200000

## An N-point FFT at sample rate s with a window of ENBW E bins and coherent gain c shows a tone on a bin with |X| = X.

::: step
answer: 2*X/(N*c)

### What is the tone's amplitude?
:::

::: step
answer: E*s/N

### What noise bandwidth does each bin have, in hertz?
:::

::: step
answer: 10*log(N/E)

### What processing gain does it give?
:::

::: right
Right. Coherent gain corrects the tone's height; ENBW corrects the noise's.
:::
:::

::: explain
## Why can a strong tone hide a weak one many bins away when the rectangular window is used?

::: model
Unless a tone falls exactly on a bin, its windowed spectrum spreads into every bin according to the window's sidelobes, and the rectangular window's sidelobes are high, the first only 13 dB down, and fall off slowly, about 6 dB per octave of distance. A tone 60 dB weaker sitting ten bins away is therefore buried under the strong tone's leakage, which at that distance is still only about 30 to 40 dB down. A tapered window such as Hann or Blackman has sidelobes that start lower and fall much faster, so the leakage at that distance drops below the weak tone and reveals it, at the price of a wider main lobe that blurs tones that are very close together.
:::
:::

::: practice

::: check
given: d = 0.25 | 0.45; N = 128 | 512
answer: 20*log(sin(pi*d)/(N*sin(pi*d/N)))
unit: dB
tolerance: 0.02

## Rectangular scalloping at offset {d}, N = {N}?

::: misses
- [any] $20\log\frac{\sin\pi d}{N\sin(\pi d/N)}$.
:::
:::

::: check
given: X = 3000 | 40000; N = 512 | 4096; c = 0.5 | 0.42
answer: 2*X/(N*c)
tolerance: 0.5%

## Amplitude from |X| = {X}, N = {N}, CG {c}?

::: misses
- [any] $2|X|/(N\cdot\text{CG})$.
:::
:::

::: check
given: N = 512 | 8192; E = 1.36 | 1.5
answer: 10*log(N/E)
unit: dB
tolerance: 0.05

## Processing gain: N {N}, ENBW {E}?

::: misses
- [any] $10\log(N/\text{ENBW})$.
:::
:::
:::
