I am trying to use the fast Fourier transform to extract the phase shift of a single sinusoidal function. I know that on paper. If we denote the transformation of our function as T, then we have the following relations:

However, I find that although I can accurately capture the frequency of my cosine wave, the phase is inaccurate if I have not tried at an extremely high speed. For instance:
import numpy as np import pylab as pl num_t = 100000 t = np.linspace(0,1,num_t) dt = 1.0/num_t w = 2.0*np.pi*30.0 phase = np.pi/2.0 amp = np.fft.rfft(np.cos(w*t+phase)) freqs = np.fft.rfftfreq(t.shape[-1],dt) print (np.arctan2(amp.imag,amp.real))[30] pl.subplot(211) pl.plot(freqs[:60],np.sqrt(amp.real**2+amp.imag**2)[:60]) pl.subplot(212) pl.plot(freqs[:60],(np.arctan2(amp.imag,amp.real))[:60]) pl.show()
Using num = 100000 points, I get phase 1.57173880459.
Using num = 10000 points, I get phase 1.58022110476.
Using num = 1000 points, I get phase 1.6650441064.
What will go wrong? Even with 1000 points, I have 33 points per cycle, which should be enough to resolve this. Maybe there is a way to increase the number of calculated frequency points? Is there a way to do this with a "low" number of points?
EDIT: from further experiments, it seems that I need ~ 1000 points per cycle to accurately extract the phase. Why?
EDIT 2: Further experiments show that accuracy is related to the number of points per cycle, and not to absolute numbers. An increase in the number of sample points per cycle makes the phase more accurate, but if the signal frequency and the number of sample points increase by the same factor, the accuracy remains unchanged.