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屋!