Wavelets and time-frequency analysis

Short, localised waves that ask "how much of this frequency, right now?" — the Morlet wavelet and the scalogram.

What you'll learn
  • Build Morlet, Haar and Mexican-hat wavelets and read their spectra
  • Convolve a signal with complex Morlet wavelets to get power over time and frequency
  • Explain the time–frequency trade-off set by the number of wavelet cycles

A sine wave lasts forever, so the Fourier transform says which frequencies a signal contains but not when. A wavelet is a short burst of oscillation: convolve a signal with one and the output is large exactly where, and only where, the signal contains that oscillation.

Three wavelets

wavelets.m builds the classic shapes on a 5-second time axis at 1024 Hz:

csw = cos(2*pi*freq*timevec);                           % 4 Hz cosine
gaussian = exp(-(4*log(2)*timevec.^2) / (fwhm^2));      % FWHM 0.5 s
morletWavelet = csw .* gaussian;

The Morlet wavelet — a sine wave inside a Gaussian envelope — has a smooth, Gaussian-shaped spectrum around its frequency, which makes it the default for time-frequency analysis. The Haar wavelet, a +1 block followed by a −1 block, is the simplest possible and has a spectrum with slowly decaying ripples. The Mexican hat (the negative second derivative of a Gaussian) detects bumps and edges rather than a particular frequency. The script plots each with its spectrum, and the comment on fwhm states the key trade-off: a wider wavelet in time is narrower in frequency.

Complex wavelets and the scalogram

A real wavelet’s output oscillates, crossing zero even when the signal is full of the target rhythm. A complex Morlet wavelet, \(e^{2\pi i f t}\) under a Gaussian, solves this: the magnitude of its convolution is a smooth envelope — power at frequency \(f\) over time. Repeating for many frequencies and stacking the rows gives a scalogram. time_freq_analysis_via_complex_wavlet.m builds 40 such wavelets and convolves each with the data through the FFT (lesson 6):

for fi = 1:numFrex
    cmw(:,fi) = exp(1i*2*pi*frex(fi)*wavet) .* exp(-(4*log(2)*wavet.^2)/fwhms(fi).^2);
end
...
waveX = fft(cmw(:,fi),nConv);
waveX = waveX ./ max(waveX);
as = ifft(dataX .* waveX);
Building a scalogram row by row. The signal is a chirp rising from 4 to 20 Hz plus a 30 Hz burst near 3 s. Each step convolves it with a complex Morlet wavelet (orange, real part) at one frequency and paints that row with the resulting power. Change the number of cycles to trade time sharpness for frequency sharpness.

The rising diagonal is the chirp; the isolated blob is the burst — two features a single Fourier spectrum would merge into one smeared band. The number of cycles under the Gaussian decides the trade-off: a wavelet of \(c\) cycles at frequency \(f\) lasts about \(c/f\) seconds and resolves frequencies to about \(0.88\,f/c\) Hz. Few cycles give sharp timing and blurry frequency; many cycles the reverse. The script’s Gaussians are 5 to 15 seconds wide, growing with frequency — 10 cycles at 2 Hz but 375 at 25 Hz — so its map is sharp in frequency and coarse in time, especially at the top of the band.

One observation on applying it to the earthquake record: the script analyses 2 to 25 Hz, but, as lesson 12 showed, 99.99% of this recording’s power lies below 0.2 Hz — only a few millionths of it is above 2 Hz. The decibel normalisation to the pre-event baseline (basetidx) is what makes the high band visible at all; setting minFreq to around 0.02 Hz, with correspondingly wider wavelets, would look where the energy is.

Try it yourself. Build a complex Morlet at 10 Hz in NumPy, convolve it with np.sin(2*np.pi*10*t) and with np.sin(2*np.pi*12*t), and compare np.abs(...) in the middle of the signal. Repeat with 3 and with 12 cycles, and relate the ratio you see to the \(0.88\,f/c\) bandwidth.

View source on GitHub →