SPECGRAM return value

262 Views Asked by At

I was studying this code:

fm = 8000;
dt = 1/fm;    % dt=0.000125
t = [1:dt:5];
y = sin(2*pi*200*t);

tw = 0.05;
ws = 2 .^ round( log2( tw*fm ) );    % ws=512
o = ws/2;    % o=128
w = hanning(ws);
[ X, f, tj ] = specgram( y, ws, fm, w, o );

What X represents is an array of Spectrums, one per "tw" windows on the signal. When I call:

plot(f,abs(X));

Matlab gives me the plot of one spectrum. Is that spectrum the summation of the all the spectrums of the signal?

Thanks in advance!

1

There are 1 best solutions below

3
On BEST ANSWER

All of your spectrums are there. They're just overlapped. Try zooming in. Or type whos and look at the size of X. If you want a spectral image plot, you need to call the function with no outputs:

specgram(y, ws, fm, w, o);

which generates a figure like this: enter image description here

Alternatively, you can manually plot the spectrum image using surf:

[X, f, tj] = specgram(y, ws, fm, w, o);
surf(tj,f,10*log10(abs(X)),'EdgeColor','none');   
axis xy;
axis tight;
colormap(jet(256)); % 256 colors instead of default 64 (why?!) reduces blotchiness
view(0,90);
grid off;
xlabel('Time');
ylabel('Frequency (Hz)');


Also, FYI, the help for specgram in Matlab R2012b states:

specgram has been replaced by SPECTROGRAM. specgram still works but
may be removed in the future. Use SPECTROGRAM instead. Type help
SPECTROGRAM for details.

You'll need to change the order of your inputs to use spectrogram, I think, assuming you have it in your version. Try:

[X, f, tj] = spectrogram(y, w, o, ws, fm);