5

I am trying to calculate an fft with Python. I am using the function fft.fft and I am applying it to a simple Sinusoidal signal. Here is my code:

import numpy as np import matplotlib.pyplot as plt frames=100 fps=1000 t=np.linspace(0, frames, frames)/fps x=np.sin(2*np.pi*80*t)+1 plt.plot(t, x, 'o-') plt.title('seno') plt.ylabel('sin') plt.xlabel('time $s$') plt.grid() plt.show() #calculating the fft sin_fft=np.fft.fft(x) #calculating the absolute value sin_fft_abs=np.ones(len(sin_fft)) for i in range(len(sin_fft)): sin_fft_abs[i]=np.sqrt((sin_fft[i].real**2)+(sin_fft[i].imag**2)) sin_fft_final=sin_fft_abs/frames #calculating the frequencies inc=fps/frames freq=np.linspace(0, fps-inc, fps/inc) plt.plot(freq, sin_fft_final, 'o-') plt.xlim(xmax=fps/2) plt.title('seno fft') plt.ylabel('sin fft') plt.xlabel('f $Hz$') plt.grid() plt.show() 

It can find the right offset (1 in this simple case), but the amplitude of the peak corresponding to the Sinus Frequency (80 in this case) is half the Amplitude of the Signal, always. I have no idea why it finds the correct Offset, but not the correct Amplitude!

I would be grateful if somebody could help me, Thanks a lot, Francesca

1 Answer 1

4

This is a property of the Fourier transformation that also appears in the FFT. Actually, if you plot the full data, you'll see a second peak. You might want to check numpy.fft.fftfreq at what frequency this actually is. The frequecies in an FFT usuall go [0, df,..., fmax, -fmax, ..., -df]. So your first peak is at omega, the second at -omega. This is because it is a complex analysis, meaning the Fourier Kernel is exp( -1j * omega * t). As sin( omega * t) = 1 / 2j * ( exp( 1j * omega * t) - exp( -1j * omega * t)), you'll get the two peaks.

In the opposite direction, with a peak amplitude A you'll have your signal as A * exp( 1j * omega * t) + (-A * exp( 1j * (-omega) * t). If you expand this you'll get 1j * 2 * A * sin( omega * t ). Hence A is and must be half the amplitude of your sine wave.

Sign up to request clarification or add additional context in comments.

3 Comments

@Francesca: This is true: see de.mathworks.com/help/matlab/ref/fft.html?s_tid=gn_loc_drop ? There is e.g. a FFT of S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t) and the found amplitudes have to be multiplied by 2.
Thank you, the only Problem is that if I multiply by 2, then the Amplitude is correct, but the Amplitude of the FFT at 0Hz also doubles, so it becomes 2 times the Offset value! Is it correct?
@Francesca Well that zero frequency is special, you can see e.g. in the details of a Fourier Series. The simple answer is: multiply by 2 except for f=0. In detail you want a single sided FFT. Have a look here, and in there at the chapter Converting from a Two-Sided Power Spectrum to a Single-Sided Power Spectrum. Are window functions of importance?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.