2016-03-01 63 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