DSPRelated.com
Forums

Phase of sine wave using fft

Started by cpshah99 November 18, 2010
Hi Guys

I am trying to get phase information of the incoming sine wave. I know the
frequency of the signal. 

So I thought to perform fft, find peak at the frequency and measure the
phase by using arctan. But I get different ans. 

Then I tried another method in that, you multiply your incoming signal with
sine and cosine waves, accumulate the result and then perform arctan. With
this method, I get exact result. 

So what is wrong with the fft method.

Best Regards

Chintan

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Here is a code.

clear all;
clc;
close all


fs=48e3;                      % Sampling frequency
T = 1/fs;                     % Sample time
L = 1000;                     % Length of signal
t = (0:L-1)*T;                % Time vector

fc=12e3;
a_phi=30*pi/180;

method='fft';

x = sin(2*pi*fc*t+a_phi);

y = x;

if strcmp(method,'fft')
    
    Nfft = 2^20;
    Y = fft(y,Nfft);
    f = linspace(0,fs/2,Nfft/2);
    
    Y1=Y(1:Nfft/2);
    
    [a b]=max(abs(Y1));
    
    phase=angle(Y1(b))*180/pi;
    
else strcmp(method,'accumulate')
    
    z=exp(1i*2*pi*fc*t);
    
    comp1=sum(y.*real(z));
    comp2=sum(y.*imag(z));
    
    phase=atan2(comp1,comp2)*180/pi;
end
On Nov 18, 10:01=A0am, "cpshah99" <cpshah99@n_o_s_p_a_m.rediffmail.com>
wrote:
> Hi Guys > > I am trying to get phase information of the incoming sine wave. I know th=
e
> frequency of the signal. > > So I thought to perform fft, find peak at the frequency and measure the > phase by using arctan. But I get different ans. > > Then I tried another method in that, you multiply your incoming signal wi=
th
> sine and cosine waves, accumulate the result and then perform arctan. Wit=
h
> this method, I get exact result. > > So what is wrong with the fft method.
It can't be used for this purpose. The difference between the two methods is that with the FFT, you estimate the phase at the frequencies implicitly determined by the FFT. In your particular case, none of these frequencies match up with the design frequency fc. In the 'accumulate' method you already know, and thus explicitly choose to use, the 'true' frequency. Rune
>On Nov 18, 10:01=A0am, "cpshah99" <cpshah99@n_o_s_p_a_m.rediffmail.com> >wrote: >> Hi Guys >> >> I am trying to get phase information of the incoming sine wave. I know
th=
>e >> frequency of the signal. >> >> So I thought to perform fft, find peak at the frequency and measure the >> phase by using arctan. But I get different ans. >> >> Then I tried another method in that, you multiply your incoming signal
wi=
>th >> sine and cosine waves, accumulate the result and then perform arctan.
Wit=
>h >> this method, I get exact result. >> >> So what is wrong with the fft method. > >It can't be used for this purpose. The difference between >the two methods is that with the FFT, you estimate the >phase at the frequencies implicitly determined by the FFT. >In your particular case, none of these frequencies match >up with the design frequency fc. > >In the 'accumulate' method you already know, and thus >explicitly choose to use, the 'true' frequency. > >Rune >
Thanks Rune. So what would you do to estimate the phase of incoming signal in practical receivers? And it is still not clear to me why fft method cant work? Best Regards Chintan
On Nov 18, 10:32=A0am, "cpshah99" <cpshah99@n_o_s_p_a_m.rediffmail.com>
wrote:
> >On Nov 18, 10:01=3DA0am, "cpshah99" <cpshah99@n_o_s_p_a_m.rediffmail.com= > > >wrote: > >> Hi Guys > > >> I am trying to get phase information of the incoming sine wave. I know > th=3D > >e > >> frequency of the signal. > > >> So I thought to perform fft, find peak at the frequency and measure th=
e
> >> phase by using arctan. But I get different ans. > > >> Then I tried another method in that, you multiply your incoming signal > wi=3D > >th > >> sine and cosine waves, accumulate the result and then perform arctan. > Wit=3D > >h > >> this method, I get exact result. > > >> So what is wrong with the fft method. > > >It can't be used for this purpose. The difference between > >the two methods is that with the FFT, you estimate the > >phase at the frequencies implicitly determined by the FFT. > >In your particular case, none of these frequencies match > >up with the design frequency fc. > > >In the 'accumulate' method you already know, and thus > >explicitly choose to use, the 'true' frequency. > > >Rune > > Thanks Rune. > > So what would you do to estimate the phase of incoming signal in practica=
l
> receivers?
I wouldn't. There is no need to. And unless one comes up with a universal time reference (which is hard), it is almost impossible for others to agree with the result.
> And it is still not clear to me why fft method cant work?
It's not that it can not work - occasionally it can - but that most of the time it doesn't. Rune
>> >> So what would you do to estimate the phase of incoming signal in
practica=
>l >> receivers? > >I wouldn't. There is no need to. And unless one comes up >with a universal time reference (which is hard), it is >almost impossible for others to agree with the result. > >> And it is still not clear to me why fft method cant work? > >It's not that it can not work - occasionally it can - but that >most of the time it doesn't. > >Rune >
OK. The reason to ask this que is that I am working on DoA problem. I was looking at TDOA method to locate the target. So for a given geometry, for example 4 sensors placed on a square geometry and one sensor placed in center, I can estimate the delay/distance using autocorrelation. And then apply trigonometry to get elevation angle. But how do I find azimuth angle?
> >Hi Guys > >I am trying to get phase information of the incoming sine wave. I know
the
>frequency of the signal. > >So I thought to perform fft, find peak at the frequency and measure the >phase by using arctan. But I get different ans. > >Then I tried another method in that, you multiply your incoming signal
with
>sine and cosine waves, accumulate the result and then perform arctan.
With
>this method, I get exact result. > >So what is wrong with the fft method. > >Best Regards > >Chintan >
The Fourier transform of a sine is given by F{sin(wo*t + a)} = 1/(2j)*[delta(w-wo)*exp(j*a) - delta(w+wo)*exp(-j*a)] = 0.5*[delta(w-wo)*exp(j*(a-pi/2)) - delta(w+wo)*exp(-j*(a+pi/2))] This means, if your sin has phase "a", the output of DFT has phase "a - pi/2" and "a + pi/2".
>The Fourier transform of a sine is given by > >F{sin(wo*t + a)} = 1/(2j)*[delta(w-wo)*exp(j*a) - delta(w+wo)*exp(-j*a)] > = 0.5*[delta(w-wo)*exp(j*(a-pi/2)) -
delta(w+wo)*exp(-j*(a+pi/2))]
> >This means, if your sin has phase "a", the output of DFT has phase "a - >pi/2" and "a + pi/2". > >
Thanks Marco. Just figured that out. Regards Chintan
On Nov 18, 4:01=A0am, "cpshah99" <cpshah99@n_o_s_p_a_m.rediffmail.com>
wrote:
> Hi Guys > > I am trying to get phase information of the incoming sine wave. I know th=
e
> frequency of the signal. > > So I thought to perform fft, find peak at the frequency and measure the > phase by using arctan. But I get different ans. > > Then I tried another method in that, you multiply your incoming signal wi=
th
> sine and cosine waves, accumulate the result and then perform arctan. Wit=
h
> this method, I get exact result. > > So what is wrong with the fft method. > > Best Regards > > Chintan > > %%%%%%%%%%%%%%%%%%%%%%%%%%%% > Here is a code.
----------SNIP
> x =3D sin(2*pi*fc*t+a_phi);
x =3D cos(2*pi*fc*t+a_phi); Hope this helps. Greg
On 11/18/2010 1:54 AM, cpshah99 wrote:

> > The reason to ask this que is that I am working on DoA problem. > > I was looking at TDOA method to locate the target. So for a given geometry, > for example 4 sensors placed on a square geometry and one sensor placed in > center, I can estimate the delay/distance using autocorrelation. And then > apply trigonometry to get elevation angle. > > But how do I find azimuth angle?
Aha! What you want is the relative phase between channels / sensors. If you have reasonable SNR then you might do this: Clip the signal - i.e. turn it into 1's and 0's .. something like that. It's the edge timing that's of interest. Do this for each channel / sensor. XOR the results for each channel pair of interest. Integrate the XOR to get relative phase. You'll have to figure out how to get polarity out of this scheme - it just has to do with which channel starts the XOR=TRUE. I used to know what an analog circuit to do this looked like. The algorithm should be pretty simple. But it implies a pretty high sample rate. I don't know how you get around that really. What is the resolution requirement for phase? How much time do you have to determine it? etc. Anyway, this is simple and gives good results. Just watch for sidelobes / multiple main lobes in the effective beam pattern this generates. Obviously, sensor spacing has a lot to do with that... Fred
On 11/18/2010 01:42 AM, Rune Allnor wrote:
> On Nov 18, 10:32 am, "cpshah99"<cpshah99@n_o_s_p_a_m.rediffmail.com> > wrote: >>> On Nov 18, 10:01=A0am, "cpshah99"<cpshah99@n_o_s_p_a_m.rediffmail.com> >>> wrote: >>>> Hi Guys >> >>>> I am trying to get phase information of the incoming sine wave. I know >> th= >>> e >>>> frequency of the signal. >> >>>> So I thought to perform fft, find peak at the frequency and measure the >>>> phase by using arctan. But I get different ans. >> >>>> Then I tried another method in that, you multiply your incoming signal >> wi= >>> th >>>> sine and cosine waves, accumulate the result and then perform arctan. >> Wit= >>> h >>>> this method, I get exact result. >> >>>> So what is wrong with the fft method. >> >>> It can't be used for this purpose. The difference between >>> the two methods is that with the FFT, you estimate the >>> phase at the frequencies implicitly determined by the FFT. >>> In your particular case, none of these frequencies match >>> up with the design frequency fc. >> >>> In the 'accumulate' method you already know, and thus >>> explicitly choose to use, the 'true' frequency. >> >>> Rune >> >> Thanks Rune. >> >> So what would you do to estimate the phase of incoming signal in practical >> receivers? > > I wouldn't. There is no need to. And unless one comes up > with a universal time reference (which is hard), it is > almost impossible for others to agree with the result.
But for coherent data reception you _do_ need to know the phase of the incoming signal -- or at least you have to know it's phase relative to your own internal time reference. Otherwise you can't demodulate anything. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html