简体   繁体   中英

scipy/numpy FFT on data from file

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.

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.

在此处输入图片说明

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. Try removing the mean with, eg, 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. For investigating the change of the spectrum over time, plt.specgram() comes in handy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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