This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Help needed to Calculate phase using FFT

Hi,

I have two signals. I am acquiring them through the ADC channels. I could successfully compute the FFT using inbuilt functions in the ROM of c5515.
To check if the FFT computations are correct, if gave a 1Hz signal and back calculated the frequency from the peak of the FFT_magnitude array.

Now, I want to calculate the phase shift between the two signals. So , I computed the peak in the FFT_magnitude and then find the phase differnce between the principal components. but unfortunately, I am not getting the expected answer. Could you please have a look, if the way i am computing phase is wrong or not ?

Length of the FFT -----------------------------> INPUT_BUFFER
Computed FFT is stored in a pointer --->  *result

    j=0;
    for(i=0;i<INPUT_BUFFER/2;i++)    //I am just tranversing only half of INPUT_BUFFER since FFT are mirrored halves
    {
        real_part[j] = *(result++);
        imaginary_part[j] = *(result++);
        magnitude[j] =  sqrt(pow(real_part[j],2) + pow(imaginary_part[j],2));    
        phase[j] = atan(imaginary_part[j] / real_part[j]);    
        j = j+1;             
    }

Using this algorithm, I found magnitude and phase information in both the signals. I am using same frequency signals but shifted by theta degrees.

Signal_1 -- > magnitude_1 and phase_1
Signal_2 -- > magnitude_2 and phase_2

I compute the maximum index in the magnitude_1 and magnitude_2 array. Since , both the signals are of same frequency, peaks in magnitude_1 and magnitude_2
occur at the same index = peak_index

Now, to calculate phase shift  = ( phase_2[peak_index] - phase_1[peak_index] ) * 180 / pi;
This should have given me theta. But, the answer is coming, bit random.

Can someone tell me how to tackle this ???

  • Anand,

     You have the right approach, your signals must be wrong.  Have you plotted them?

    For example, in MATLAB (or Octave):

    x = cos((0:8191)/8192*2*pi);
    >> y = fft(x);
    >> abs(y(2))

    ans =

    4096

    >> angle(y(2))

    ans =

    -1.358234271581216e-016

    >> x2 = cos((0:8191)/8192*2*pi + pi/4);
    >> y2 = fft(x2);
    >> angle(y2(2)) - angle(y(2))

    ans =

    0.785398163397448

    >> pi/4

    ans =

    0.785398163397448 

     

    Regards,

    Bill 

  • hi,

    i could solve the issue wen i declared real_part and imaginary_part as floats..

  • Hi Anand,

    The problem is most likely the line:

    Anand Chandrasekhar said:
     

            phase[j] = atan(imaginary_part[j] / real_part[j]);

    Assuming the arrays are integer ones the division will give a (possibly very) inaccurate result. It should suffice to cast one of the operands to float, e.g:

    phase[j] = atan((float)imaginary_part[j] / real_part[j]);

    Or, if the atan2 function is available that would be even better:

    phase[j] = atan2(imaginary_part[j], real_part[j]);

    Best Regards,

     Tobias