怎么用matlab进行频域分析法,如何用Matlab进行频域分析?

Matlab可以说是一个非常有用且功能齐全的工具,在通信、自控、金融等方面有广泛的应用。

本文讨论使用Matlab对信号进行频域分析的方法。

说到频域,不可避免的会提到傅里叶变换,傅里叶变换提供了一个将信号从时域转变到频域的方法。之所以要有信号的频域分析,是因为很多信号在时域不明显的特征可以在频域下得到很好的展现,可以更加容易的进行分析和处理。

FFT

Matlab提供的傅里叶变换的函数是FFT,中文名叫做快速傅里叶变换。快速傅里叶变换的提出是伟大的,使得处理器处理数字信号的能力大大提升,也使我们生活向数字化迈了一大步。

接下来就谈谈如何使用这个函数。

fft使用很简单,但是一般信号都有x和y两个向量,而fft只会处理y向量,所以想让频域分析变得有意义,那么就需要用户自己处理x向量

一个简单的例子

从一个简单正弦信号开始吧,正弦信号定义为:

y(t)=2sin⁡(2πf0t)

我们现在通过以下代码在Matlab中画出这个正弦曲线

1  fo = 4;   %frequency of the sine wave

2  Fs = 100; %sampling rate

3  Ts = 1/Fs; %sampling time interval

4  t = 0:Ts:1-Ts; %sampling period

5  n = length(t); %number of samples

6  y = 2*sin(2*pi*fo*t); %the sine curve

7

8  %plot the cosine curve in the time domain

9  sinePlot = figure;

10  plot(t,y)

11  xlabel('time (seconds)')

12  ylabel('y(t)39;)

13  title('Sample Sine Wave')

14  grid

这就是我们得到的:

怎么用matlab进行频域分析法,如何用Matlab进行频域分析?_第1张图片

当我们对这条曲线fft时,我们希望在频域得到以下频谱(基于傅里叶变换理论,我们希望看见一个幅值为1的峰值在-4Hz处,另一个在+4Hz处)

怎么用matlab进行频域分析法,如何用Matlab进行频域分析?_第2张图片

使用FFT命令

我们知道目标是什么了,那么现在使用Matlab的内建的FFT函数来重新生成频谱

1  %plot the frequency spectrum using the MATLAB fft command

2  matlabFFT = figure;  %create a new figure

3  YfreqDomain = fft(y); %take the fft of our sin wave, y(t)

4

5  stem(abs(YfreqDomain));  %use abs command to get the magnitude

6  %similary, we would use angle command to get the phase plot!

7  %we'll discuss phase in another post though!

8

9  xlabel('Sample Number')

10  ylabel('Amplitude39;)

11  title('Using the Matlab fft command')

12  grid

13  axis([0,100,0,120])

效果如下:

怎么用matlab进行频域分析法,如何用Matlab进行频域分析?_第3张图片

但是注意一下,这并不是我们真正想要的,有一些信息是缺失的

·x轴本来应该给我们提供频率信息,但是你能读出频率吗?

·幅度都是100

·没有让频谱中心为0

为FFT定义一个函数来获取双边频谱

以下代码可以简化获取双边频谱的过程,复制并保存到你的.m文件中

1  function [X,freq]=centeredFFT(x,Fs)

2  %this is a custom function that helps in plotting the two-sided spectrum

3  %x is the signal that is to be transformed

4  %Fs is the sampling rate

5

6  N=length(x);

7

8  %this part of the code generates that frequency axis

9  if mod(N,2)==0

10    k=-N/2:N/2-1; % N even

11  else

12    k=-(N-1)/2:(N-1)/2; % N odd

13  end

14  T=N/Fs;

15  freq=k/T;  %the frequency axis

16

17  %takes the fft of the signal, and adjusts the amplitude accordingly

18  X=fft(x)/N; % normalize the data

19  X=fftshift(X); %shifts the fft data so that it is centered

这个函数输出正确的频域范围和变换后的信号,它需要输入需要变换的信号和采样率。

接下来使用前文的正弦信号做一个简单的示例,注意你的示例.m文件要和centeredFFT.m文件在一个目录下

1  [YfreqDomain,frequencyRange] = centeredFFT(y,Fs);

2  centeredFFT = figure;

3

4  %remember to take the abs of YfreqDomain to get the magnitude!

5  stem(frequencyRange,abs(YfreqDomain));

6  xlabel('Freq (Hz)')

7  ylabel('Amplitude39;)

8  title('Using the centeredFFT function')

9  grid

10  axis([-6,6,0,1.5])

效果如下:

怎么用matlab进行频域分析法,如何用Matlab进行频域分析?_第4张图片

这张图就满足了我们的需求,我们得到了在+4和-4处的峰值,而且幅值为1.

为FFT定义一个函数来获取右边频谱

从上图可以看出,FFT变换得到的频谱是左右对称的,因此,我们只需要其中一边就能获得信号的所有信息,我们一般保留正频率一侧。

以下的函数对上面的自定义函数做了一些修改,让它可以帮助我们只画出信号的正频率一侧

1  function [X,freq]=positiveFFT(x,Fs)

2  N=length(x); %get the number of points

3  k=0:N-1;     %create a vector from 0 to N-1

4  T=N/Fs;      %get the frequency interval

5  freq=k/T;    %create the frequency range

6  X=fft(x)/N; % normalize the data

7

8  %only want the first half of the FFT, since it is redundant

9  cutOff = ceil(N/2);

10

11  %take only the first half of the spectrum

12  X = X(1:cutOff);

13  freq = freq(1:cutOff);

和前面一样,使用正弦信号做一个示例,下面是示例代码

1  [YfreqDomain,frequencyRange] = positiveFFT(y,Fs);

2  positiveFFT = figure;

3  stem(frequencyRange,abs(YfreqDomain));

4  set(positiveFFT,'Position39;,[500,500,500,300])

5  xlabel('Freq (Hz)')

6  ylabel('Amplitude39;)

7  title('Using the positiveFFT function')

8  grid

9  axis([0,20,0,1.5])

效果如下:

怎么用matlab进行频域分析法,如何用Matlab进行频域分析?_第5张图片

你可能感兴趣的:(怎么用matlab进行频域分析法,如何用Matlab进行频域分析?)