fft in python not showing peaks in right place
I'm trying to understand the numpy fft function, because my data reduction is acting weirdly. But now that I've transformed a simple sum of two sines, I get weird results. The peaks I have is extremely high and several points wide around zero, flattening the rest. Does anybody have a clue of what I might be doing wrong?
import numpy as np
from numpy import exp, sqrt, pi, linspace
from matplotlib import cm
import matplotlib.pyplot as plt
import scipy as sp
import pylab
#fourier
tdata = np.arange(5999.)/300
datay = 3*np.sin(tdata)+6*np.sin(2*tdata)
fouriery = np.fft.fft(datay)
freqs = np.fft.fftfreq(datay.size, d=0.1)
pylab.plot(freqs,fouriery)
pylab.show()
What I get is this: While it should have two sidepeaks on both sides, one of em 2x higher than the other
datay
is real, so perhaps you should be taking a FFT for a real sequence using scipy.fftpack.rfft
. 2*pi/n
, where n = len(datay)
. If not, it will take many such sine waves to approximate the data. import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack as fftpack
pi = np.pi
tdata = np.arange(5999.)/300
datay = 3*np.sin(2*pi*tdata)+6*np.sin(2*pi*2*tdata)
fouriery = fftpack.rfft(datay)
freqs = fftpack.rfftfreq(len(datay), d=(tdata[1]-tdata[0]))
plt.plot(freqs, fouriery, 'b-')
plt.xlim(0,3)
plt.show()
链接地址: http://www.djcxy.com/p/67758.html