2016-03-01 96 views
2

我想了解如何在matlab中的FFT工作,特別是如何定義頻率範圍來繪製它。碰巧,我已經閱讀了matlab的幫助鏈接和其他討論,我想(猜)我對此感到困惑。 在matlab鏈路: http://es.mathworks.com/help/matlab/math/fast-fourier-transform-fft.html 它們限定這樣的頻率範圍爲:Matlab,FFT頻率範圍的差異還是相同嗎?

f = (0:n-1)*(fs/n) 

nfs爲:

n = 2^nextpow2(L); % Next power of 2 from length of signal x 
fs = N/T; % N number of samples in x and T the total time of the recorded signal 

但是,在另一方面,在過去後Understanding Matlab FFT example (基於之前版本的matlab),得到的頻率範圍定義爲:

f = fs/2*linspace(0,1,NFFT/2+1); 

NFFT如上述n(信號長度爲x的下一次冪)。 那麼,基於此,這些不同的向量(方程1和最終方程)如何可以相同? 如果你可以看到,向量是不同的,因爲前者有n分,而後者有NFFT/2分!實際上,因子(fs/n)fs/2不同。

回答

1

這個混淆可能是由於你所引用的兩個例子不同而產生的結果。請參閱下面的代碼以獲取本說明中的參考資料。

在第一個示例中,該圖是頻率範圍內的功率譜(週期圖)。請注意,在第一個曲線圖中,週期圖並未居中於0,這意味着頻率範圍似乎是奈奎斯特採樣頻率的兩倍。正如在數學工具鏈接中提到的那樣,爲了避免這種混淆(圖2),將週期圖居中爲0是很常見的做法。

對於第二個例子,採用相同的參數,原始圖是具有不同歸一化的傅立葉光譜的振幅,比第一個例子(圖3)。使用Matlab完整頻率排序的語法(如代碼中所述),將這個看起來不同的fft結果轉換爲示例1的結果是微不足道的;在圖4中複製了0中心週期圖的相同結果。

因此,具體回答你的問題,在這兩種情況下的頻率範圍是相同的,最大頻率爲奈奎斯特採樣頻率爲:瞭解如何DFFT作品

f = fs/2*linspace(0,1,NFFT/2+1); 

的關鍵(同樣在Matlab中)的理解是,您只需將您的離散數據集投影到傅立葉空間,其中由matlab中的fft()函數返回的是每個頻率分量的展開係數,以及給出係數(在Matlab中如例2):

f = [f(1:end-1) -fliplr(f(1,2:end))]; 

瞭解更多詳細信息,請參見在DFT的維基百科頁面: https://en.wikipedia.org/wiki/Discrete_Fourier_transform

這也可能有助於您拿FFT省略長度爲2參數的功率

y = fft(x). 

在這情況下,你會看到y中只有少數非零分量對應於你的輸入信號的確切係數。數學工作頁面聲稱以下作爲使用或不使用此長度的動機:

「對變換長度使用兩個冪來優化FFT算法,但實際上通常使用n的執行時間差別很小= m「。

%% First example: 
% http://www.mathworks.com/help/matlab/math/fast-fourier-transform-fft.html 

fs = 10;        % Sample frequency (Hz) 
t = 0:1/fs:10-1/fs;      % 10 sec sample 
x = (1.3)*sin(2*pi*15*t) ...    % 15 Hz component 
    + (1.7)*sin(2*pi*40*(t-2));   % 40 Hz component 
% Removed the noise 

m = length(x);   % Window length 
n = pow2(nextpow2(m)); % Transform length 
y = fft(x,n);   % DFT 
f = (0:n-1)*(fs/n);  % Frequency range 
power = y.*conj(y)/n; % Power of the DFT 

subplot(2,2,1) 
plot(f,power,'-o') 
xlabel('Frequency (Hz)') 
ylabel('Power') 
title('{\bf Periodogram}') 

y0 = fftshift(y);   % Rearrange y values 
f0 = (-n/2:n/2-1)*(fs/n); % 0-centered frequency range 
power0 = y0.*conj(y0)/n; % 0-centered power 

subplot(2,2,2) 
plot(f0,power0,'-o') 
% plot(f0,sqrt_power0,'-o') 
xlabel('Frequency (Hz)') 
ylabel('Power') 
title('{\bf 0-Centered Periodogram} Ex. 1') 

%% Second example: 
% http://stackoverflow.com/questions/10758315/understanding-matlab-fft-example 

% Let's redefine the parameters for consistency between the two examples 

Fs = fs;      % Sampling frequency 
% T = 1/Fs;     % Sample time (not required) 
L = m;      % Length of signal 
% t = (0:L-1)*T;    % Time vector (as above) 
% % Sum of a 3 Hz sinusoid and a 2 Hz sinusoid 
% x = 0.7*sin(2*pi*3*t) + sin(2*pi*2*t); %(as above) 

NFFT = 2^nextpow2(L); % Next power of 2 from length of y 
         % NFFT == n (from above) 
Y = fft(x,NFFT)/L; 
f = Fs/2*linspace(0,1,NFFT/2+1); 

% Plot single-sided amplitude spectrum. 
subplot(2,2,3) 
plot(f,2*abs(Y(1:NFFT/2+1)),'-o') 
title('Single-Sided Amplitude Spectrum of y(t)') 
xlabel('Frequency (Hz)') 
ylabel('|Y(f)|') 


% Get the 0-Centered Periodogram using the parameters of the second example 
f = [f(1:end-1) -fliplr(f(1,2:end))]; % This is the frequency ordering used 
             % by the full fft in Matlab 

power = (Y*L).*conj(Y*L)/NFFT; 

% Rearrange for nicer plot 
ToPlot = [f; power]; [~,ind] = sort(f); 
ToPlot = ToPlot(:,ind); 

subplot(2,2,4) 
plot(ToPlot(1,:),ToPlot(2,:),'-o') 
xlabel('Frequency (Hz)') 
ylabel('Power') 
title('{\bf 0-Centered Periodogram} Ex. 2') 
+0

親愛的PZwan,謝謝。其實這是一個完整的答案和一個非常有用的代碼來理解你的解釋和matlab的DFT和fft的一般事情。 – Gohann

+0

親愛的Gohann,這是我的榮幸。我很高興這對你有幫助。 – PZwan

2

因此,基於的是,如何將這些不同的向量(公式1和公式決賽)可能是一樣的嗎?

example in the documentation from Mathworks繪製了FFT的整個n點輸出。這涵蓋從0到幾乎fs(正好是(n-1)/n * fs)的頻率。然後,他們進行以下觀察(有效的實數輸入到FFT):

的頻率範圍(從0到奈奎斯特頻率fs/2)的前半部分是足以識別分量頻率中的數據,因爲下半場只是上半場的反映。

other post you refer to只是選擇不顯示多餘的下半部分。然後它使用覆蓋頻率範圍一半的點數的一半。

實際上,因子(fs/n)與fs/2不同。

也許,它的意義最簡單的方法是將兩個表達式的輸出比較的n一些小的價值,說n=8和設置fs=1(因爲fs乘兩個表達式)。一方面,第一表達[0:n-1]*(fs/n)的輸出將是:

0.000 0.125 0.250 0.500 0.625 0.750 0.875 

而的fs/2*linspace(0,1,n/2+1)輸出將是:

0.000 0.125 0.250 0.500 

正如你可以看到一組頻率完全相同達奈奎斯特頻率fs/2

+0

飛機和簡單。感謝您的解釋SleuthEye – Gohann