1

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!

JMFS
  • 247
  • What does the function specgram do? Does it multiply the hanning window with lowpass filer response? – Samrat Mukhopadhyay Jul 23 '13 at 19:47
  • It splits the signal into overlapping segments of "tw" seconds, and multiply each segment by the hanning window. The window acts like a lowpass filter. – JMFS Jul 23 '13 at 20:06
  • I don't understand the hates and downvotes of this question. OP is not begging for code, neither asking for debugging, he has a perfectly executable matlab code with no error message. OP is just asking for some interpretation the results he got. +1 – Shuhao Cao Jul 24 '13 at 00:01

1 Answers1

1

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);
horchler
  • 3,203
  • Thanks, your explanation is usefull!! But the questions was about the command "plot(f,abs(X))", because it already plots me a spectrum, not a specgram. But I don't know which of the set of spectrums it plots. – JMFS Jul 24 '13 at 21:21
  • @juanma2268: See the first three sentences of my answer. You're plotting all of the spectrums. They happen to be almost completely lined up with each other, which is why it looks like one plot. If you zoom into the area around the origin you'll see that there are actually multiple lines. If you want to see this more clearly, use semilogy(f,abs(X)) instead of plot(f,abs(X)). X is 257-by-124. The 124 columns correspond to the 124 times in your vector tj. If, e.g., you just want to plot the first spectrum corresponding to tj(1), use plot(f,abs(X(:,1))). – horchler Jul 24 '13 at 22:04
  • Ups, sorry. I have to read more carefully. – JMFS Jul 25 '13 at 01:15