Fft in python not showing peaks in the right place

I am trying to understand the numpy fft function because my data reduction is acting weird. But now that I have converted a simple sum of two sines, I get strange results. The peaks that I have are very high and a few dots around zero, smoothing the rest. Does anyone know what I can do 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: enter image description here While it should have two side backs on both sides, one of them is 2x higher than the other

+7
source share
1 answer
  • Your datay real, so maybe you need to get the FFT for using scipy.fftpack.rfft .
  • If you are looking for an FFT with two distinct peaks, then you should give it data that is the sum of sine waves, whose members have periods that are integer multiples of 2*pi/n , where n = len(datay) . If not, many such sines of the wave are needed 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

+4
source

All Articles