Spectral analysis in practice

Welch's method for stable spectra, spectrograms for changing ones, and what a trend does to a spectrum — on an earthquake record and twenty years of search interest.

What you'll learn
  • Explain why averaging windowed FFTs gives a cleaner power spectrum
  • Choose a window length for a spectrogram, knowing the time–frequency trade-off
  • Build a correct frequency axis and remove trends before looking for rhythms

The FFT of a whole long recording is noisy: every bin is estimated from a single look at the data. For noisy, stationary signals — EEG, vibration, network traffic — Welch’s method cuts the recording into overlapping windows, tapers each with a window, takes its power spectrum, and averages. welch_method.m writes it out before calling pwelch:

winlen = 1*srate;                                  % 1-second windows
nOverlap = round(winlen/2);                        % hop by half a window
winonsets = 1:nOverlap:length(eegdata)-winlen;
hannw = 0.5 - cos(2*pi*linspace(0,1,winlen))./2;  % Hann taper
for i = 1:length(winonsets)
    window = eegdata(winonsets(i):winonsets(i)+winlen-1) .* hannw;
    tempdata = abs(fft(window)/winlen).^2;
    eegPow = eegPow + tempdata(1:length(hzw));
end
eegpoW = eegPow / length(winonsets);

Averaging \(K\) windows cuts the variance of each bin by about a factor of \(K\); the price is frequency resolution, which drops to \(1/T_{\text{window}}\) — 1 Hz here. The Hann taper is the window of lesson 7 again, there to stop each segment’s abrupt ends from leaking power across the spectrum. (The EEG file the script loads is not in the repository; the same steps run unchanged on any signal.)

Spectrograms: spectra that change

When the frequency content changes over time, compute a spectrum per window and stack them side by side: a spectrogram. spectrogramm.m does it for a bird call; here is the same treatment, plus Welch’s method, for the earthquake recording Solomon_Time_History.txt that ships with the repository (50 samples per second, about 2.6 hours):

Left: a Welch power spectrum on a log scale, peaking near 0.06 hertz. Right: a spectrogram from 0 to 0.3 hertz over about 2.6 hours, with energy concentrated in a band near 0.06 hertz that strengthens about an hour after time zero.
Almost all of this record's power — 99.99% — lies below 0.2 Hz, with a peak near 0.06 Hz (periods of about 16 seconds). The spectrogram shows when: power in the 0.05–0.08 Hz band rises about a hundredfold between half an hour and an hour after time zero, peaks near one hour, and fades by two.

The window length sets the trade-off. Five-minute windows resolve \(1/300 \approx 0.003\) Hz, enough to see the 0.06 Hz band, but blur anything faster than five minutes in time; short windows do the opposite. Lesson 13 shows how wavelets pick a different trade-off at every frequency.

Rhythms in a trend: twenty years of searches

google_data.m looks for rhythms in the monthly Google Trends interest in “deep learning”, 2004–2023 (233 months, multiTimeline.csv):

data1 = data1 - mean(data1);
dataPow = abs(fft(data1)/n).^2;
hz = linspace(0,233,n);

Two problems hide here. The frequency axis is the bin index — 0 to 233 “cycles per 233 months” — so with xlim([0,6]) the plot shows only rhythms slower than one cycle every 39 months; a yearly rhythm would sit at bin 19, off the plot. A correct axis in cycles per year is (0:n-1)*12/n. And subtracting the mean removes only the constant: the series is dominated by its rise from zero to 100, and that trend puts its energy into the lowest bins, which top the spectrum whether the data has a rhythm or not. Removing a 13-month moving average first and then taking a Hann-windowed spectrum, the strongest remaining peaks sit near 1 and 2 cycles per year — a modest yearly pattern, with interest highest in March–April and lowest in August.

Try it yourself. Load multiTimeline.csv with np.loadtxt(..., delimiter=',', skiprows=3, usecols=1). Plot its spectrum against np.arange(n)*12/n three ways: mean removed, linearly detrended with scipy.signal.detrend, and with a 13-month moving average removed. Which one shows the yearly peak?

View source on GitHub →