Scipy/Numpy FFT频率分析 [英] Scipy/Numpy FFT Frequency Analysis
问题描述
我正在寻找如何将fft(通过scipy.fftpack.fftfreq获取)中的频率轴转换为以赫兹为单位的频率,而不是将bin或分数bin转换为
I'm looking for how to turn the frequency axis in a fft (taken via scipy.fftpack.fftfreq) into a frequency in Hertz, rather than bins or fractional bins.
我尝试在下面进行编码以测试FFT:
I tried to code below to test out the FFT:
t = scipy.linspace(0,120,4000)
acc = lambda t: 10*scipy.sin(2*pi*2.0*t) + 5*scipy.sin(2*pi*8.0*t) + 2*scipy.random.random(len(t))
signal = acc(t)
FFT = abs(scipy.fft(signal))
FFT = scipy.fftpack.fftshift(FFT)
freqs = scipy.fftpack.fftfreq(signal.size)
pylab.plot(freqs,FFT,'x')
pylab.show()
采样率应为4000个样本/120秒= 33.34个样本/秒.
The sampling rate should be 4000 samples / 120 seconds = 33.34 samples/sec.
该信号具有2.0 Hz信号,8.0 Hz信号和一些随机噪声.
The signal has a 2.0 Hz signal, a 8.0 Hz signal, and some random noise.
我进行FFT,获取频率并将其绘制出来.这些数字很荒谬.如果我将频率乘以33.34(采样频率),则会得到大约8 Hz和15 Hz的峰值,这似乎是错误的(而且,这些频率应该相差4倍,而不是2倍!).
I take the FFT, grab the frequencies, and plot it. The numbers are pretty nonsensical. If I multiply the frequencies by 33.34 (the sampling frequency), then I get peaks at about 8 Hz and 15 Hz, which seems wrong (also, the frequencies should be a factor of 4 apart, not 2!).
对我在这里做错什么有任何想法吗?
Any thoughts on what I'm doing wrong here?
推荐答案
我认为您不需要执行fftshift(),并且可以将采样周期传递给fftfreq():
I think you don't need to do fftshift(), and you can pass sampling period to fftfreq():
import scipy
import scipy.fftpack
import pylab
from scipy import pi
t = scipy.linspace(0,120,4000)
acc = lambda t: 10*scipy.sin(2*pi*2.0*t) + 5*scipy.sin(2*pi*8.0*t) + 2*scipy.random.random(len(t))
signal = acc(t)
FFT = abs(scipy.fft(signal))
freqs = scipy.fftpack.fftfreq(signal.size, t[1]-t[0])
pylab.subplot(211)
pylab.plot(t, signal)
pylab.subplot(212)
pylab.plot(freqs,20*scipy.log10(FFT),'x')
pylab.show()
从图中可以看到在2Hz和8Hz处有两个峰值.
from the graph you can see there are two peak at 2Hz and 8Hz.
这篇关于Scipy/Numpy FFT频率分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!