# 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
noise=randn(size(delt)); % added noise

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
phase=atan(abs(imag(FF))/abs(real(FF))) % phase in radians
PHI=abs(phase)*180/pi                   % phase angle in degrees

• 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
• Being more specific, please refer here: Phase angle.
– jojek
Jul 11 '14 at 11:33
• 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
• im extremly sorry..as im newbie here tht's why doing a lot of mistake Jul 11 '14 at 17:43

## 1 Answer

Since the length of your FFT is exactly one period of your sinusoidal input signal (N = Fs/F0) , the signal will appear in bin 1 (the one above DC) of the FFT result (the conjugate mirror is redundant for strictly real input). To estimate the phase of the sinusoid of frequency F0, referenced to the start of the data window, just compute the atan2() of the signed imaginary and real components from that single complex bin 1. No need to abs() components or sum or window or estimate the amplitude or examine any other bin. Unless you want to sanity check the magnitude of bin 1 to make sure it isn't zero or just numerical noise.