# How to obtain the Spectrogram of a signal

I am trying to use Matlab to compute the spectrogram of a signal but I can't seem to figure out how Matlab computes the spectrogram.Consider the case where I have a signal x(t) with M samples and I want to compute it's spectrogram using hann windows of length N and using an overlap of K samples.

Because there are M samples and the advance time is (N - K) samples, I would expect the resultant spectrogram to have dimensions of either $N$ by $\frac{W}{(N - K)}$ OR if matlab only provides frequencies up to $\frac{f_s}{2}$ then dimensions to be $\frac{N}{2}$ by $\frac{W}{(N-K)}$ but I am getting weird dimensions.

The code below shows the case where I have computed the spectrogram when M = 32 * 700 = 24400, N = 256 and K = 256 - 32 = 224.

%specgram(data, ChunkSize, SampleRate, Size Of Hanning Window, Overlap (in Samples)
D = specgram( 1:1:(32*700),256, 8000 ,256 , 224);


produces a 129 * 693 matrix instead of the (256 * 700) or (128 * 700) spectrogram I was expecting.

Result should be an ($M$ by $\text{Number Of STFT Chuncks}$) matrix and $$\text{Number Of Chunks} = \frac{\text{Number Of Samples in Signal}}{\text{Advance Time (in Samples)}}$$

$$\text{Number Of Chunks} = \frac{32 * 700}{32} = 700$$

Have I missed out on something? Any help will be appreciated.

• FYI, I believe specgram is being deprecated, and replaced by spectrogram. – dpbont Mar 4 '14 at 8:30

Since the input signal is real, half of the FFT data is "useless" because it is simply the complex conjugate of the other half. More precisely, a 256-long FFT of a real signal will give you: the DC amplitude, 127 amplitudes, the amplitude at Nyquist, and 127 conjugate amplitudes.

Among the 700 chunks, 256 / 32 - 1 = 7 extend outside the boundaries of the original signal.

Hence, 129x693.