文件中数据的scipy/numpy FFT [英] scipy/numpy FFT on data from file

查看:132
本文介绍了文件中数据的scipy/numpy FFT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究了scipy.fft和numpy.fft的许多示例.具体来说,此示例 Scipy/Numpy FFT频率分析与我要执行的操作非常相似.因此,我使用了相同的子图定位,并且一切看起来都非常相似.

I looked into many examples of scipy.fft and numpy.fft. Specifically this example Scipy/Numpy FFT Frequency Analysis is very similar to what I want to do. Therefore, I used the same subplot positioning and everything looks very similar.

我想从一个只包含一列的文件中导入数据,以使我的第一个测试尽可能容易.

I want to import data from a file, which contains just one column to make my first test as easy as possible.

我的代码是这样写的:

import numpy as np
import scipy as sy
import scipy.fftpack as syfp
import pylab as pyl

# Read in data from file here
array = np.loadtxt("data.csv")
length = len(array)
# Create time data for x axis based on array length
x = sy.linspace(0.00001, length*0.00001, num=length)

# Do FFT analysis of array
FFT = sy.fft(array)
# Getting the related frequencies
freqs = syfp.fftfreq(array.size, d=(x[1]-x[0]))

# Create subplot windows and show plot
pyl.subplot(211)
pyl.plot(x, array)
pyl.subplot(212)
pyl.plot(freqs, sy.log10(FFT), 'x')
pyl.show()

问题在于,我的峰值始终总是精确地为零,而事实并非如此.它确实应该出现在200 Hz左右.

The problem is that I will always get my peak at exactly zero, which should not be the case at all. It really should appear at around 200 Hz.

范围更小:

With smaller range:

最大峰值仍为零.

推荐答案

如前所述,您的信号似乎具有DC分量,它将导致在f = 0处出现峰值.尝试使用例如arr2 = array - np.mean(array)去除均值.

As already mentioned, it seems like your signal has a DC component, which will cause a peak at f=0. Try removing the mean with, e.g., arr2 = array - np.mean(array).

此外,对于分析信号,您可能希望尝试绘制功率谱密度.:

Furthermore, for analyzing signals, you might want to try plotting power spectral density.:

import matplotlib.pylab as plt
import matplotlib.mlab as mlb

Fs = 1./(d[1]- d[0])  # sampling frequency
plt.psd(array, Fs=Fs, detrend=mlb.detrend_mean) 
plt.show()

请参阅plt.psd()的文档,因为有很多可供选择的选项.为了调查频谱随时间的变化,plt.specgram()派上了用场.

Take a look at the documentation of plt.psd(), since there a quite a lot of options to fiddle with. For investigating the change of the spectrum over time, plt.specgram() comes in handy.

这篇关于文件中数据的scipy/numpy FFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆