---
title: Spectrograms
heading: Watching a spectrum change over time with the short-time Fourier transform, the window length that trades time resolution for frequency resolution, when two tones can be told apart, frames, hops and overlap, and reading wideband and narrowband spectrograms of speech
eyebrow: D5 · Lesson 8 · Spectrograms
xp: 40
---

::: hook
A single FFT of a whole song tells you which notes appear somewhere in it, but not when. A **spectrogram** slides a short window along the signal, takes an FFT of each slice, and stacks the spectra side by side as columns of color: time across, frequency up, loudness as brightness. Bird songs, speech, radar returns and engine vibrations all become pictures you can read. Every spectrogram involves one unavoidable choice, the window's length, because a short window sees quick changes but blurs frequency, and a long one sees fine frequency detail but blurs time.
:::

::: play
- The spectrum of two equal tones $\Delta$ hertz apart, seen through a Hann window of length T. Each tone appears as a main lobe about 2/T wide on each side.
- With a long window the lobes are narrow and two separate peaks show; shorten it and they merge into one blur.
- The readouts give the window's frequency resolution and time resolution. Their product never changes: that's the trade-off.
:::

```sim plot
view: graph
x: f = 900..1200
y: L = -50..8
slider: T = 5..200 step 1 = 50
slider: D = 5..100 step 1 = 40
plot: 20*log(abs(sinc((f - 1000)*T/1000)/(1 - ((f - 1000)*T/1000)^2 + 1e-12) + sinc((f - 1000 - D)*T/1000)/(1 - ((f - 1000 - D)*T/1000)^2 + 1e-12)) + 1e-9) | samples: 800 | label: two tones through a Hann window, dB (T in ms, D in Hz)
marker: x = 1000 | label: tone 1
marker: x = 1000 + D | label: tone 2
readout: frequency resolution 2/T, Hz = 2000/T | digits: 3
readout: time resolution, ms = T | digits: 3
```

::: predict
## Two tones are 20 Hz apart. About how long must a Hann window be for the spectrogram to show them as two peaks?

- [ ] 5 ms
- [ ] 20 ms
- [x] At least 100 ms
- [ ] 1 s

::: reveal
A Hann window of length T has a main lobe reaching 2/T on each side, and two equal tones show a dip between them only when they're more than about 2/T apart. So T must exceed 2/20 Hz = 0.1 s. Any changes faster than 100 ms are then smeared together in time.
:::
:::

::: nameit
## The short-time Fourier transform

Multiply the signal by a window of length T starting at time $t_m$, take the FFT, move the window by a **hop** H, repeat. Each FFT is a **frame**, a column of the spectrogram; there are 1/H frames per second. Hops shorter than T (overlap, typically 50 to 75%) give a smoother picture.

## The trade-off

A window of length T gives bins 1/T apart, and its main lobe sets how close two tones can be and still appear separately:

- rectangular window: tones more than about 1.36/T apart;
- Hann window: more than about 2/T apart.

But anything that changes within T is averaged together. Time resolution times frequency resolution is fixed, $\Delta t\,\Delta f \approx$ constant: no window beats it, only chooses where to sit on it.

## Speech

- A **wideband** spectrogram (window about 3 to 5 ms, resolution a few hundred hertz) shows each glottal pulse as a vertical stripe and the **formants**, the vocal tract's resonances, as dark bands.
- A **narrowband** spectrogram (window 20 to 40 ms, resolution tens of hertz) shows the individual harmonics of the voice's pitch as horizontal lines.

Speech recognizers typically use 25 ms frames every 10 ms.
:::

::: example
## A speech recognizer at 16 kHz uses 25 ms frames every 10 ms. How many samples per frame, how far apart are the bins, and how many frames per second?

::: solution
1. **Samples per frame.**
   $$0.025\ \text{s} \times 16\,000\ \text{Hz} = 400$$
   usually zero-padded to a 512-point FFT.
2. **Bin spacing.** Before padding:
   $$\frac{1}{0.025\ \text{s}} = 40\ \text{Hz}$$
3. **Frame rate.**
   $$\frac{1}{0.01\ \text{s}} = 100\ \text{frames/s}$$
4. **Overlap.** Each 25 ms frame shares 15 ms with the next, an overlap of 60%.

**Answer:** 400 samples per frame, bins 40 Hz apart, and 100 frames per second, overlapping by 60%.
:::
:::

::: example
## With a 50 ms Hann window, are tones 40 Hz apart resolved? With a rectangular window, how close could they be?

::: solution
1. **Hann window.** It resolves tones about 2/T apart:
   $$\frac{2}{0.05\ \text{s}} = 40\ \text{Hz}$$
   so 40 Hz is exactly the limit: the two lobes merge into a flat top with no dip.
2. **Rectangular window.** One of the same length separates tones down to about 1.36/T apart, at the cost of much higher leakage:
   $$\frac{1.36}{0.05\ \text{s}} = 27\ \text{Hz}$$

**Answer:** 40 Hz is exactly the Hann window's limit, so the tones merge; a rectangular window separates tones about 27 Hz apart.
:::
:::

::: example
## A chirp sweeps at 100 kHz per second. What window length keeps its spectrogram trace sharpest?

::: solution
1. **Two kinds of smear.** In a window T the chirp moves $kT$ Hz, while the bins are $1/T$ wide.
2. **Match them.** The smear is least when the two are equal:
   $$\begin{aligned} kT &= \frac{1}{T} \\ T &= \frac{1}{\sqrt{k}} \\ &= \frac{1}{\sqrt{10^5}}\ \text{s} \\ &= 3.2\ \text{ms} \end{aligned}$$
3. **Either side.** Longer windows blur in frequency, shorter ones in time.

**Answer:** a window of about 3.2 ms keeps the trace sharpest.
:::
:::

::: check
given: T = 10 | 25 | 64 | 200
answer: 1000/T
unit: Hz
tolerance: 0.5%

## How far apart are the bins of a {T} ms analysis window (without zero padding)?

::: right
Right. 1/T = {answer} Hz.
:::

::: misses
- [any] Not yet. The reciprocal of the window length.
:::
:::

::: check
given: D = 5 | 20 | 50 | 120
answer: 2000/D
unit: ms
tolerance: 0.5%

## Two tones are {D} Hz apart. What's the shortest Hann window that shows them as separate peaks?

::: right
Right. 2/Δf = {answer} ms.
:::

::: misses
- [1000/D] A Hann window's main lobe reaches 2/T each side: double it.
- [any] Not yet. Separation must exceed 2/T.
:::
:::

::: check
given: T = 20 | 25 | 32; s = 8 | 16 | 44.1
answer: T*s
tolerance: 0.5%

## How many samples does a {T} ms frame hold at {s} kHz?

::: right
Right. $Tf_s$ = {answer} samples.
:::

::: misses
- [any] Not yet. Duration times rate.
:::
:::

::: check
given: H = 5 | 10 | 12.5 | 20
answer: 1000/H
tolerance: 0.5%

## A spectrogram hops {H} ms between frames. How many frames does it compute per second?

::: right
Right. 1/H = {answer} frames per second.
:::

::: misses
- [any] Not yet. One frame per hop.
:::
:::

::: check
kind: parts
given: T = 10 | 25 | 50; fs = 8 | 16 | 44.1; h = 5 | 10

## A spectrogram uses {T} ms frames every {h} ms at {fs} kHz.

::: part
answer: T*fs
unit: samples
tolerance: 0.5%

### How many samples per frame?

::: misses
- [any] Duration × rate.
:::
:::

::: part
answer: 1000/T
unit: Hz
tolerance: 0.5%

### How far apart are its bins?

::: misses
- [any] $1/T$.
:::
:::
:::

::: check
kind: parts
given: k = 10 | 100 | 1000

## A chirp sweeps at {k} kHz per second.

::: part
answer: 1/sqrt(k*1000)*1000
unit: ms
tolerance: 0.5%

### What window length keeps its trace sharpest?

::: misses
- [any] $T = 1/\sqrt k$.
:::
:::

::: part
answer: sqrt(k*1000)
unit: Hz
tolerance: 0.5%

### How wide are the bins then?

::: misses
- [any] $1/T$.
:::
:::
:::

::: explain
## Why can't a spectrogram have both sharp time resolution and sharp frequency resolution?

::: model
Each column of a spectrogram is the spectrum of one windowed slice of the signal, and a slice of length T can only distinguish frequencies whose difference makes a noticeably different number of cycles within it, about one over T. A long window lets small frequency differences add up to a visible difference, giving fine frequency resolution, but it averages together everything that happens during the window, so quick changes are blurred. A short window localizes events in time but can't tell close frequencies apart. This is the same reciprocal relationship between duration and bandwidth that appears throughout Fourier analysis, and no choice of window escapes it.
:::
:::

::: practice

::: check
given: D = 1 | 2 | 5 | 10 | 50 | 100 | 500
answer: 2000/D
unit: ms
tolerance: 0.5%

## Shortest Hann window for tones {D} Hz apart?

::: misses
- [any] 2/Δf.
:::
:::

::: check
given: T = 1 | 2 | 10 | 20 | 50 | 100 | 1000
answer: 1000/T
unit: Hz
tolerance: 0.5%

## Bin spacing of a {T} ms window?

::: misses
- [any] 1/T.
:::
:::
:::
