how to implement spectrogram in matlab?

2 次查看(过去 30 天)
I am trying to plot the spectrogram of the following signal with following code
% Signal 2
fs = 40; % Sampling frequency
t2 = 0:( 1/fs ):6; % Time vector
S2 = [ sin( 2*pi*5*t2( t2<=2 ) ), sin( 2*pi*10*t2( 2<t2<4 ) ),sin( 2*pi*15*t2( t2>4 ) ) ];
% codes for spectrogram
X = S2 + 2*randn(size(t2)); % Defining the Entire Data Vector for Spectogram
NFFT = 2^nextpow2(402);
window = 100;
spectrogram(X,window,window/2,NFFT,fs);
I am not getting the right spectrogram plot. Can someone tell me where is the problem with the code?

回答(1 个)

Walter Roberson
Walter Roberson 2016-12-29
2<t2<4 is parsed as ((2<t2)<4). The 2<t2 part returns 0 (false) or 1 (true) and then <4 part compares that 0 or 1 to <4, which is always true. The fix is:
S2 = [ sin( 2*pi*5*t2( t2<=2 ) ), sin( 2*pi*10*t2( 2<t2 & t2<4 ) ),sin( 2*pi*15*t2( t2>4 ) ) ];
By the way: is there a reason that you want the sample at t2 == 4 exactly to be omitted ?

类别

Help CenterFile Exchange 中查找有关 Time-Frequency Analysis 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by