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.
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.
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.
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.spectrogramis 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
- Plotting time on the independent axis
- Poisson Regression in statsmodels and R
- Popularity decay algorithm for popular website posts
- Predict classes or class probabilities?
- Predict NA missing values with machine learning
- Predicting a users next action based on current day and time
- Predicting Values with k-Means Clustering Algorithm
- Prediction After One-hot encoding
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.