6

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

2
  • Can you upload a photo of the resulting graph to your answer? And perhaps a graph of what it "should" look like?
    – yurisich
    Dec 22, 2012 at 17:38
  • Done, can't make a "how it should look" graph, as I don't have anything that could do that. But it should have 2 peaks on the left and right and have a lot lower amplitude.
    – Coolcrab
    Dec 22, 2012 at 18:03

1 Answer 1

4
  • 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

6
  • Ok, that makes a lot of sense. But how about the amplitude, that still seems to be very large for reasons I don't understand. Oh and why are they negative?
    – Coolcrab
    Dec 22, 2012 at 18:17
  • The Fourier Transform of a sine is a sum of delta functions. The sine is distributed in space, and highly localized in frequency-space. The delta function has mass at only one point, but integrates to one, so it is thought to have "infinite" amplitude. The discrete Fourier transform which we are computing here is sort of an approximation of the Fourier transform, so it makes some sense that the amplitudes should also be very large and localized, like the delta function.
    – unutbu
    Dec 22, 2012 at 20:51
  • I don't have an intuitive explanation for why the amplitudes are negative.
    – unutbu
    Dec 22, 2012 at 20:52
  • By the way, it is a general principle that functions that are localized in space are distributed in frequency-space, and vice versa.
    – unutbu
    Dec 22, 2012 at 21:03
  • 2
    Different people define fourier transform different ways, often varying by factors involving 2, pi and N. You'll probably have to go back to the definition scipy.fftpack.rfft is using and compare it to the definition used in the theoretical curve you are fitting. N/(2*pi) is about 1000, so that might be the source of the discrepancy.
    – unutbu
    Dec 22, 2012 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.