新聞中心

        EEPW首頁 > 設計應用 > 使用Matlab對信號進行頻域分析的方法

        使用Matlab對信號進行頻域分析的方法

        作者: 時間:2018-08-29 來源:網絡 收藏

          可以說是一個非常有用且功能齊全的工具,在通信、自控、金融等方面有廣泛的應用。

        本文引用地址:http://www.104case.com/article/201808/391336.htm

          本文討論使用對信號進行分析的方法。

          說到,不可避免的會提到傅里葉變換,傅里葉變換提供了一個將信號從時域轉變到的方法。之所以要有信號的頻域分析,是因為很多信號在時域不明顯的特征可以在頻域下得到很好的展現,可以更加容易的進行分析和處理。

          FFT

          提供的傅里葉變換的函數是FFT,中文名叫做快速傅里葉變換。快速傅里葉變換的提出是偉大的,使得處理器處理數字信號的能力大大提升,也使我們生活向數字化邁了一大步。

          接下來就談談如何使用這個函數。

          fft使用很簡單,但是一般信號都有x和y兩個向量,而fft只會處理y向量,所以想讓頻域分析變得有意義,那么就需要用戶自己處理x向量

          一個簡單的例子

          從一個簡單正弦信號開始吧,正弦信號定義為:



          我們現在通過以下代碼在Matlab中畫出這個正弦曲線

          fo = 4; %frequency of the sine wave

          Fs = 100; %sampling rate

          Ts = 1/Fs; %sampling time interval

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

          n = length(t); %number of samples

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

          %plot the cosine curve in the time domain

          sinePlot = figure;

          plot(t,y)

          xlabel('time (seconds)')

          ylabel('y(t)')

          title('Sample Sine Wave')

          grid

          這就是我們得到的:



          當我們對這條曲線fft時,我們希望在頻域得到以下頻譜(基于傅里葉變換理論,我們希望看見一個幅值為1的峰值在-4Hz處,另一個在+4Hz處)



          使用FFT命令

          我們知道目標是什么了,那么現在使用Matlab的內建的FFT函數來重新生成頻譜

          %plot the frequency spectrum using the MATLAB fft command

          matlabFFT = figure; %create a new figure

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

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

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

          %we'll discuss phase in another post though!

          xlabel('Sample Number')

          ylabel('Amplitude')

          title('Using the Matlab fft command')

          grid

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

          效果如下:



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

          x軸本來應該給我們提供頻率信息,但是你能讀出頻率嗎?

          幅度都是100

          沒有讓頻譜中心為

          為FFT定義一個函數來獲取雙邊頻譜

          以下代碼可以簡化獲取雙邊頻譜的過程,復制并保存到你的.m文件中

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

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

          %x is the signal that is to be transformed

          %Fs is the sampling rate

          N=length(x);

          %this part of the code generates that frequency axis

          if mod(N,2)==0

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

          else

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

          end

          T=N/Fs;

          freq=k/T; %the frequency axis

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

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

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

          這個函數輸出正確的頻域范圍和變換后的信號,它需要輸入需要變換的信號和采樣率。

          接下來使用前文的正弦信號做一個簡單的示例,注意你的示例.m文件要和centeredFFT.m文件在一個目錄下

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

          centeredFFT = figure;

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

          stem(frequencyRange,abs(YfreqDomain));

          xlabel('Freq (Hz)')

          ylabel('Amplitude')

          title('Using the centeredFFT function')

          grid

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

          效果如下:



          這張圖就滿足了我們的需求,我們得到了在+4和-4處的峰值,而且幅值為1.

          為FFT定義一個函數來獲取右邊頻譜

          從上圖可以看出,FFT變換得到的頻譜是左右對稱的,因此,我們只需要其中一邊就能獲得信號的所有信息,我們一般保留正頻率一側。

          以下的函數對上面的自定義函數做了一些修改,讓它可以幫助我們只畫出信號的正頻率一側

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

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

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

          T=N/Fs; %get the frequency interval

          freq=k/T; %create the frequency range

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

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

          cutOff = ceil(N/2);

          %take only the first half of the spectrum

          X = X(1:cutOff);

          freq = freq(1:cutOff);

          和前面一樣,使用正弦信號做一個示例,下面是示例代碼

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

          positiveFFT = figure;

          stem(frequencyRange,abs(YfreqDomain));

          set(positiveFFT,'Position',[500,500,500,300])

          xlabel('Freq (Hz)')

          ylabel('Amplitude')

          title('Using the positiveFFT function')

          grid

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

          效果如下:




        關鍵詞: Matlab 頻域

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 互助| 曲周县| 崇仁县| 鹤壁市| 闻喜县| 旺苍县| 安丘市| 五指山市| 北票市| 湘潭县| 桂平市| 赞皇县| 浙江省| 漯河市| 望城县| 东山县| 德保县| 巫溪县| 桐梓县| 上思县| 凤凰县| 永春县| 浦城县| 赞皇县| 两当县| 信宜市| 连江县| 环江| 九寨沟县| 丽水市| 兴海县| 宝清县| 肃宁县| 信丰县| 安顺市| 荃湾区| 临汾市| 汉川市| 蕲春县| 临高县| 佛山市|