Spectrogram

Four short pieces made of pure tones, their spectrogram, and one slice through it. Change the window length and watch close notes separate while time smears, or the reverse. The dashed bars are the score: where the notes really are.

Loading interactive simulation...

Lesson

The theory — Spectrogram

A spectrogram shows how a signal's spectrum changes in time. The signal is cut into overlapping slices, each multiplied by a window T long and Fourier-transformed (the short-time Fourier transform), and the magnitudes are laid side by side. A window T long blurs time by about T and pitch by about 1/T, so no one length shows close pitches and quick changes at once. Two pitches a semitone apart differ by Δf = f(21/12 − 1), and they beat once every 1/Δf.

Assumes
Pure sine tones of equal loudness. Where a real note has harmonics, the n-th harmonics of two notes a semitone apart are n times further apart than the notes themselves, so a spectrogram separates those harmonics with shorter windows than the fundamentals need; how much of a bass sound sits in its harmonics depends on the instrument.

Two notes a semitone apart need a window longer than one beat 🖖

Press One beat, rectangular. A4 and A♯4 are 26.16 Hz apart, so together they beat 26.16 times a second, once every 38.2 ms, and a window that long is the usual rule of thumb: frequency resolution is one over the window's length. The page counts the slices, and only 61.9% show two peaks. The ones that fail are the moments when the two tones are in step and add up midway between their pitches into a single hump; a beat later they are in step again. Every slice splits only once the window holds 1.33 beats, 50.6 ms, for a rectangular window, or 1.88 beats, 72.0 ms, for Hann, whose tapered ends make it listen for less time than its length. Press Long enough: at 80 ms every slice shows both notes. Julius Smith's rule in Spectral Audio Signal Processing, two beats for rectangular and four for Hann, is safer than this test needs, because it asks that the two main lobes not overlap at all.

A bass line needs a slower clock than a melody 🖖

A semitone is a fixed ratio, 21/12, so its width in hertz shrinks with the pitch: 52.33 Hz at A5, 3.27 Hz at A1, four octaves down. The window has to grow to match, and the table under the results gives the ladder: a Hann window needs 36.0 ms to split a semitone at A5 and 576.2 ms at A1, more than four and a half sixteenth notes at 120 beats a minute. Press Melody trill: A5 and A♯5 alternating in sixteenths, and a 36 ms window shows which note is playing in 95.0% of slices. Press Bass trill, the same trill four octaves down, through a window as long as one note, 125 ms: 1.2%. Now drag the window. Short, it is too blunt to tell 55 Hz from 58.27 Hz; long enough to tell them, it straddles four or five notes and hears both at once. No window from 10 ms to a second shows which bass note is playing in a quarter of the slices. Analysers built for music answer this by giving every pitch its own window: Judith Brown's constant-Q transform of 1991 makes the window's length go inversely with the frequency, long for the bass and short for the treble.

A sliding note has a best window, and a longer one blurs it 🖖

Press A glide: one tone sliding from 440 Hz to 880 Hz in two seconds, 220 Hz every second. Through a 140 ms Hann window its line is 18.2 Hz wide at the middle. Halve the window to 70 ms and the line widens to 29.0 Hz, as a shorter window should. Double it to 280 ms and it widens too, to 30.8 Hz. A longer window has a narrower main lobe, but the pitch moves while it listens: in 280 ms the glide covers 61.6 Hz, and the window smears that whole stretch into one line. The two blurs pull opposite ways, and for this glide the line is narrowest near 140 ms. A held chord has no such best length: past 72.0 ms every Hann slice splits, and a longer window only narrows the lobes. Underneath both is a floor on how small a window's spread in time times its spread in pitch can be, which is what the page on wave packets is about.

Problem solved in full

  1. The piano's lowest note 4 steps

    The lowest A on a piano is A0, 27.5 Hz. How long a Hann window does a spectrogram need to show A0 and A♯0 as two peaks in every slice, and how does that compare with a sixteenth note at 120 beats a minute? What window does a constant-Q analyser like Judith Brown's use there? Press Bass trill first: the page gives the figures for A1, an octave higher.

    Open this problem in the tool

    1. The semitone above A0:

    2. A Hann window needs 1.8845 beats. An octave below A1 the gap halves, so the window doubles the page's 576.2 ms:

    3. In sixteenth notes at 120 beats a minute, 125 ms each:

    4. Brown's constant-Q windows hold Q = 34 cycles of the pitch they analyse (Hamming-shaped, for quarter-tone resolution), so their length is Q/f:

    Answer

    About 1.15 s, more than nine sixteenth notes. At that length one window spans more than nine notes of a fast passage in the piano's bottom octave. Brown's window at A0, 1.24 s, is longer still; at A4 his 77.3 ms lands close to the page's 72.0 ms for Hann, from a different criterion.

Learning path

The Fourier transform in practice

References (2)