I am getting started with Python's FFT. I tested it on a signal that is a sum of three signals, two of which have an eigenfrequency of the grid, the third one does not (but due to large no. of data points, I still get a peak at the correct position).

However, the amplitudes are a bit off. The amplitude of the third signal is a bit higher than it should be (>=5.1 instead of 5). I was expecting that the amplitude would be lower for this signal, as it does not have one of the eigenfrequencies. But how can it be higher?

Also, for the two "well-defined" components of my signal, the amplitude is only correct to order 10^(-2). Again, one of them is a bit larger than the actual value. Are these numerical errors in the fft routine or is something wrong with my code?

Thank you!

import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import fft, fftfreq, ifft

L = 5
N = 1000
pi2 = 2*np.pi

x = np.linspace(0, L, N)

y1 = 10*np.sin(6*(2*np.pi/5)*x)  ## has one of the eigenfrequencies
y2 = 20*np.sin(10*(2*np.pi/5)*x) ## has one of the eigenfrequencies
y3 = 5 *np.cos(626.3*x)  ## lies between the highest and second highest eigenfrequencies

y = y1 + y2 + y3

A = fft(y,N)

freqs = fftfreq(N, d=L/N) * pi2
mask = freqs > 0

plt.plot(freqs[mask], 2*np.abs(A[mask]/N))

enter image description here

  • 1
    $\begingroup$ Your problem is not the FFT implementation... $\endgroup$ – Ben Nov 2 '19 at 2:06
  • $\begingroup$ download.ni.com/evaluation/pxi/… $\endgroup$ – Ben Nov 2 '19 at 2:07
  • $\begingroup$ Excuse me, but can you just tell me the problem? If simply looking at the theory would help me, then I wouldn't have felt the need to post here. $\endgroup$ – SchroedingersLion Nov 14 '19 at 21:35
  • 1
    $\begingroup$ My x vector consists of 1000 entries. But I checked the contents. The first point is 0 and the last point is 5. This is wrong, isn't it? Last point should be 5-L/N = 4.995. $\endgroup$ – SchroedingersLion Nov 20 '19 at 22:48
  • 1
    $\begingroup$ That helped!!! Now, if I leave the "bad" signal part out, I am at 10 and 20 with 5 digits accuracy after the comma. If I have the bad signal there, I am at 10 and 20 with 3-4 digits accuracy. Is this correct behavior? $\endgroup$ – SchroedingersLion Nov 20 '19 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.