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
amp=220;                 % ampitude of signal   
delt=[1:N]*2*pi/N;       % sampling time interval
noise=randn(size(delt)); % added noise

for i=1:1:length(delt)

grid on


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

real_amp=sqrt(SUM_REAL)                  % Amplitude of real part
imag_amp=sqrt(SUM_IMAG)                  % Amplitude of imaginary part
phase=atan(abs(imag(FF))/abs(real(FF))) % phase in radians
PHI=abs(phase)*180/pi                   % phase angle in degrees
  • 1
    $\begingroup$ 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? $\endgroup$
    – jojek
    Jul 11 '14 at 11:18
  • $\begingroup$ Being more specific, please refer here: Phase angle. $\endgroup$
    – jojek
    Jul 11 '14 at 11:33
  • 1
    $\begingroup$ 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)),'.-') $\endgroup$
    – dpwe
    Jul 11 '14 at 17:35
  • 1
    $\begingroup$ 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. $\endgroup$
    – jojek
    Jul 11 '14 at 17:35
  • 1
    $\begingroup$ im extremly sorry..as im newbie here tht's why doing a lot of mistake $\endgroup$ Jul 11 '14 at 17:43

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.