audio analysis
spectrogram
signal processing
data visualization
sound analysis

plotting spectrogram in audio analysis

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A spectrogram shows how the frequency content of a signal changes over time. In audio analysis, it is one of the most useful visual tools because it turns a one-dimensional waveform into a time-frequency map that reveals harmonics, transients, noise, and repeating patterns much more clearly than the raw waveform alone.

What a Spectrogram Represents

A spectrogram is built from short-time Fourier transforms. Instead of transforming the whole signal at once, you split the signal into overlapping windows, compute a Fourier transform for each window, and then stack the results over time.

The axes mean:

  • horizontal axis: time
  • vertical axis: frequency
  • color intensity: magnitude or power

That makes spectrograms especially good for speech, music, bioacoustics, and machine listening.

A Simple Spectrogram With SciPy

Python's scipy.signal.spectrogram is a straightforward starting point.

python
1import numpy as np
2import matplotlib.pyplot as plt
3from scipy import signal
4
5sample_rate = 8000
6seconds = 2
7t = np.linspace(0, seconds, sample_rate * seconds, endpoint=False)
8
9# Two tones: 440 Hz and 880 Hz
10audio = np.sin(2 * np.pi * 440 * t) + 0.5 * np.sin(2 * np.pi * 880 * t)
11
12frequencies, times, spec = signal.spectrogram(
13    audio,
14    fs=sample_rate,
15    nperseg=256,
16    noverlap=128,
17)
18
19plt.pcolormesh(times, frequencies, 10 * np.log10(spec + 1e-12), shading="gouraud")
20plt.ylabel("Frequency [Hz]")
21plt.xlabel("Time [s]")
22plt.title("Spectrogram")
23plt.colorbar(label="Power [dB]")
24plt.show()

This produces a basic spectrogram in decibels, which is the usual display scale because raw magnitudes often span a large dynamic range.

Why Window Size and Overlap Matter

Two parameters shape the plot heavily:

  • 'nperseg, which controls window length'
  • 'noverlap, which controls overlap between windows'

Longer windows improve frequency resolution but smear events in time. Shorter windows improve time resolution but blur frequencies. There is no universally perfect choice; it depends on whether your signal contains short transients, steady tones, or both.

For music and speech, moderate overlap is common because it smooths the time axis without throwing away too much computation.

A librosa Version for Audio Workflows

When working on audio or music tasks, librosa is often more convenient because it is designed around spectrogram-driven workflows.

python
1import librosa
2import librosa.display
3import matplotlib.pyplot as plt
4
5y, sr = librosa.load(librosa.ex("trumpet"))
6stft = librosa.stft(y, n_fft=1024, hop_length=256)
7db = librosa.amplitude_to_db(abs(stft), ref=np.max)
8
9plt.figure(figsize=(10, 4))
10librosa.display.specshow(db, sr=sr, x_axis="time", y_axis="log")
11plt.colorbar(format="%+2.0f dB")
12plt.title("Log-frequency spectrogram")
13plt.tight_layout()
14plt.show()

This version is especially useful when log-scaled frequency is more natural for the problem, such as speech or musical pitch analysis.

Spectrograms Are Interpretation Tools, Not Just Pretty Images

A spectrogram can reveal:

  • strong harmonics as horizontal bands
  • onsets and percussive events as vertical bursts
  • background noise as broad diffuse energy
  • chirps and glides as sloping traces

That is why plotting a spectrogram is not just about visualization. It is often the fastest way to debug preprocessing, clipping, resampling problems, or mislabeled recordings.

Common Pitfalls

The biggest pitfall is plotting raw magnitudes without converting to a logarithmic scale. A linear scale often hides important lower-energy structure that becomes obvious in decibels.

Another common mistake is choosing a window size without thinking about the signal. If you use a very long window for a short transient sound, the spectrogram may make the event look smeared and uninformative.

Developers also forget sample-rate context. Frequency bins are only meaningful if the plotting code uses the correct sampling frequency. A wrong sample rate makes the entire y-axis misleading.

Summary

  • A spectrogram shows how frequency content changes over time.
  • It is computed from windowed Fourier transforms of the signal.
  • 'scipy.signal.spectrogram is a simple starting point for plotting one.'
  • Window size and overlap control the time-frequency tradeoff.
  • Converting magnitudes to decibels usually makes spectrograms far more informative.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.