Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 57 ยท Working with Data

Plotting Sound and Spectrograms

Plot a sound wave and its spectrum with Matplotlib and NumPy, then draw a spectrogram with specgram: sample rate, window size, overlap, decibels and colour limits.

Sound is a number that changes quickly over time: the air pressure at a microphone, measured thousands of times per second. That makes audio a perfect subject for Matplotlib. In this lesson you will draw three pictures of the same sound: the waveform (pressure over time), the spectrum (which frequencies are present), and the spectrogram (how the frequencies change over time).

All sounds here are generated with NumPy, so nothing needs to be downloaded. The same code works on a real recording once you have its samples in an array, and the end of the lesson shows how to read a WAV file.

Samples and sample rate

A digital recording is an array of samples. The sample rate says how many samples there are per second, in hertz (Hz). CD audio uses 44,100 Hz; speech is often 16,000 Hz. To plot against time in seconds, build a time axis from the sample rate:

t = np.arange(n_samples) / sample_rate

A pure musical note is a sine wave, sin(2 * pi * f * t), where f is the frequency in Hz. The note A above middle C has f = 440. A chord is a sum of several sines.

Python Waveform of a chord

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Plot only a short slice. One second at 8,000 samples per second is 8,000 points, and a 440 Hz wave repeats 440 times, so plotting everything makes a solid block of colour. Zoom in to about 50 milliseconds to see the individual oscillations. In the lower chart the two notes interfere: the shape repeats less cleanly than a single sine.

The spectrum: which frequencies are present?

The waveform of a chord is hard to read, but its spectrum is clear. The Fast Fourier Transform (FFT) splits a signal into frequencies. np.fft.rfft handles real-valued signals, and np.fft.rfftfreq gives the frequency of each result bin:

Python Spectrum with the FFT

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two peaks stand out: one at 440 Hz and a smaller one at 1,000 Hz, whose height matches its amplitude of 0.5. The noise forms a low floor. The Hann window (np.hanning) fades the signal in and out so that cutting it at the ends does not create false frequencies, a problem called spectral leakage. The Signals and FFT lesson in the NumPy course explains the transform in more depth.

A spectrum has one weakness: it describes the whole signal at once. A melody and its notes played in reverse would have the same spectrum. We need to see frequencies change over time.

The spectrogram: frequency over time

A spectrogram slices the signal into short overlapping windows, computes a spectrum for each window, and shows the results side by side as an image: time on the x-axis, frequency on the y-axis, and colour for the strength (loudness) of each frequency.

flowchart LR
    S["Signal"] --> W["Cut into short<br/>overlapping windows"]
    W --> F["FFT of each window"]
    F --> I["Stack as columns:<br/>time x frequency, colour = power"]

A spectrogram is many small spectra, one per time slice, drawn as an image.

Matplotlib has a built-in function, ax.specgram, which does all of this. Here is a chirp, a tone whose pitch rises over time, which shows up as a diagonal line:

Python A rising chirp in a spectrogram

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

specgram returns the data as well as drawing it: a 2D array power with shape (frequency bins, time slices), plus the matching freqs and times. So the spectrogram is simply a heatmap of a NumPy array. The colour scale is in decibels (dB), a logarithmic scale, because the loudness range of real sounds is enormous.

The key parameters

Three settings control the picture, and they trade off against each other.

  • NFFT: window length in samples. A long window (1024 or 2048) separates close frequencies sharply but blurs timing. A short window (128 or 256) locates events in time sharply but smears the pitch. This is a fundamental trade-off, not a bug.
  • noverlap: how many samples neighbouring windows share. Half of NFFT is common; more overlap gives a smoother image and costs more computation.
  • Fs: the sample rate. If it is wrong, the frequency axis is wrong. Always pass the true rate.

You can also choose window= (the default is a Hann window), cmap=, and vmin/vmax to limit the colour range, which brings out faint details. Compare window sizes on a signal that has both a steady tone and a short click:

Python Window size: pitch detail against timing detail

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With the short window (left) the two tones at 600 and 680 Hz blur into one fat band, but the click is a thin vertical line, exactly located in time. With the long window (right), the two tones separate into two lines, while the click is smeared horizontally. Pick the window that shows what you care about.

A hand-made spectrogram

To know what specgram does, build one yourself: cut the signal into overlapping frames, multiply each frame by a window, take rfft, and stack the results into a 2D array. Then show it with imshow:

Python A spectrogram built from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Details that matter: origin="lower" puts low frequencies at the bottom (images normally start at the top), aspect="auto" stretches the image to the Axes, extent labels the axes in seconds and hertz instead of pixel indexes, and vmin = max - 70 shows only the top 70 dB, hiding numerical noise. These are the same tools used in Heatmaps and Images.

More signal plots: psd, magnitude_spectrum, acorr, xcorr and cohere

specgram is the best-known signal method, but the Axes object has a family of one-line plots for the questions that come next. They all take the raw signal (and its sample rate Fs) and do the maths for you, so they are a quick alternative to the manual FFT earlier in this lesson.

Method Question it answers
ax.magnitude_spectrum(x, Fs) How strong is each frequency? (one FFT of the whole signal)
ax.psd(x, NFFT, Fs) How is the power spread over frequency? (Welch averaging: less noisy)
ax.acorr(x) Does the signal resemble a shifted copy of itself? (repeating patterns, periods)
ax.xcorr(x, y) How similar are two signals when one is shifted? (delays, alignment)
ax.cohere(x, y, NFFT, Fs) At which frequencies do two signals move together? (value from 0 to 1)

Spectrum and power spectral density

magnitude_spectrum is the FFT of the full signal in one call (it can also scale in dB with scale="dB"). It is sharp but noisy for noisy signals: every random fluctuation shows. psd (power spectral density) cuts the signal into overlapping windows, computes a spectrum for each, and averages them (Welch's method), which smooths the noise and makes the true peaks stand out. NFFT is the window length: longer windows separate close frequencies, shorter ones average more and look smoother.

Python magnitude_spectrum and psd on a noisy signal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both methods find the 50 Hz tone, and psd shows the weaker 120 Hz tone as a clear bump above a flatter noise floor, because averaging many windows pulls the noise down. The methods return the numbers as well as drawing them, so you can read off peaks as the print lines do. If you only need the numbers, use scipy.signal.welch instead; for plots, the Axes methods are shorter.

Autocorrelation and cross-correlation

Autocorrelation compares a signal with a copy of itself shifted by a lag. A signal that repeats every 40 samples correlates strongly with itself at lag 40, so peaks in the plot reveal periods hidden in noise. Cross-correlation compares two different signals at every lag; the lag of the highest peak tells you how much one is delayed against the other (a sound arriving at two microphones, a sensor reading that follows a cause).

ax.acorr(x, maxlags=...) and ax.xcorr(x, y, maxlags=...) draw the correlation as vertical lines by default. Subtract the mean first (x - x.mean()), otherwise the constant offset dominates, and use normed=True (the default) to get values between -1 and 1.

Python Finding a period and a delay with acorr and xcorr

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The autocorrelation has its trivial maximum of 1 at lag 0 (a signal equals itself), then a clear second peak near lag 40: that is the period (the exact lag of the highest point wobbles by a sample or two because of the noise). The cross-correlation peaks at lag -15. The sign depends on the argument order: xcorr(x, y) peaks at a negative lag when y is a delayed copy of x, so always check the direction with a signal whose delay you know.

Coherence: where do two signals agree?

Coherence is a correlation per frequency. Two signals can share a 100 Hz hum but disagree about everything else. ax.cohere(x, y, NFFT, Fs) plots a number between 0 (no relationship at this frequency) and 1 (perfectly linked), which makes it a standard test in vibration, brain-signal and audio analysis.

Python cohere: a shared tone in two noisy channels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The coherence is close to 1 at 100 Hz (the shared tone) and low at 30 Hz, where only channel a has a tone, and low across the noise. Use enough data and windows: coherence is estimated by averaging, and with too few windows it is misleadingly high everywhere.

Using a real recording

Reading a WAV file needs only the standard library. Here is how it looks for a mono 16-bit file (this part is read-only because there is no file in the browser):

Python
import wave
import numpy as np

with wave.open("speech.wav", "rb") as wav:
    sr = wav.getframerate()
    raw = wav.readframes(wav.getnframes())
    channels = wav.getnchannels()

samples = np.frombuffer(raw, dtype=np.int16).astype(np.float32) / 32768.0   # scale to -1..1
if channels == 2:
    samples = samples.reshape(-1, 2).mean(axis=1)                          # mix to mono

fig, ax = plt.subplots()
ax.specgram(samples, NFFT=512, Fs=sr, noverlap=256, cmap="magma")

Libraries such as soundfile (sf.read("file.flac")) and librosa read more formats and give you ready-made mel spectrograms, but the Matplotlib part stays the same. Dividing by 32768 converts 16-bit integers into floats between -1 and 1; skipping this step is a common reason for spectrograms with strange colours.

Common mistakes

  • Wrong sample rate. Everything on the frequency axis scales with it.
  • Plotting millions of samples. Zoom into a time slice, or plot the minimum and maximum of blocks of samples (an "envelope").
  • Linear power instead of dB. A linear colour scale shows only the loudest event and hides everything else.
  • Not limiting the colour range. Set vmin and vmax (or use ax.set_ylim to focus on the interesting frequencies, such as 0 to 4,000 Hz for speech).
  • Stereo files. A (n, 2) array holds two channels. Select one channel or average them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you build a time axis in seconds for a signal with n samples?

    Each sample is 1 / sample_rate seconds apart, so dividing the sample index by the rate gives the time in seconds.
  2. 2. What does the FFT of a signal tell you?

    The FFT splits the signal into frequencies. It describes the whole signal, so it does not show when each frequency occurs.
  3. 3. What are the three axes of information in a spectrogram?

    A spectrogram is a heatmap: each column is the spectrum of one short time slice, with colour showing power.
  4. 4. You increase NFFT from 128 to 1024. What happens?

    A longer window has finer frequency resolution and coarser time resolution. This trade-off is fundamental.
  5. 5. Why are spectrograms usually coloured in decibels?

    The logarithmic dB scale compresses the large dynamic range, so faint detail is visible next to loud events.

Summary

  • Audio is an array of samples; the time axis is np.arange(n) / sample_rate. Plot only short slices of a waveform.
  • The spectrum (np.fft.rfft, rfftfreq, with a Hann window) shows which frequencies are present.
  • A spectrogram is a heatmap of many short spectra: time, frequency and colour for power in dB.
  • ax.specgram(x, NFFT, Fs, noverlap, cmap) draws it and returns the data; long windows favour pitch detail, short windows favour timing.
  • Build one by hand with frames, a window, rfft and imshow(origin="lower", aspect="auto", extent=...).
  • magnitude_spectrum and psd show frequency content (psd averages windows to tame noise); acorr finds periods, xcorr finds delays, cohere shows per-frequency agreement of two signals.
  • Read WAV files with the wave module, scale integers to -1..1, mix stereo to mono, and always use the true sample rate.

Next: 3D Plots leaves the flat page and adds a third axis.