Wrong Amplitude of the fft of a signal

Francesca Source

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

pythonfft

Answers

answered 6 months ago mikuszefski #1

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.

comments powered by Disqus