views:

38

answers:

3

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?

+1  A: 

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')

alt text

Amro
Thanks, I'll try something similar in my project, since I don't think I'll need the exact same output.
dMaggot
A: 

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

mtrw
This one hits remarkably close to specgram
dMaggot
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
+1  A: 

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