I'm trying to figure out how FFT's phase works. Here's my code:
import numpy as np
t = np.linspace(0,120,1200)
acc = lambda t: 10*np.sin(2*np.pi*t)
# signal is a perfect 10 amplitude 1 frequency sin wave
signal = acc(t)
FFT = np.fft.rfft(signal)
freqs = np.fft.rfftfreq(len(signal),1/10.)
real = signal[110]
total = 0
for binnum in range(len(FFT)):
bin = FFT[binnum]
amp = (np.sqrt(bin.real**2 + bin.imag**2))/len(freqs)
angle = np.angle(bin)
phase = np.arctan2(bin.imag, bin.real)
freq = freqs[binnum]
contribution = amp * np.sin(2*np.pi*freq*t[110] + phase)
total += contribution
print "Amp: %f, angle: %f, phase: %f, freq: %f" % (amp, angle, phase, freq)
In the print, I see this line:
mp: 9.815303, angle: -1.256637, phase: -1.256637, freq: 1.000000
It thinks the phase of the perfect 1 frequency wave is -1.25, but it should be 0. How do I find the right phase?
Also, I understand if it's because of spectral leakage, but how does the inverse FFT work then? I was under the impression that to reconstruct the signal at time t, you'd just sum up all the contributions from the sinusoidal waves. But it doesn't work, since the output of the above code is:
total: -9.90868489422 real: 0.576119838246