简体   繁体   English

文件中数据的scipy / numpy FFT

[英]scipy/numpy FFT on data from file

I looked into many examples of scipy.fft and numpy.fft. 我研究了scipy.fft和numpy.fft的许多示例。 Specifically this example Scipy/Numpy FFT Frequency Analysis is very similar to what I want to do. 具体来说,此示例Scipy / Numpy FFT频率分析与我要执行的操作非常相似。 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. 我想从一个仅包含一列的文件中导入数据,以使我的第一个测试尽可能地容易。

My code writes like this: 我的代码是这样写的:

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()

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. 它实际上应该出现在200 Hz附近。

在此处输入图片说明

With smaller range: 范围较小时:
在此处输入图片说明

Still biggest peak at zero. 最高峰仍为零。

As already mentioned, it seems like your signal has a DC component, which will cause a peak at f=0. 如前所述,您的信号似乎具有直流分量,这将导致在f = 0处出现峰值。 Try removing the mean with, eg, arr2 = array - np.mean(array) . 尝试使用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()

Take a look at the documentation of plt.psd() , since there a quite a lot of options to fiddle with. 看一下plt.psd()的文档,因为有很多可供选择的选项。 For investigating the change of the spectrum over time, plt.specgram() comes in handy. 为了调查频谱随时间的变化,请使用plt.specgram()

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

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