# Help understanding FFT Phase: code and results provided

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

• It's sometimes easier to understand phase if you do an fftshift of the data before applying an FFT, which references phase to the center of your sample vector. Or you can set t=0 in your generator to the center instead. The ends can be discontinuous for a non-periodic-in-aperture sinusoid, and the phase of a discontinuity can seem non-intuitive. – hotpaw2 Jan 18 '14 at 7:30

You phase isn't zero because your FFT width isn't an exact multiple of your sinewave's period.

Use

t = np.linspace(0,120,1200, endpoint=False)


to get exactly 1200 sampling points with the correct wrap-around.

Then consider also that

$$\sin(x)=\frac{e^{ix}-e^{-ix}}{2i}=\tfrac12 e^{i(x-\frac\pi2)}+\tfrac12e^{-i(x-\frac\pi2)}$$

which leads to the then observable phase of -1.570796...