---
title: The short-time Fourier transform
heading: Spectra that change with time, window length against time and frequency resolution, hop size and overlap, spectrograms, and perfect reconstruction by overlap-add
eyebrow: S7 · Lesson 30 · The short-time Fourier transform
xp: 50
---

::: hook
Speech, music, radar returns and machine vibration all have spectra that change from moment to moment, and a single FFT of the whole recording averages that change away. The short-time Fourier transform slides a window along the signal and takes an FFT of each windowed piece, giving a spectrum for every moment: a spectrogram. The window length decides what can be seen. A long window resolves close frequencies but blurs events in time; a short one catches fast changes but smears frequencies. Choosing it well is a quantitative trade, and for some signals there's an exact best length.
:::

::: play
- The frequency spread of a linear chirp sweeping R Hz per second, seen through a Hann window of length T: the sweep during the window, RT (dotted), plus the window's own resolution, 2/T (dashed), against T.
- Short windows are limited by their resolution and long ones by the chirp's motion; the total (solid) is smallest at $T = \sqrt{2/R}$.
- For a chirp sweeping 1 kHz per second that's a 44.7 ms window, with a spread of 89 Hz. Faster chirps need shorter windows and blur more.
:::
:::

```sim plot
view: graph
x: T = 1..300 ms log
y: S = 1..2000 Hz log
slider: R = 100..100000 = 1000 log
plot: R*T/1000 + 2000/T | samples: 400 | label: total spread
plot: 2000/T | samples: 200 | dashed | color: ch3 | label: window resolution 2/T
plot: R*T/1000 | samples: 200 | dotted | color: ch2 | label: sweep during the window
readout: best window, ms = 1000*sqrt(2/R)
readout: smallest spread, Hz = 2*sqrt(2*R)
```

::: predict
## A speech analyzer at 16 kHz uses 25 ms Hann windows every 10 ms. How finely can it resolve frequencies, and how many spectra does it produce per second?

- [ ] 40 Hz, 40 per second
- [x] About 80 Hz, 100 per second
- [ ] 16 Hz, 25 per second
- [ ] 625 Hz, 100 per second

::: reveal
A 25 ms Hann window resolves about 2/T = 80 Hz, enough to separate the harmonics of most voices (100 to 250 Hz apart) yet short enough that a vowel's spectrum barely changes within it. Hopping 10 ms gives 100 frames per second. The window is 400 samples, usually zero-padded to a 512-point FFT.
:::
:::

::: nameit
## Definition

$$X[n, k] = \sum_{m=0}^{L-1}x[nR + m]\,w[m]\,e^{-j2\pi km/N}$$

a window of L samples, hopped by R samples, each frame transformed by an N-point DFT (N ≥ L). Its squared magnitude, plotted against time and frequency, is the **spectrogram**.

## Resolution

- **Frequency**: about $f_s/L$ for a rectangular window, 2f_s/L for Hann and Hamming (lesson 29).
- **Time**: about the window length, $T = L/f_s$.
- Their product can't be made small: Δt·Δf is at least of order 1, the time–bandwidth (uncertainty) principle. A **wideband** spectrogram (short window) shows pitch pulses in time; a **narrowband** one (long window) shows individual harmonics.
- For a chirp sweeping R Hz/s, the spread is about $RT + 2/T$ with a Hann window, smallest at $T = \sqrt{2/R}$, where it's $2\sqrt{2R}$.

## Hop size and reconstruction

- The **hop** R sets the frame rate $f_s/R$. With a real input, the STFT produces about N/(2R) complex values per input sample, so it's redundant unless R = N.
- **Overlap-add synthesis**: inverse-FFT each frame, then add the frames at their positions. The original returns exactly if the shifted windows sum to a constant: $\sum_nw[m - nR] = $ const (the **COLA** condition). The periodic Hann window at 50% overlap sums to exactly 1; Hamming at 50% sums to 1.08.
- Modifying the frames before synthesis gives time-varying filtering, noise reduction and time-scale changes.
:::

::: example
## A chirp sweeps 1 kHz per second. What window length gives the sharpest spectrogram, and how sharp is it?

::: solution
1. **Best window length.**
   $$\begin{aligned} T &= \sqrt{2/1000}\ \text{s} \\ &= 44.7\ \text{ms} \end{aligned}$$
2. **The window's resolution.**
   $$\begin{aligned} \Delta f &= \frac{2}{0.0447}\ \text{Hz} \\ &= 44.7\ \text{Hz} \end{aligned}$$
3. **The chirp's motion.** During the window the chirp moves
   $$\begin{aligned} \Delta f &= 1000 \times 0.0447\ \text{Hz} \\ &= 44.7\ \text{Hz} \end{aligned}$$
   The two contributions are equal at the optimum.
4. **Total spread.**
   $$\begin{aligned} \Delta f &= 44.7 + 44.7\ \text{Hz} \\ &= 89.4\ \text{Hz} \end{aligned}$$
5. **Away from the optimum.** Halving or doubling T raises it to 112 Hz.

**Answer:** a 44.7 ms window, which smears the chirp over 89.4 Hz.
:::
:::

::: example
## Show that the periodic Hann window satisfies the COLA condition at 50% overlap for N = 8.

::: solution
1. **The window.**
   $$w[m] = 0.5 - 0.5\cos(2\pi m/8)$$
2. **Add a frame 4 samples on.**
   $$\begin{aligned} &w[m] + w[m + 4] \\ &= 1 - 0.5[\cos(\pi m/4) \\ &\qquad + \cos(\pi m/4 + \pi)] \\ &= 1 \end{aligned}$$
   for every m, since the two cosines cancel.
3. **Conclusion.** Frames hopped by 4 therefore add back to exactly the original signal.

**Answer:** $w[m] + w[m + 4] = 1$ for every m, so the periodic Hann window satisfies COLA at 50% overlap.
:::
:::

::: example
## Speech sampled at 16 kHz has a pitch of 120 Hz. Compare 5 ms and 40 ms Hann windows.

::: solution
1. **The 5 ms window.** That's 80 samples, with about
   $$\begin{aligned} \Delta f &= \frac{2f_s}{L} \\ &= 400\ \text{Hz} \end{aligned}$$
   of frequency resolution and 5 ms of time resolution.
2. **What it shows.** The harmonics, 120 Hz apart, blur together, but each glottal pulse (every 8.3 ms) shows as a vertical stripe.
3. **The 40 ms window.** That's 640 samples, with 50 Hz resolution: the harmonics separate into horizontal lines, but the pulses merge.
4. **In practice.** Speech analysis uses both: wideband for timing, narrowband for pitch.

**Answer:** the 5 ms window (400 Hz resolution) shows each pitch pulse but blurs the harmonics; the 40 ms window (50 Hz) resolves the harmonics but merges the pulses.
:::
:::

::: check
given: T = 10 | 25 | 64; s = 8 | 16 | 48
answer: T*s
tolerance: 0.5%

## A {T} ms window at {s} kHz holds how many samples?

::: right
Right. $T f_s$ = {answer}.
:::

::: misses
- [T*s*1000] T is in milliseconds and $f_s$ in kilohertz.
- [any] Not yet. Duration times sample rate.
:::
:::

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

## Roughly what frequency resolution does a {T} ms Hann window give?

::: right
Right. About 2/T = {answer} Hz.
:::

::: misses
- [1000/T] That's a rectangular window's resolution; Hann's main lobe is twice as wide.
- [any] Not yet. Hann resolves about two bins: 2/T.
:::
:::

::: check
given: s = 8 | 16 | 44.1; R = 64 | 160 | 441
answer: s*1000/R
tolerance: 0.5%

## An STFT at {s} kHz hops {R} samples per frame. How many frames per second?

::: right
Right. $f_s/R$ = {answer}.
:::

::: misses
- [R/s] Divide the sample rate by the hop.
- [any] Not yet. $f_s/R$.
:::
:::

::: check
given: R = 200 | 5000 | 50000
answer: 1000*sqrt(2/R)
unit: ms
tolerance: 0.5%

## A chirp sweeps {R} Hz per second. What Hann window length shows it most sharply?

::: right
Right. $\sqrt{2/R}$ = {answer} ms.
:::

::: misses
- [1000*sqrt(1/R)] With a Hann window the resolution term is 2/T, so $T = \sqrt{2/R}$.
- [any] Not yet. Minimize $RT + 2/T$.
:::
:::

::: check
given: R = 2000 | 5000 | 50000; T = 5 | 20 | 50
answer: R*T/1000 + 2000/T
unit: Hz
tolerance: 0.5%

## A chirp at {R} Hz/s is analyzed with a {T} ms Hann window. Roughly how wide does it appear?

::: right
Right. $RT + 2/T$ = {answer} Hz.
:::

::: misses
- [2000/T] Add the chirp's sweep during the window.
- [R*T/1000] Add the window's own resolution.
- [any] Not yet. Sweep plus resolution.
:::
:::

::: check
given: N = 512 | 1024 | 2048; R = 128 | 256 | 512
answer: N/(2*R)
tolerance: 0.5%

## A real signal's STFT uses {N}-point FFTs with a hop of {R}. How many unique complex values does it produce per input sample?

::: right
Right. About N/2 unique bins every R samples: {answer}.
:::

::: misses
- [N/R] For a real input, half the bins are conjugates of the other half.
- [any] Not yet. Unique bins per frame over the hop.
:::
:::

::: check
kind: parts
given: T = 5 | 20 | 40; s = 8 | 16 | 44.1; p = 100 | 200

## A {T} ms Hann window analyzes {s} kHz audio whose harmonics are {p} Hz apart.

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

### How many samples does the window hold?

::: misses
- [any] Duration times sample rate.
:::
:::

::: part
answer: 2/(T*1e-3)
unit: Hz
tolerance: 0.5%

### Roughly what frequency resolution does it give?

::: misses
- [1/(T*1e-3)] That's a rectangular window's; Hann's main lobe is twice as wide.
- [any] About $2/T$.
:::
:::
:::

::: check
kind: derivation
vars: R = 10..100000, T = 0.001..1

## A chirp sweeps R Hz per second through a Hann window T seconds long.

::: step
answer: R*T + 2/T

### Roughly how wide does it appear?
:::

::: step
answer: sqrt(2/R)

### What window length minimizes that?
:::

::: step
answer: 2*sqrt(2*R)

### What is the minimum width?
:::

::: right
Right. The sweep during the window and the window's own resolution trade off, balancing at $T = \sqrt{2/R}$.
:::
:::

::: explain
## Why can't a spectrogram be sharp in both time and frequency at once?

::: model
Each column of a spectrogram is the spectrum of a windowed stretch of signal, and a window of length T can only separate frequencies differing by about 1/T, since two tones closer than that don't complete enough relative cycles within the window to look different. Making T short, to localize events in time, therefore widens every frequency peak; making T long, to sharpen the peaks, averages over everything that happened during the window. This is the same time–bandwidth trade that governs all Fourier analysis: a signal concentrated in a short time necessarily has a wide spectrum. The choice of window length is a decision about which kind of detail matters more for the signal at hand.
:::
:::

::: practice

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

## Samples in a {T} ms window at {s} kHz?

::: misses
- [any] $Tf_s$.
:::
:::

::: check
given: R = 100 | 500 | 1000 | 2000 | 5000 | 20000
answer: 1000*sqrt(2/R)
unit: ms
tolerance: 0.5%

## Best Hann window for a {R} Hz/s chirp?

::: misses
- [any] $\sqrt{2/R}$.
:::
:::

::: check
given: s = 8 | 48; R = 80 | 480
answer: s*1000/R
tolerance: 0.5%

## Frames per second: {s} kHz, hop {R}?

::: misses
- [any] $f_s/R$.
:::
:::
:::
