Coolcrab
Coolcrab

Reputation: 2723

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: enter image description here While it should have two sidepeaks on both sides, one of em 2x higher than the other

Upvotes: 6

Views: 2053

Answers (1)

unutbu
unutbu

Reputation: 880997

  • Your datay is real, so perhaps you should be taking a FFT for a real sequence using scipy.fftpack.rfft.
  • If you are looking for an FFT with two distinct peaks, then you must give it data which is the sum of sine waves whose terms have periods which are whole multiples of 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()

enter image description here

Upvotes: 4

Related Questions