How can I plot the spectrogram function results?

In my figure, I have 2 axes, the first is the time series of the signal, and the second is the ifft signal. I would like to add a third axis containing 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 tried using the spectrogram function, but it's hard for me to interpret its results as a shape. How to calculate the spectrogram so that I have time passing along the x axis and the amplitude along y?

+4
source share
1 answer

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

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

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

  • windows : number of samples used 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 to evaluate in
  • fs : sample rate of your signal.

Then build 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 the spectrogram depends on the use of the correct inputs in the spectrogram function.

+5
source

All Articles