2

In Matlabs recent versions, the specgram function is being replaced by spectrogram, and the documentation states:

Note. To obtain the same results for the removed specgram function, specify a 'Hann' window of length 256.

Unfortunately, that doesn't seem to work for me, if I use spectrogram(signal,hann(256)), the result is different from specgram(signal), although both are quite similar. Is there a way to get the exact same output?

Amro
  • 123,847
  • 25
  • 243
  • 454
dMaggot
  • 81
  • 1
  • 6

3 Answers3

4

Well, I just stumbled upon the solution:

specgram(singal) = spectrogram(signal, hanning(256))

since hann and hanning aren't the same thing in Matlab.

Thanks everyone for the support.

dMaggot
  • 81
  • 1
  • 6
3

I believe they are computed a bit differently in each function. This is the best I could obtain:

sig = rand(1280,1);
Fs = 2;
nfft = 256;
numoverlap = 128;
window = hanning(nfft);

%# specgram
subplot(121), specgram(sig,nfft,Fs,window,numoverlap)

%# spectrogram: make it look like specgram
[S,F,T,P] = spectrogram(sig,window,numoverlap,nfft,Fs);
subplot(122), imagesc(T, F, 20*log10(P))
axis xy, colormap(jet), ylabel('Frequency')

spectrogram

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Thanks, I'll try something similar in my project, since I don't think I'll need the exact same output. – dMaggot Jul 20 '10 at 11:54
1

I don't currently have Matlab to try, but hann(256,'periodic') might be what you're looking for.

mtrw
  • 34,200
  • 7
  • 63
  • 71
  • I would be surprised because if you look at the source code of specgram `edit specgram.m`, you will find around line 133 that its using the older HANNING function instead of the HANN function (although both are the same and defined as `0.5*(1-cos(2*pi*n/nfft))`) with 'symmetric' as the default option – Amro Jul 21 '10 at 05:23