自定义函数
function [AW,f]=cftbyfft(wt,t,flag)
if nargin==2;flag=1;end
N=length(t);
T=t(length(t))-t(1);
dt=T/N;
W0=fft(wt);
W=dt*W0;
df=1/T;
n=0:1:(N-1);
if flag==0
n=-N/2:(N/2-1);
W=fftshift(W);
end
f=n*df;
AW=abs(W);
if nargout==0
plot(f,AW);grid,xlabel('频率f');ylabel('|w(f)|')
end
调用自定义函数
M=5;
tend=1;
T=10;
N=2^M;
dt=T/N;
n=0:N-1;
t=n*dt;
w=zeros(size(t,2),1);
Tow=find((tend-t)>0);
w(Tow,1)=ones(length(Tow),1);%
plot(t,w,'b','LineWidth',2.5),title('Time Waveform');xlabel('t --- >')
[AW,f]=cftbyfft(w,t,0);
ff=f+eps;
AWW=abs(sin(pi*ff)./(pi*ff));
plot(f,AW,'b-',ff,AWW,'r:')
title('Aliasing caused by undersampling')
xlabel('f --- >');ylabel('|W(f)|'),legend('by FFT','Theoretical') |