# Using MATLAB's Spectrogram Function for Analysis

In the documentation - sepctrogram().

Question 1)s = spectrogram(signal) and spectrogram(signal) are two commands to plot the spectrogram. However, the variable s is complex valued. I am unable to understand which output of the spectrogram is used to generate the image plot?

Question 2) How to determine the best values of the parameters window and noverlap ? Should noverlap be 50% of the signal length (number of elements in the time series) or 90% etc? What if it is zero then what does it mean? My dataset has sampling time = 1sec. I remember reading somewhere that the window should be at least roughly twice as long as the period of the lowest frequency. So, for my case is w=2 since frequency = 1?

I was thinking of using pspectrum(signal,'spectrogram') which outputs the spectrogram and use the output values as inputs to the spectrogram() function. But again, I don't know which output values from pspectrum can be used, if at all that is possible.

## 2 Answers

1. See the MATLAB documentation:

s = spectrogram(x) returns the short-time Fourier transform of the input signal, x. Each column of s contains an estimate of the short-term, time-localized frequency content of x.

Namely each column of the matrix s is the result of an fft() on some samples of the input. So the plot you see is the magnitude of the columns of s.

1. Spectrogram is about analysis of Non Stationary signals. So something is changing over time which means it makes no sense to look on the DFT of all samples. The window length is the time you think the signal has the same properties over time. The overlap time should be similar to the time the signal is changing. Something like the time of Fade Out / Fade In, the transient length.

## Example

The following code will recreate the figure from the function (Up to the Colorbar and the units of the Axis):

t = 0:0.001:2;
x = chirp(t, 100, 1, 200, 'quadratic');

figure();
spectrogram(x, 128, 120, 128, 1e3);

s = spectrogram(x, 128, 120, 128, 1e3);

figure();
hA = axes();
imagesc(20 * log10(abs(s).'));
set(hA, 'YDir', 'normal');

• Thank you for answering. Just to be clear for point1) just for sanity check if I do spectImage=(20*log10(abs(s))) (this is the magnitude of the fft of s) and do imagesc(spectImage) I should get the same output as given by using spectrogram(signal)? Also, could you please clarify few stuff regarding point 2) My sampling time is 1sec so what should be the nfft value. I have plotted the time signal in my Question. It seems not to be changing much. How do I know the window length for this case?
– Sm1
Sep 15, 2020 at 5:06
• @Sm1, I added how to reproduce the figure in the answer. Regarding your signal, it seems to be piece wise linear. It might be smart to analyze it after removing those linear trends from each section.
– Royi
Sep 15, 2020 at 5:17
• Thank you for answering. One last question- In the example that you plotted is 1e3 the value of nfft or fs or f is unclear. Is fs the nyquist frequency? For my signal whose sampling time is 1 sec, what should be the nfft, fs or f which I should include as input parameters and what is the unit of the colorbar?
– Sm1
Sep 15, 2020 at 14:36

In general:

1.) The real values are the magnitudes and the imaginary values are the phase. Phase is typically ignored when plotting a spectrogram.

2.) The best values for overlap and windowsize are made on a case-by-case basis and really depends on what you're looking for. Overlap is usually measured in percent and window is usually measured in samples so it's samplerate agnostic. BUT! The window size determines the resolution in frequency at the expense of temporal resolution; i.e. better frequency resolution (larger window) means less time resolution and vice versa. The overlap can mitigate this a bit but tends to "smear" the magnitudes in time.

Say my signal is 1000 samples long and I'm looking for something in which my temporal resolution needs to be high (a short event). Then I'd set my window low (say 256 samples) and my overlap to be 50% to start. Then you just play with it and see what you need: more frequency information (larger window) or more time information (smaller window and/or more overlap). Setting it to 0% means no overlap and "hard" changes from one window to the next.

Setting the window to 50% of your signal (500 samples in the case above) is fine if all you need is frequency information over a long period of time. But lots of information (especially temporal information) will be lost...