在NumPy中使用FFT时的频率单位 [英] Units of frequency when using FFT in NumPy
问题描述
我正在NumPy中使用FFT函数来进行一些信号处理.我有一个名为signal
的数组
每小时有一个数据点,总共有576个数据点.我在signal
上使用以下代码来查看其傅立叶变换.
I am using the FFT function in NumPy to do some signal processing. I have array called signal
which has one data point for each hour and has a total of 576 data points. I use the following code on signal
to look at its fourier transform.
t = len(signal)
ft = fft(signal,n=t)
mgft=abs(ft)
plot(mgft[0:t/2+1])
我看到了两个峰值,但是我不确定x轴的单位是什么,即它们如何映射到小时上?任何帮助将不胜感激.
I see two peaks but I am unsure as to what the units of the x axis are i.e., how they map onto hours? Any help would be appreciated.
推荐答案
给出采样率FSample
并转换块大小N
,您可以计算频率分辨率deltaF
,采样间隔deltaT
和总捕获时间capT
使用以下关系:
Given sampling rate FSample
and transform blocksize N
, you can calculate the frequency resolution deltaF
, sampling interval deltaT
, and total capture time capT
using the relationships:
deltaT = 1/FSample = capT/N
deltaF = 1/capT = FSample/N
还请记住,FFT将值从0
返回到FSample
,或者等效地从-FSample/2
返回到FSample/2
.在您的绘图中,您已经将-FSample/2
放到0
部分. NumPy包括一个帮助程序函数,可以为您计算所有这些信息: fftfreq .
Keep in mind also that the FFT returns value from 0
to FSample
, or equivalently -FSample/2
to FSample/2
. In your plot, you're already dropping the -FSample/2
to 0
part. NumPy includes a helper function to calculate all this for you: fftfreq.
对于deltaT = 1 hour
和N = 576
的值,您得到deltaF = 0.001736 cycles/hour = 0.04167 cycles/day
,从-0.5 cycles/hour
到0.5 cycles/hour
.因此,如果您在bin 48(和bin 528)处有一个幅度峰值,则它对应于48*deltaF = 0.0833 cycles/hour = 2 cycles/day.
For your values of deltaT = 1 hour
and N = 576
, you get deltaF = 0.001736 cycles/hour = 0.04167 cycles/day
, from -0.5 cycles/hour
to 0.5 cycles/hour
. So if you have a magnitude peak at, say, bin 48 (and bin 528), that corresponds to a frequency component at 48*deltaF = 0.0833 cycles/hour = 2 cycles/day.
通常,您应在计算FFT之前对您的时域数据应用窗口函数,以减少光谱泄漏. Hann窗口几乎从来都不是一个坏选择.您也可以使用rfft
功能跳过输出的-FSample/2, 0
部分.因此,您的代码将是:
In general, you should apply a window function to your time domain data before calculating the FFT, to reduce spectral leakage. The Hann window is almost never a bad choice. You can also use the rfft
function to skip the -FSample/2, 0
part of the output. So then, your code would be:
ft = np.fft.rfft(signal*np.hanning(len(signal)))
mgft = abs(ft)
xVals = np.fft.fftfreq(len(signal), d=1.0) # in hours, or d=1.0/24 in days
plot(xVals[:len(mgft)], mgft)
这篇关于在NumPy中使用FFT时的频率单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!