How can I plot the results of the spectrogram function?

99封情书 提交于 2019-12-07 06:43:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13858807/how-can-i-plot-the-results-of-the-spectrogram-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!