MATLAB-FFT
e小白网址:www.e-xiaobai.com
%FFT实验数据为对连续信号4*cos(2*pi*35*t)+5*sin(2*pi*60*t)采样所得
%采样时间间隔为0.005(根据奈奎斯特采样定理而定),共采集1001个数据
%采样得到的数据保存在FFT实验数据.txt文件内
% t=0.005:0.005:5;
% y=4*cos(2*pi*35*t)+5*sin(2*pi*60*t);
filename='FFT实验数据.txt';
fileID=fopen(filename);
C=textscan(fileID,'%f %f','HeaderLines',1);
fclose(fileID);
t=C{1,1};
y=C{1,2};
N=length(t);%数据个数
T=t(2,1)-t(1,1);%时间间隔
fs=1/T;%采样频率
figure(1)
subplot(2,1,1)
plot(t,y)
title('时域')
xlabel('秒')
%FFT
%NFFT=N;
NFFT=2^nextpow2(N);
y1=fft(y,NFFT);
f=(0:NFFT-1)/NFFT*fs;
y1(1,1)=abs(y1(1,1))/2;%直流量除以2
subplot(2,1,2)
plot(f(1:NFFT/2+1),abs(y1(1:NFFT/2+1))*2/NFFT)
xlabel('赫兹')
ylabel('幅度')
title('幅频谱')

注:FFT实验数据.txt文件可在e小白官网《MATLAB-FFT》文章中下载。