还剩2页未读,继续阅读
文本内容:
如何使用matlab进行频域分析Matlab可以说是一个非常有用且功能齐全的工具,在通信、自控、金融等方面有广泛的应用本文讨论使用Matlab对信号进行频域分析的方法说到频域,不可避免的会提到傅里叶变换,傅里叶变换提供了一个将信号从时域转变到频域的方法之所以要有信号的频域分析,是因为很多信号在时域不明显的特征可以在频域下得到很好的展现,可以更加容易的进行分析和处理FFTMatlab提供的傅里叶变换的函数是FFT,中文名叫做快速傅里叶变换快速傅里叶变换的提出是伟大的,使得处理器处理数字信号的能力大大提升,也使我们生活向数字化迈了一大步接下来就谈谈如何使用这个函数fft使用很简单,但是一般信号都有x和y两个向量,而fft只会处理y向量,所以想让频域分析变得有意义,那么就需要用户自己处理x向量一个简单的例子从一个简单正弦信号开始吧,正弦信号定义为y t=2sin网2冗f to我们现在通过以下代码在Matlab中画出这个正弦曲线1fo=4;%frequency of the sinewave2Fs=100;%sampling rate3Ts=1/Fs;%sampling timeinterval4t=0:Ts:l_Ts;%sampling period5n=length t;%number ofsamples6y=2*sin2*pi*fo*t;%the sinecurve7%plot thecosine curvein thetime domain8sinePlot=figure;9plot t,y10xlabel,time seconds,11ylabelC ytJ12titleC,Sample SineWave13grid这就是我们得到的:当我们对这条曲线fft时,我们希望在频域得到以下频谱(基于傅里叶变换理论,我们希望看见一个幅值为1的峰值在-4Hz处,另一个在+4Hz处)
①P2=dEFreq Hz使用FFT命令我们知道目标是什么了,那么现在使用Matlab的内建的FFT函数来重新生成频谱1%plot the frequency spectrumusing theMATLAB fft command2matlabFFT=figure;%crcate anew figure3YfreqDomain=ffty;%take thefft ofour sinwave,yt4stemabsYfreqDomain;%use abscommand to get the magnitude5%similary,we woulduse anglecommand toget thephase plot!6如e11discuss phasein anotherpost though!7xlabel,Sample Number8ylabel,Amplitude9title,Using theMat labfft command10grid12013axis[0,100,0,120]效果如下:01020U30sing th4e0Matla5b0fftco6m0mand708090100Sample Number但是注意一下,这并不是我们真正想要的,有一些信息是缺失的•X轴本来应该给我们提供频率信息,但是你能读出频率吗?
①三0pndE4080604020,幅度都是100•没有让频谱中心为0为FFT定义一个函数来获取双边频谱以下代码可以简化获取双边频谱的过程,复制并保存到你的.m文件中1function[X,freq]=centeredFFTx,Fs2%this isa customfunction thathelps inplotting thetwo-sided spectrum3%x isthe signalthat isto betransformed4%Fs isthe samplingrate5N=lengthx;6%this partofthecode generatesthat frequencyaxis7if modN,2=08k=-N/2:N/2-l;%N even9else10k=N-l/2:N-D/2;%N odd11end12T=N/Fs;13freq=k/T;%thefrequencyaxis14%takes thefft ofthe signal,and adjuststhe amplitudeaccordingly15X=fftx/N;%normalize thedata16X=fftshiftX;%shifts thefft dataso thatit iscentered这个函数输出正确的频域范围和变换后的信号,它需要输入需要变换的信号和采样率接下来使用前文的正弦信号做一个简单的示例,注意你的示例.ni文件要和centeredFFT.m文件在一个目录下17[YfreqDomain,frequencyRange]=centeredFFTy,Fs;18centeredFFT=figure;19%remember totake theabs ofYfreqDomai ntoget themagnitude!20stemfrequencyRange,absYfreqDomain;21xlabel Freq Hz22ylabel Amplitude23title,Using thecenteredFFT function*24grid25axis[-6,6,0,
1.5]效果如下:这张图就满足了我们的需求,我们得到了在+4和-4处的峰值,而且幅值为
1.为FFT定义一个函数来获取右边频谱从上图可以看出,FFT变换得到的频谱是左右对称的,因此,我们只需要其中一边就能获得信号的所有信息,我们一般保留正频率一侧
①Freq Hzpm--dE4以下的函数对上面的自定义函数做了一些修改,让它可以帮助我们只画出信号的正频率一侧1function[X,freq]=positiveFFTx,Fs2N=lengthx;%get thenumber ofpoints3k=0:N-l;%create avector from0to N-l4T=N/Fs;%getthefrequency interval5freq=k/T;%create thefrequency range6X=fftx/N;%normalize thedata7%only wantthe firsthalf ofthe FFT,since itis redundant8cutOff=ceil N/2;9%take onlythe firsthalf ofthe spectrum10X=Xl:cutOff;11freq=freq1:cutOff;和前面一样,使用正弦信号做一个示例,下面是示例代码1[YfreqDomain,frequencyRange]=positiveFFTy,Fs;2positiveFFT=figure;3stemfrequencyRange,absYfreqDomain;4setpositiveFFT,*Position,[500,500,500,300]5xlabel*FreqHz6ylabel Amplitude7Using thepositiveFFF function8grid9axis[0,20,0,
1.5]效果如下
①进入朱浩博客pm--dE。
个人认证
优秀文档
获得点赞 0