简体   繁体   English

Scipy / Numpy FFT频率分析

[英]Scipy/Numpy FFT Frequency Analysis

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中的频率轴(通过scipy.fftpack.fftfreq获取)转换为赫兹频率,而不是箱子或分数箱。

I tried to code below to test out the FFT: 我尝试在下面编写代码来测试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()

The sampling rate should be 4000 samples / 120 seconds = 33.34 samples/sec. 采样率应为4000个样本/ 120秒= 33.34个样本/秒。

The signal has a 2.0 Hz signal, a 8.0 Hz signal, and some random noise. 该信号具有2.0 Hz信号,8.0 Hz信号和一些随机噪声。

I take the FFT, grab the frequencies, and plot it. 我采用FFT,抓取频率并绘制它。 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!). 如果我将频率乘以33.34(采样频率),那么我会得到大约8 Hz和15 Hz的峰值,这似乎是错误的(同样,频率应该是4倍,而不是2!)。

Any thoughts on what I'm doing wrong here? 对我在这里做错了什么的想法?

I think you don't need to do fftshift(), and you can pass sampling period to fftfreq(): 我认为你不需要做fftshift(),你可以将采样周期传递给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()

from the graph you can see there are two peak at 2Hz and 8Hz. 从图中你可以看到在2Hz和8Hz有两个峰值。

在此输入图像描述

scipy.fftpack.fftfreq(n, d) gives you the frequencies directly. scipy.fftpack.fftfreq(n,d)直接给你频率。 If you set d=1/33.34 , this will tell you the frequency in Hz for each point of the fft. 如果设置d=1/33.34 ,这将告诉您fft每个点的频率(Hz)。

The frequency width of each bin is (sampling_freq / num_bins). 每个bin的频率宽度是(sampling_freq / num_bins)。

A more fundamental problem is that your sample rate is not sufficient for your signals of interest. 一个更基本的问题是您的采样率不足以满足您感兴趣的信号。 Your sample rate is 8.3 Hz; 您的采样率为8.3 Hz; you need at least 16Hz in order to capture an 8Hz input tone. 您需要至少16Hz才能捕获8Hz输入音。 1 1


1. To all the DSP experts; 1.致所有DSP专家; I'm aware that it's actually BW that's relevant, not max frequency. 我知道实际上BW是相关的,而不是最大频率。 But I'm assuming the OP doesn't want to do undersampled data acquisition. 但我假设OP不想做欠采样数据采集。

Your equation is messed up. 你的等式搞砸了。

fs = 33.33
df1 = 2*pi * (2.0/fs)
df2 = 2*pi * (5.0/fs)
x = [10*sin(n*df1) + 5*sin(n*df2) + 2*random.random() for n in range(4000)]

This gives you 4000 samples of your function, sampled at 33.33 Hz, representing 120 seconds of data. 这将为您提供4000个函数样本,采样频率为33.33 Hz,代表120秒的数据。

Now take your FFT. 现在拿你的FFT。 Bin 0 will hold the DC result. Bin 0将保持DC结果。 Bin 1 will be 33.33, bin 2 will be 66.66, etc.. Bin 1将是33.33,bin 2将是66.66等。

Edit: I forget to mention that, since your sampling rate is 33.33 Hz, the maximum frequency that can be represented will be fs/2, or 16.665 Hz. 编辑:我忘记提及,由于您的采样率为33.33 Hz,因此可以表示的最大频率为fs / 2或16.665 Hz。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM