Understanding the Mel Spectrogram
Signals
A?signal?is a variation in a certain quantity over time. For audio, the quantity that varies is air pressure. How do we capture this information digitally? We can take samples of the air pressure over time. The rate at which we sample the data can vary, but is most commonly 44.1kHz, or 44,100 samples per second. What we have captured is a?waveform?for the signal, and this can be interpreted, modified, and analyzed with computer software.
The Fourier Transform
An audio signal is comprised of several single-frequency sound waves. When taking samples of the signal over time, we only capture the resulting amplitudes. The?Fourier transform?is a mathematical formula that allows us to decompose a signal into it’s individual frequencies and the frequency’s amplitude. In other words, it converts the signal from the time domain into the frequency domain. The result is called a?spectrum.

This is possible because every signal can be decomposed into a set of sine and cosine waves that add up to the original signal. This is a remarkable theorem known as?Fourier’s theorem.

The Spectrogram
The fast Fourier transform is a powerful tool that allows us to analyze the frequency content of a signal, but what if our signal’s frequency content varies over time? Such is the case with most audio signals such as music and speech. These signals are known as?non periodic?signals. We need a way to represent the spectrum of these signals as they vary over time. You may be thinking, “hey, can’t we compute several spectrums by performing FFT on several windowed segments of the signal?” Yes! This is exactly what is done, and it is called the?short-time Fourier transform.?The FFT is computed on overlapping windowed segments of the signal, and we get what is called the?spectrogram.?Wow! That’s a lot to take in. There’s a lot going on here. A good visual is in order.

You can think of a spectrogram as a bunch of FFTs stacked on top of each other. It is a way to visually represent a signal’s loudness, or amplitude, as it varies over time at different frequencies. There are some additional details going on behind the scenes when computing the spectrogram. The y-axis is converted to a log scale, and the color dimension is converted to decibels (you can think of this as the log scale of the amplitude). This is because humans can only perceive a very small and concentrated range of frequencies and amplitudes.

Ok. We are almost there! We have a solid grasp on the “spectrogram” part, but what about “Mel.” Who is he?
The Mel Scale
Studies have shown that humans do not perceive frequencies on a linear scale. We are better at detecting differences in lower frequencies than higher frequencies. For example, we can easily tell the difference between 500 and 1000 Hz, but we will hardly be able to tell a difference between 10,000 and 10,500 Hz, even though the distance between the two pairs are the same.
In 1937, Stevens, Volkmann, and Newmann proposed a unit of pitch such that equal distances in pitch sounded equally distant to the listener. This is called the?mel scale.?We perform a mathematical operation on frequencies to convert them to the mel scale.

The Log Mel Spectrogram
A?mel spectrogram?is a spectrogram where the frequencies are converted to the mel scale. I know, right? Who would’ve thought? What’s amazing is that after going through all those mental gymnastics to try to understand the mel spectrogram, it can be implemented in only a couple lines of code.

In Summary
We took samples of air pressure over time to digitally represent an audio?signal
We mapped the audio signal from the time domain to the frequency domain using the?Fourier transform, and we performed this on overlapping windowed segments of the audio signal.
We converted the y-axis (frequency) to a log scale and the?amplitude?(color dimension) to decibels to form the?spectrogram.
We mapped the y-axis (frequency) onto the?mel scale?to form the?mel spectrogram.