%例程6-2 计算非平稳随机信号的STFT时频谱
% e.g.6-2.m for example6-2;
clear;
N=500; %length of the signal x.
t=0:N-1;
x=zeros(size(t));
%Generate two sine signal with different frequencies.
x(50:150)=cos(pi*(t(50:150)-50)/10);
x(250:350)=cos(pi*(t(250:350)-250)/20);
subplot(221);
plot(x);
axis([ 0 500 -1 1]);
grid;
X=fft(x);
X=fftshift(X); %Shift zero-frequency component to center of spectrum
subplot(222);
plot(abs(X));
axis([0 500 0 60 ]);
grid;
Nw=20; %window length
L=Nw/2;
Ts=round((N-Nw)/L)+1;
nfft=128;
TF=zeros(Ts,nfft);
for i=1:Ts
xw=x((i-1)*L+1:i*L+L);
temp=fft(xw,nfft);
temp=fftshift(temp);
TF(i,:)=temp;
end
subplot(223);
mesh(abs(TF));
subplot(224);
contour(abs(TF));
axis equal tight % set the axes aspect ratio
grid;