Skip to content

5.3 — Spectral Analysis in Practice

Take an FFT of a real signal and you get a jagged, noisy plot that changes completely every time you take another one. That is not a fault in the measurement — a single FFT of a noisy signal is a terrible estimate of its spectrum, and understanding why leads to every technique in this chapter.

This chapter is about a problem Chapter 5.1's DFT quietly created. The DFT assumes the block of samples you hand it repeats forever, and a real recording does not, so the spectrum it returns contains features the signal never had.

1. Why one FFT is not enough

Feed pure white noise into an FFT. Each bin's output is a complex number formed from a weighted sum of many independent random samples, so its real and imaginary parts are each Gaussian. The magnitude squared is therefore the sum of two squared Gaussians, which follows a chi-squared distribution with two degrees of freedom.

The standard deviation of that distribution equals its mean. So the estimate of power in each bin has a 100% standard deviation — the value you get is as likely to be double the truth as half of it.

And it does not improve with more samples. Taking a 65,536-point FFT instead of a 1024-point one gives you more bins, each still with 100% variance. Longer records buy resolution, not reliability.

The plot looks jagged because it genuinely is random, and averaging is the only cure.

2. Welch's method

Break the record into overlapping segments, transform each, and average the magnitudes.

  1. Divide the data into K segments of length L, overlapping by 50%.
  2. Window each segment (Chapter 5.1).
  3. Take the FFT of each and compute |X_k[f]|^2.
  4. Average the K power spectra.

P[f]=\frac{1}{K}\sum_{k=1}^{K}|X_k[f]|^2

Averaging K independent estimates reduces the standard deviation by \sqrt K. Sixteen segments cuts the variance by a factor of four in standard deviation, and the plot becomes readable.

The trade, stated exactly. A fixed record of N samples can be one segment of length N or K segments of length N/K.

  • One long segment: resolution f_s/N, variance 100%.
  • K short segments: resolution Kf_s/NK times worse — variance 100/\sqrt K percent.

You are trading frequency resolution for statistical confidence, and there is no way to have both from a fixed record. More data is the only escape.

Why 50% overlap. Windowing tapers the ends of each segment towards zero, so data near a segment boundary contributes almost nothing. Overlapping by half means every sample sits in the middle of some segment. With a Hann window, 50% overlap recovers nearly all the statistical benefit of independent segments while wasting no data. Going beyond 50% adds little, because the segments are then strongly correlated and their errors no longer average away.

3. Reading a spectrum plot honestly

Four things must be right before a number on a spectrum plot means anything.

The frequency axis. Bin k is at kf_s/N. Only bins 0 to N/2 are meaningful for a real signal.

The amplitude scaling. The raw FFT output is not in volts. For a sinusoid of amplitude A landing exactly on bin k, the FFT magnitude is AN/2 — so divide by N/2. And the window reduces the amplitude, so divide by the window's mean value as well. This is the single most common mistake in a first spectrum plot, and the symptom is a plot whose numbers are wrong by an unremarkable-looking factor.

Power spectral density versus power. For a discrete tone, the whole of its power lands in one bin, so you quote the power in that bin. For broadband noise, the power in a bin depends on how wide the bin is, so you must divide by the bin width and quote power per hertz — the power spectral density. Quoting noise in dBV without saying "per root hertz" is meaningless, because doubling the FFT length would halve the number.

The noise floor is not the instrument's. A longer FFT has narrower bins, so each bin collects less noise and the apparent floor drops by 3 dB per doubling of length. This is called processing gain, and it is why a tone can be visible below the broadband noise level: the tone's power stays in one bin while the noise spreads across all of them.

\text{processing gain}=10\log_{10}(N/2)\ \text{dB}

For a 4096-point FFT that is 33 dB, which is why a spectrum analyser can find a carrier buried well under the noise.

4. The spectrogram

A single spectrum tells you what frequencies are present over the whole record, with no indication of when. For speech, music or a machine that is speeding up, that is useless.

The short-time Fourier transform computes a spectrum of each short segment and stacks them side by side, giving a picture with time along one axis, frequency up the other, and intensity showing magnitude.

X[m,k]=\sum_{n}x[n]w[n-mH]e^{-j2\pi kn/N}

where H is the hop size, how far the window advances between frames.

timefrequencyvowelplosivefricativevowel, pitch risingloudwhat a spoken word looks like
A schematic spectrogram of speech. Vowels appear as steady horizontal bands at the vocal tract's resonances; a plosive consonant is a brief vertical burst across all frequencies; a fricative is a patch of high-frequency noise. Nearly all speech analysis works from this picture.

The window length forces a choice, and it is the same trade as always.

  • Short window (5 ms): good time resolution, so you can see exactly when a consonant starts. Poor frequency resolution — individual harmonics blur together. This is a wideband spectrogram, and it shows the vertical striations of individual glottal pulses.
  • Long window (50 ms): good frequency resolution, so individual harmonics separate cleanly. Poor time resolution — a rapid consonant smears. This is a narrowband spectrogram.

Speech researchers routinely look at both, because each answers a question the other cannot.

The theoretical limit is exact:

\Delta t\cdot\Delta f \ge \frac{1}{4\pi}

This is the same inequality as Heisenberg's uncertainty principle, with time and frequency in place of position and momentum. It is not a limitation of the instrument or the algorithm; it is a property of what a signal is. The Gaussian window achieves the bound exactly and nothing does better.

5. Wavelets — a different compromise

The STFT uses one window length for the whole analysis, so the time-frequency trade is fixed everywhere. But real signals do not need it fixed. A bass note lasts a long time and needs fine frequency resolution; a cymbal crash is brief and needs fine time resolution.

Wavelet analysis uses a window that scales with frequency: short windows for high frequencies, long ones for low. The analysing function is a small wave — a wavelet — that is stretched or compressed rather than a fixed sinusoid windowed to a fixed length.

W(a,b)=\frac{1}{\sqrt a}\int x(t)\psi^*\!\left(\frac{t-b}{a}\right)dt

where a is the scale (inversely related to frequency) and b the position in time.

The result is constant relative resolution — each octave gets the same number of coefficients — which happens to match how hearing works, how images are structured, and how most natural signals behave.

Where it is used in practice: JPEG 2000 (which replaced the DCT with a wavelet transform and eliminated the block artifacts of ordinary JPEG), fingerprint compression by the FBI, denoising by discarding small wavelet coefficients, and transient detection where an event's exact time matters.

Where it is not used: anywhere the signal genuinely is a sum of steady sinusoids, which is most of communications engineering. Wavelets are better for transients and worse for tones, and choosing between them is choosing what kind of signal you have.

6. Finding a frequency precisely

The FFT gives frequencies on a grid f_s/N apart. A tone between bins produces a peak whose true position must be estimated.

Parabolic interpolation fits a parabola through the peak bin and its two neighbours, using the values in decibels:

\delta=\frac{1}{2}\cdot\frac{\alpha-\gamma}{\alpha-2\beta+\gamma}

where \alpha, \beta, \gamma are the levels of the bin before, at, and after the peak, and \delta is the offset in bins.

True frequency: f=(k+\delta)f_s/N.

Worked example. 1024-point FFT at 8 kHz, so bins are 7.8125 Hz apart. Peak at bin 128 with levels -12.5, -8.0, -14.0 dB.

\delta=\frac12\cdot\frac{-12.5-(-14.0)}{-12.5-2(-8.0)+(-14.0)}=\frac12\cdot\frac{1.5}{-10.5}=-0.0714

f=(128-0.0714)\times7.8125=999.4\ \text{Hz}

Against the bin centre of 1000.0 Hz. The interpolation improved the estimate from a 7.8 Hz grid to well under 1 Hz.

This works best with a window whose main lobe is roughly parabolic near the peak, which the Hann window is. With a rectangular window the peak shape is a sinc and the parabola fits badly, so the improvement is smaller.

For serious frequency measurement, zero-pad by 4× first so the peak is drawn with more points, then interpolate. Accuracy of a hundredth of a bin is achievable with a clean signal.

7. Cepstral analysis

A trick worth knowing because it separates two things that look inseparable.

The cepstrum is the inverse Fourier transform of the log magnitude spectrum:

c[n]=\mathcal{F}^{-1}\left\{\log|X[f]|\right\}

The name is "spectrum" with the first four letters reversed, and the joke continued: its independent variable is quefrency (measured in seconds), and filtering in that domain is liftering. Bogert, Healy and Tukey coined all of it in 1963, and it stuck.

Why it works. Many signals are a product of two parts — a source and a filter. Speech is a glottal pulse train shaped by the vocal tract. A struck string is an excitation shaped by the instrument body.

X(f)=E(f)\cdot H(f)

Taking the logarithm turns the product into a sum:

\log|X|=\log|E|+\log|H|

And the two parts vary at very different rates across the spectrum. The excitation's harmonics produce rapid ripple; the vocal tract's resonances produce slow undulation. So in the cepstral domain they separate: the excitation shows up as a peak at high quefrency, and the filter's shape occupies low quefrency.

What it gives you:

  • Pitch detection. The cepstral peak's position is the fundamental period directly. Robust even when the fundamental itself is missing from the spectrum.
  • Speech recognition features. The low-quefrency coefficients describe the vocal tract shape independently of pitch — which is exactly what you want, since the same word said at different pitches should give the same features. Mel-frequency cepstral coefficients (MFCCs) were the standard speech recognition feature for thirty years and are still a strong baseline. Volume I, Chapter 12.7 covers what replaced them.
  • Echo detection. An echo multiplies the spectrum by a periodic ripple, which appears as a clean cepstral peak at the echo delay.

8. Measuring a system's response

Beyond analysing a signal, you often want to characterise a system. Three methods, in ascending order of usefulness.

Swept sine. Play a slow frequency sweep, measure the response at each frequency. Excellent signal-to-noise ratio because all the energy is at one frequency at a time. Slow — a full audio sweep takes 30 seconds or more.

Impulse. Fire a single impulse and record the response, which is h[n] directly. Fast, and the signal-to-noise ratio is terrible, because the total energy is tiny. Firing a starting pistol in a hall to measure its acoustics is exactly this method, and the reason it works at all is that the pistol is very loud.

Maximum-length sequence. Play a pseudorandom binary sequence that is deterministic and known, then cross-correlate the recording with it. The correlation of an MLS with itself is almost exactly an impulse (Chapter 4.3), so the cross-correlation with the recorded response gives h[n] directly.

The advantage is decisive: the excitation is spread over the whole measurement time, so it can be continuous and moderate rather than briefly enormous — the same processing gain that Chapter 4.3 described for radar. You get impulse-response accuracy at swept-sine signal-to-noise, in a few seconds. Every acoustic measurement system built since the 1990s works this way.


Chapter 5.4 applies all of this to sound: what an MP3 actually throws away, how an equaliser works, and how noise-cancelling headphones subtract the world.

Every formula above, built from scratch

None of the results in this chapter are worth memorising, because each one can be rebuilt in under a minute from something simpler. What follows is that rebuilding, one result at a time, so the formula and the reason for it sit on the same page as the explanation that needed them.

Windows

WindowPeak sidelobeMain lobe width (bins)Coherent gain
Rectangular-13 dB21.00
Hann-31 dB40.50
Hamming-43 dB40.54
Blackman-58 dB60.42
Flat-top-93 dB100.22

w_{Hann}[n]=0.5\left(1-\cos\frac{2\pi n}{N-1}\right)

w_{Hamming}[n]=0.54-0.46\cos\frac{2\pi n}{N-1}

w_{Blackman}[n]=0.42-0.5\cos\frac{2\pi n}{N-1}+0.08\cos\frac{4\pi n}{N-1}

The trade is fixed: lower sidelobes always cost a wider main lobe. Choose by whether you need to find something small (low sidelobes) or separate two things close together (narrow main lobe).

What the next chapter fixes

The machinery is now complete. Chapter 5.4 applies it to the signal most people care about most — sound — covering what an equaliser, a compressor, a reverb and a lossy codec are actually doing to the numbers, and why an MP3 can throw away most of a file and still sound identical.