No history yet

Spectrogram Generation

From Waveform to Image

A raw audio file, in a format like PCM, is just a long series of numbers. Each number represents the amplitude of the sound wave at a specific moment in time. This is the time-domain view. While accurate, it's not very useful for identifying a song. It tells us how loud the sound is, but not what notes are being played. To identify a song, we need to see its frequency content over time.

The goal is to convert this one-dimensional list of amplitudes into a two-dimensional image called a spectrogram. A spectrogram plots time on the x-axis, frequency on the y-axis, and the intensity or amplitude of each frequency with color. It’s like a musical score written by a computer. The tool for this job is the (STFT).

The STFT works by breaking the long audio signal into many small, overlapping segments or "frames." It then runs a Fast Fourier Transform (FFT) on each frame individually to find the frequencies present in that tiny slice of time. But there's a catch. Simply chopping the signal into rectangular blocks creates sharp, unnatural edges, which introduces noise into the frequency analysis. This phenomenon is called —where energy from one frequency "leaks" into others.

To fix this, we apply a windowing function to each frame before the FFT. Functions like Hann or Hamming smoothly taper the beginning and end of each frame down to zero. This gentle fade-in and fade-out removes the hard edges, resulting in a much cleaner frequency analysis for each slice.

The Resolution Trade-Off

Choosing the size of the window (or frame) is a critical decision. It forces a trade-off between time resolution and frequency resolution. Think of it like a camera's shutter speed.

A short window is like a fast shutter speed. It captures a precise moment in time, but the resulting image might lack fine detail. You know exactly when a sound occurred, but you have less certainty about its exact frequency.

A long window is like a slow shutter speed. It gathers more data, giving you a very detailed, sharp picture of the frequencies present. But because it captures a longer duration, any fast changes are blurred together, and you lose precision on when the sound happened.

Window SizeTime ResolutionFrequency Resolution
Short (e.g., 1024 samples)High (Good for transients like drum hits)Low (Hard to distinguish close notes)
Long (e.g., 4096 samples)Low (Fast events are smeared)High (Good for analyzing harmonies)

Because the windowing function tapers the ends of each frame, some data would be lost if the frames were placed back-to-back. To prevent this, we overlap them, typically by 50%. This ensures every part of the signal is fully analyzed and contributes to the final spectrogram, creating a smoother and more accurate result.

Putting It All Together

The final step is to compute the magnitude of the FFT for each windowed frame. The FFT produces complex numbers, but for the spectrogram, we only care about the energy, or magnitude, at each frequency bin. We simply take the absolute value of each complex number in the FFT output.

Each of these magnitude calculations produces a single vertical slice of our spectrogram. By stacking these slices side-by-side in chronological order, we build the complete image. The result is a rich, two-dimensional representation of the audio's harmonic content over time, ready to be analyzed for its unique fingerprint.

Quiz Questions 1/5

What is the primary purpose of converting a raw, time-domain audio signal into a spectrogram?

Quiz Questions 2/5

In the Short-Time Fourier Transform (STFT) process, what problem is directly addressed by applying a windowing function (like Hann or Hamming) to each audio frame?