bjornhartmann
bjornhartmann

Reputation: 31

Unexpected amplitude in numpy fft

I am having an issue with numpy fft not giving me the expected amplitude in the fft plot. This only happens for certain periods as input.

I am using a clean sine signal with a period of 25 points over 240 datapoints.

The np.fft.rfft gives a peak of 24.

enter image description here

I am wondering what may cause this. I would think clean signal should produce a dirac-delta function like result around 25. I get this type of result for certain periods, but not all. Is there a need for more repetitions of this period in order to specify the period accurately? this does not make sense to me. The fft is done in the following way, where y=my sine datapoints with period 25:

fft = np.fft.rfft(y)
fft = abs(fft)
x=np.fft.rfftfreq(len(y),d=1./1)
x = 1/x # to convert from freq to periods. T = 1/f
plt.plot(x,fft)

Upvotes: 2

Views: 418

Answers (1)

nneonneo
nneonneo

Reputation: 179392

Recall that an FFT is technically computed over an infinite periodic extension of your signal. Therefore, if your signal doesn’t contain an integer number of periods, the periodic extension will contain a discontinuity (in phase, and usually also in amplitude) at the period boundaries. This will manifest as a “smearing” of your signal across multiple adjacent frequency bins. Additionally, note that individual bins of the FFT represent particular frequencies (use fftfreq for a list); any frequency that isn’t representable in a single bin will need to be reconstructed from nearby bins.

You can improve your FFT result by applying windowing, e.g. by multiplying your signal with a window function (typically something like a Hamming, Hann or raised cosine filter), which will reduce the amplitude discontinuity, and thus sharpen your peak, at the expense of some accuracy at the edges of your signal.

Upvotes: 6

Related Questions