Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fft in python not showing peaks in right place

Tags:

python

fft

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

like image 291
Coolcrab Avatar asked Dec 22 '12 17:12

Coolcrab


People also ask

What do peaks in FFT mean?

FFTs of complex vibrations have multiple peaks- one for each frequency present in the vibration. Low frequency peaks are shown at the left end of the graph. The left-most peak represents the fundamental. Other peaks indicate overtones.

What does Numpy FFT Fftfreq do?

fft. fftfreq. Return the Discrete Fourier Transform sample frequencies.


1 Answers

  • 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

like image 112
unutbu Avatar answered Sep 22 '22 14:09

unutbu