matlab中fft的用法及注意事项

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

matlab的FFT函数

相关语法:

Y=fft(X)

Y=fft(X,n)

Y=fft(X,[],dim)

Y=fft(X,n,dim)

定义如下:

相关的一个例子:

Fs=1000;%采样频率

T=1/Fs;%采样时间

L=1000;%总的采样点数

t=(0:L-1)*T;%时间序列(时间轴)

%产生一个幅值为0.7频率为50HZ正弦+另外一个信号的幅值为1频率为120Hz的正弦信号x=0.7*sin(2*pi*50*t)+sin(2*pi*120*t);

y=x+2*randn(size(t));%混入噪声信号

plot(Fs*t(1:50),y(1:50))%画出前50个点

title('Signal Corrupted with Zero-Mean Random Noise')

xlabel('time(milliseconds)')

NFFT=2^nextpow2(L);%求得最接近总采样点的2^n,这里应该是2^10=1024

Y=fft(y,NFFT)/L;%进行fft变换(除以总采样点数,是为了后面精确看出原始信号幅值)f=Fs/2*linspace(0,1,NFFT/2+1);%频率轴(只画到Fs/2即可,由于y为实数,后面一半是对称的)

%画出频率幅度图形,可以看出50Hz幅值大概0.7,120Hz幅值大概为1.

plot(f,2*abs(Y(1:NFFT/2+1)))

title('Single-Sided Amplitude Spectrum of y(t)')

xlabel('Frequency(Hz)')

ylabel('|Y(f)|')

主要有两点注意的地方:

1、从公式上看,matlab的fft序号是从1到N,但是绝大多数教材上是从0到N-1。

2、2、Y=fft(x)之后,这个Y是一个复数,它的模值应该除以(length(x)2),才能得到各个频率信号实际幅值。

fftshift

fftshift

作用:将零频点移到频谱的中间

用法:

Y=fftshift(X)

Y=fftshift(X,dim)

描述:fftshift移动零频点到频谱中间,重新排列fft,fft2和fftn的输出结果。将零频点放到频谱的中间对于观察傅立叶变换是有用的。

示例:

clf;

fs=100;N=256;%采样频率和数据点数

n=0:N-1;t=n/fs;%时间序列

x=0.5*sin(2*pi*15*t)+2*sin(2*pi*40*t);%信号

y1=fft(x,N);%对信号进行快速Fourier变换

y2=fftshift(y1);

mag1=abs(y1);%求得Fourier变换后的振幅

mag2=abs(y2);

f1=n*fs/N;%频率序列

f2=n*fs/N-fs/2;

subplot(3,1,1),plot(f1,mag1,'r');%绘出随频率变化的振幅

xlabel('频率/Hz');

ylabel('振幅');title('图1:usual FFT','color','r');grid on;

subplot(3,1,2),plot(f2,mag1,'b');%绘出随频率变化的振幅

xlabel('频率/Hz');

ylabel('振幅');title('图2:FFT without fftshift','color','b');grid on;

subplot(3,1,3),plot(f2,mag2,'c');%绘出随频率变化的振幅

xlabel('频率/Hz');

ylabel('振幅');title('图3:FFT after fftshift','color','c');grid on;

结论:

1)如果期望绘制的幅频图的频率范围为0~fs,则无需运行fftshift变换,如图1。2)如果期望绘制的幅频图的频率范围为-fs/2~fs/2,则需要运行fftshift变换,如图3;如果不变换,图示的响应频点会发生变换,如图2。

MATLAB中的fft后为何要用fftshift?

fft是一维傅里叶变换,即将时域信号转换位频域

fftshift

是针对频域的,将FFT的DC分量移到频谱中心

即对频域的图像,(假设用一条水平线和一条垂直线将频谱图分成四块)对这四块进行对角线的交换与反对角线的交换

FFTSHIFT Shift zero-frequency component to center of spectrum.

For vectors,FFTSHIFT(X)swaps(交换)the left and right halves of

X.For matrices,FFTSHIFT(X)swaps the first and third

quadrants and the second and fourth quadrants.For N-D

arrays,FFTSHIFT(X)swaps"half-spaces"of X along each

dimension.

FFTSHIFT(X,DIM)applies the FFTSHIFT operation along the

dimension DIM.

FFTSHIFT is useful for visualizing the Fourier transform with

the zero-frequency component in the middle of the spectrum.

fftshift就是对换数据的左右两边比如

x=[1234]

fftshift(x)->[3412]

IFFTSHIFT Inverse FFT shift.(就是fftshift的逆)

x=[12345];

y=fftshift(x)

y=

45123

ifftshift(y)

ans=

12345

For vectors,IFFTSHIFT(X)swaps the left and right halves of

X.For matrices,IFFTSHIFT(X)swaps the first and third

quadrants and the second and fourth quadrants.For N-D

arrays,IFFTSHIFT(X)swaps"half-spaces"of X along each

dimension.

IFFTSHIFT(X,DIM)applies the IFFTSHIFT operation along the

dimension DIM.

IFFTSHIFT undoes the effects of FFTSHIFT.

直接用fft得出的数据与频率不是对应的,fftshift可以纠正过来

以下是Matlab的帮助文件中对fftshift的说明:

Y=fftshift(X)rearranges the outputs of fft,fft2,and fftn by moving the zero-frequency component to the center of the array.It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum.For vectors,fftshift(X)swaps the left and right halves of X.

相关文档
最新文档