Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot the results of the spectrogram function?

Within my figure I have 2 axes, the first is the time series of the signal and the second is the ifft of the signal. I'd like to add a 3rd axes that contains the spectrogram of the signal. How can I do this?

% Create the raw signal
fs = 40;
t = 0:( 1/fs ):4;
y1 = [ sin( 2*pi*5*t( t<=2 ) ), sin( 2*pi*10*t( t>2 ) ) ];

% Compute the ifft of the signal
Fy1 = abs(ifft(y1));
N = numel(t);
idx = 1:numel(Fy1) / 2;
f = fs*(0:(N-1)) / N;

% Plot the raw signal as a time series
subplot(311);
plot(t,y1,'k');
xlabel('Time (s)');
ylabel('Amplitude');

% Plot the spectrum of the signal
subplot(312);
plot(f(idx),2*Fy1(idx),'k')
xlabel('Frequency (cycles/second)');
ylabel('Amplitude');

I've tried using the spectrogram function however I'm having a hard time interpreting its results as a figure. How do I compute the spectrogram so that i have time running along the xaxis and the amplitude along the y?

like image 894
KatyB Avatar asked Dec 13 '12 11:12

KatyB


1 Answers

You need to provide more input arguments into spectrogram. The form of the function you need is:

[S,F,T]=spectrogram(x,window,noverlap,F,fs)

See http://www.mathworks.com/help/signal/ref/spectrogram.html complete documentation, but basically you need define:

  • windows: the number of samples to use for each spectral estimate calculation
  • noverlap: how many samples to include from the calculation of spectrum N-1 in spectrum N
  • F: the frequencies you want the spectrum evaluated at
  • fs: the sampling frequency of your signal.

Then plot the spectrogram with:

subplot(313);
imagesc( T, F, log(S) ); %plot the log spectrum
set(gca,'YDir', 'normal'); % flip the Y Axis so lower frequencies are at the bottom

Note: The quality and interpretability of a spectrogram depends on using the correct inputs into the spectrogram function.

like image 147
slayton Avatar answered Sep 30 '22 17:09

slayton