# How do I plot the spectrogram of a wav file?

I am dabbing a little bit into signal processing on MATLAB. And I have the following question that I would like to know the answer to.

• What has to be done : Plot the Spectrogram of an audio file.
• What I know : Since its an audio file, I use wavread to read it and use the array returned to plot any of the graphs that I would like to. Only recently, I found that wav files store data on multiple channels. All the files that I have, have two channels. I know that the Spectrogram is supposed to give you an idea of the fundamental frequencies present in your audio file.
• What I would like to know : What does the Spectrogram exactly do? What do the parameters mean? This is the structure of the Spectrogram function call : S=spectrogram(x,window,noverlap,nfft,fs). What do the window and noverlap parameters mean? How do I set these parameters? Are they input file dependent? After all this, how do I plot the Spectrogram of the audio file since it contains two channels? The function returns an error when I try to pass the two dimensional(since there are two channels) array returned by wavread. It says that I can pass only a row or a column at a time. How do I go about plotting the Spectrogram of my wav file?

Thanks a million, in advance! : )

• – Paul R Jun 22 '12 at 17:19
• @PaulR well, yes even I saw the MATLAB help page. But I want to learn something more than that. Hence the new question on stackoverflow : ) – hRob Jun 22 '12 at 18:02
• @hRob: See Short-time Fourier transform – Amro Jun 22 '12 at 20:35

This isn't a great question, but I'll try to answer:

All the files that I have, have two channels.

A spectrogram can only show one channel at a time. Either make one spectrogram for each channel, or average your channels together into one. Here's an example of the syntax I found with a quick google search:

[y, fs]=wavread(fileName); % Read wave file
left=y(:,1); % Left channel
right=y(:,2); % Right channel


then you can do spectrogram(left)

I know that the Spectrogram is supposed to give you an idea of the fundamental frequencies present in your audio file.

Not quite. The term "fundamental frequency" means something different.

What does the Spectrogram exactly do?

It shows the frequency spectrum of the signal and how it changes over time. If the beginning of the signal is white noise (flat spectrum), and the end of the signal is a tone (spikey spectrum), the spectrogram will show how it changed from one spectrum to the other over time. It does this by dividing the signal up into small chunks and calculating the spectrum of each chunk.

What do the parameters mean? This is the structure of the Spectrogram function call : S=spectrogram(x,window,noverlap,nfft,fs). What do the window and noverlap parameters mean?

window is a window function that is applied to each chunk of the file before the frequency transform. If you didn't use a window function, there would be abrupt changes at the beginning and end of each chunk, which produce wide frequency components that you don't want. The window function is like a "fade-in, fade-out" envelope, so that you only see the spectrum of the middle of the chunk, and not the artifacts caused by chopping the chunk abruptly at the endpoints.

noverlap is the number of samples that each chunk overlaps the next. The windowing function attenuates the signal to near 0 at the boundaries of each chunk, so if you didn't overlap the chunks at all, you would be emphasizing parts of the signal and ignoring other parts. The overlap lets you measure the same part of the signal twice, once in the middle of the chunk where it's important, and once at the ends where it's mostly ignored.

How do I set these parameters?

Basically you try different ones and see which is best at showing you what you want to see. Different window functions are designed to do different things, so read about them and what they are meant to do.

Are they input file dependent?

Only if you're looking for different types of things in different files.

I know that the Spectrogram is supposed to give you an idea of the fundamental frequencies present in your audio file

For any sound sample there is always only one fundamental frequency and (possible) harmonics (integer multiples of fundamental frequency)

What does the Spectrogram exactly do?

At the most basic level it is dividing the sound sample into multiple "blocks" (in time domain) and plotting the fft of each block and displaying all of them to you in the same graph. The x-axis is time and the y axis is frequency (compare to an fft amplitude plot, where x axis is frequency and y axis is amplitude; in this case the analysis is done only on one of the "blocks" of the time domain; in a spectrogram the fft amplitude plot of each such "block" is shown to us at the same time). The amplitude of FFT for each time domain block is represented by colour and intensity

PS: please leave any corrections or better references for this explanation in the comments

• "For any sound sample there is always only one fundamental frequency": Sorry, that's simply not true. With chords & multiple instruments there are multiple fundamentals and with drums or some consonants there is none. – Hilmar Jan 4 '13 at 15:28

I am happy to add my 2 cents to answer your question. First lets get to the basics. A speech signal in technical terms is a quasi stationary signal. This means that the features of the signal are constant for a very short duration(say for about 20-25 ms). A speech signal consists of source information (just think of air pulses vibrating at a specific frequency which is called the pitch frequency) and the system information (As the air pulses pass thru your vocal tract they sort of get modulated by the resonant frequencies of the vocal tract). It is these features which change every 20-25ms.Generally in Speech Signal Processing, any operation is performed on a subset of signal which is of 20-25 ms since it is over this window/frame that the features are constant.

To answer your question about Spectrogram you need to know what a spectrum is. A spectrum like I said previously is calculated for a small window/frame which shows the resonant frequencies present in that window.A spectrogram is a graphical representation that shows the spectrum plot over all the frames. This makes it a 3-D figure and the Spectrogram function generates a 2-D projection of this figure. The x-axis represents the time scale (or the corresponding window), the y-axis represents the frequency and the grey stripes you see denote the magnitude of the frequency for that particular window.The darker the stripe, greater is the magnitude of the frequency component in that particular window of the signal.

I think the previous post explains the spectrogram function in detail. Please feel free to get back if you have any queries.

I guess it should be this (in frequency domain, not time domain):

[y, fs]=wavread(input);             % read wave file

left=y(:,1);                        % Left channel
right=y(:,2); % Right channel

fy = fft(left);                     % transform waveform to frequency domain

figure;
spectrogram(fy);

• 'spectrogram' does the transformation. – pichenettes Jun 14 '13 at 7:00
• Indeed, no FFT needed here. Please correct your answer. – Matt L. Jun 14 '13 at 8:43