how to implement spectrogram in matlab?

2 views (last 30 days)
Dana Alsalman
Dana Alsalman on 29 Dec 2016
Answered: Walter Roberson on 29 Dec 2016
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?

Answers (1)

Walter Roberson
Walter Roberson on 29 Dec 2016
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 ?

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!