R=0.1;
t=-2:R:2;
ft=[zeros(1,10),ones(1,21),zeros(1,10)];
W1=10*pi;
N=500;k=0;N;w=k*W1/N;
Fw=ft.*exp(-1i*t'*w)*R;
FRw=abs(Fw);
W=[-fliplr(w),w(2:501)];
FW=[fliplr(FRw),FRw(2:501)];
subplot(2,1,1);
plot(t,ft);
axis([-5 5 -0.5 1.5]);
grid on;
xlabel('t');ylabel('f(t)');
title('f(t)=u(t+1)-u(t-1)');
subplot(2,1,2);
plot(W,FW);
grid on;
xlabel('W');ylabel('F(W)');
title('f(t)的振幅频谱图');