C926 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C9 · Lesson 26 · The short-time Fourier transform

Spectra that change with time, and the trade between seeing when and seeing which frequency

A DFT of a whole recording (lesson 24) says which frequencies occur but not when: a bird's rising chirp and a falling one have the same spectrum. Speech, music, radar returns and machine vibration all change from moment to moment, so engineers cut the signal into short windowed pieces (lesson 25), take a DFT of each, and stack the results into a spectrogram: time across, frequency up, brightness for magnitude. Choosing the piece length is the whole art. Short windows locate events sharply in time and blur frequency; long windows do the opposite, the duration-bandwidth limit of lesson 17 in a new form.

  • For a window of L samples at sample rate fs = 8 kHz, the solid curve is the frequency resolution (Hann main-lobe half-width, 2fs/L) and the dashed curve the time resolution, L/fs, both against window length.
  • They move in opposite directions; their product is fixed at 2.
  • A chirp sweeping at β Hz per second smears across βT hertz during a window of length T, so for a chirp there's a best window: where the smear equals the window's own resolution.

Predict

Speech sampled at 8 kHz is analyzed with 256-sample windows hopped by 128 samples. How many spectrogram frames does one second give?

Name it

The short-time Fourier transform

Multiply the signal by a window w of length L placed at hop m, and take the DFT:

X[m,k]=∑n=0L−1x[n+mH]w[n]e−j2πkn/L

with hop H. Frame m describes the signal around time mH/fs; bin k is frequency kfs/L.

Resolution

  • Time: an event is located to within about the window's length, L/fs.
  • Frequency: bins are fs/L apart, and a window's main lobe spreads a pure tone over several bins: a Hann window's main lobe reaches ±2fs/L. Two tones are cleanly separated when they're about that far apart.

Their product is constant: halving the window doubles the frequency blur. Zero-padding each frame makes the plot smoother but doesn't change the resolution.

Hop and overlap

Windows usually overlap by 50 to 75%, so nothing between frames is missed and the display is smooth. The number of frames from N samples is ⌊(N−L)/H⌋+1. With a Hann window at 50% overlap, the shifted windows add up to a constant, so the frames can be modified and added back together to rebuild the signal: the basis of overlap-add processing.

The best window for a chirp

A tone whose frequency rises at β Hz/s moves by βT during a window of length T, while the window's own resolution is about 1/T. The total blur is smallest when the two are equal, at T≈1/β.

Worked example

Design a spectrogram for 8 kHz speech with 32 ms frames and 50% overlap. Give L, H, the bin spacing, the Hann main-lobe half-width, and the frames per second.

Show the solution
  1. Frame and hop. L=0.032×8000=256samplesH=128
  2. Bins and main lobe. The bins are 8000/256=31.25Hz apart, and a Hann window spreads each tone over ±62.5 Hz.
  3. Frames per second. One second gives ⌊(8000−256)/128⌋+1=61 frames, a new frame every 16 ms.
  4. What it resolves. A voice's harmonics, 100 to 250 Hz apart, are resolved; a 10 ms plosive is located to within one frame.

Answer L = 256 and H = 128, bins 31.25 Hz apart, a Hann main-lobe half-width of 62.5 Hz, and 61 frames a second.

Worked example

A vibration signal sampled at 8 kHz contains two tones 20 Hz apart. How long a Hann window is needed to separate them, and what time resolution does that leave?

Show the solution
  1. Window length. The main-lobe half-width 2fs/L must be at most 20 Hz, so L≥2×8000/20=800samples which is 100 ms.
  2. Time resolution. Events shorter than a tenth of a second will be smeared.
  3. When both matter. If both separations matter, one display can't show them; analysts make two spectrograms, or use a wavelet transform whose window shrinks at high frequencies.

Answer at least 800 samples (100 ms), which smears events shorter than a tenth of a second.

Worked example

A bat's chirp sweeps 1000 Hz per millisecond, 106 Hz/s. What window length shows it most sharply?

Show the solution
  1. Best window length. T≈1/β=1/106s=1ms
  2. Why it balances. During 1 ms the chirp moves 1 kHz, and a 1 ms window's own resolution is about 1 kHz: the two blurs balance.
  3. Longer or shorter. A 10 ms window would smear the sweep over 10 kHz; a 0.1 ms window would blur each moment's frequency over 10 kHz. Either way it's ten times worse.

Answer a window about 1 ms long.

Check

A spectrogram of a 16 kHz signal uses 256-sample frames. How far apart are its frequency bins?

Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.

Check

A record of 16000 samples is analyzed with 256-sample frames hopped by 256/4. How many complete frames fit?

Check

Tones 5 Hz apart, sampled at 16 kHz, must be separated with a Hann window. What's the shortest window, in samples, whose main-lobe half-width 2fs/L is at most 5 Hz?

Check

A chirp sweeps 1000 Hz per second. About what window length shows it most sharply?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

How long is a 512-sample frame at 8 kHz?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A spectrogram's frames are zero-padded from 256 to 4096 points before the DFT. What changes?

Check

Track a chirp with a spectrogram. At fs = 8000, the signal is x = cos(2*pi*(500*t + b*t**2/2)) for one second (t = n/fs), so its frequency is 500 + b*t Hz. With Hann frames of L = 64 samples, hop L//2, find each frame's peak bin, convert it to Hz with k*fs/L, and compare with the true frequency at the frame's center. Set rms_err (the RMS tracking error in Hz over all frames) and frames.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

A technician uses a spectrogram to find an intermittent 2 ms glitch on a motor drive's current, and also to tell apart two vibration tones 3 Hz apart. Why can't one window setting do both, and what should they do?

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.