# Finding phase angle via FFT

I am using the FFT to calculate the amplitude and phase of AC cos signal. Regarding amplitude it gives me an accurate result but I get wrong result for a phase: 6 degrees instead of 36 degrees.

My MATLAB code is as follows:

fs=3840;                 % sampling frequency
N=64;                    % number of sample
f=60;                    % signal frequency in hz
w=2*pi*f;
amp=220;                 % ampitude of signal
delt=[1:N]*2*pi/N;       % sampling time interval

sig=zeros(size(delt));
for i=1:1:length(delt)
sig(i)=noise(i)+amp*cos(w*delt(i)+pi/5);
end

FF=fft(sig,N);
stem(abs(FF))
grid on

REAL=real(FF);
SUM_REAL=sum(REAL)
IMAG=abs(imag(FF));
SUM_IMAG=sum(IMAG)

% *******************************PHASOR REPRESENTATION*******************

real_amp=sqrt(SUM_REAL)                  % Amplitude of real part
imag_amp=sqrt(SUM_IMAG)                  % Amplitude of imaginary part
sumation=real_amp+imag_amp;
amplitude_nominal=sqrt(sumation)/N

• Herzlich Willkommen to DSP SE! As you probably noticed, I modified your questions with proper code formatting blocks and fixed few formatting issues. Nevertheless it is still messy, i.e. your signal generation is in wrong place and few variables are still not defined. Could you be so kind and update it so it will work? Also may I suggest you to use the angle function? – jojek Jul 11 '14 at 11:18
• For a real-valued time-domain signal, the FFT will be conjugate-symmetric, so sum(imag(FF)) ought to be zero (to machine precision). You really want to look at the phase of individual bins corresponding to the frequency you care about. If your components don't fall exactly on FFT bins, you'll want to use a tapered window, and then to rotate your time axis to be centered on n=0 e.g. FF = fft(fftshift([0,hann(63)'].*sig)); plot(atan(imag(FF)./real(FF)),'.-') – dpwe Jul 11 '14 at 17:35
• First of all - this code didn't worked out of a box - obvious mistakes, such as sqr instead of sqrt and variable p instead of phase. Secondly, you claim something about 6 degrees and 36 degrees. None of these numbers is appearing. This question won't likely benefit anyone else. You should really revise your work. There is not point in fixing MATLAB bugs by us. – jojek Jul 11 '14 at 17:35