"Roger Stafford" wrote in message <itntrm$a1j$1@newscl01ah.mathworks.com>...
> "zoul " <joelsubash@gmail.com> wrote in message <itnrb0$2ku$1@newscl01ah.mathworks.com>...
> > Hey guys,
> >
> > Hope you doing alright. The following code calculates the phase difference between two sinusoids using correlation.
> >
> > t=[0:0.00001:4];
> > f = 1;
> > s1 = sin(2*pi*f*t);
> > s2 = sin(2*pi*f*(t0.35)); % s1 lags s2 by 0.35s
> > subplot(2,1,1);
> > plot(t,s1,'r',t,s2,'b');
> > grid
> > title('signals')
> > % Now crosscorrelate the two signals
> > x = xcorr(s1,s2,'coeff');
> > tx=[4:.00001:4];
> > subplot(2,1,2)
> > plot(tx,x)
> > grid
> > % Determine the lag
> > [mx,ix] = max(x);
> > lag = tx(ix)
> > hold on
> > tm = [lag,lag];
> > mm = [1,1];
> > plot(tm,mm,'k')
> > hold off
> > S = sprintf('Lag = %5.2f',lag);
> > title(S)
> >
> > Everything seems to be fine as long as the frequency remains the same. But once I double the frequency(f) the phase(lag) estimate goes wrong. This suggests that there is a relation between phase difference introduced during signal generation and frequency, which to me seems wrong.Could someone explain what is happening??
> >
> > cheers
>         
> I haven't checked your stuff in detail, but with f equal to 1, a time lag of 0.35 represents a 0.35 fractional part of a full cycle, whereas with f equal to 2, it is a 0.70 fractional part of a full cycle. Over an infinite number of cycles, that would be the equivalent of a negative lag of .30 part of a cycle or a negative time lag of 0.15 . Are you sure something like this isn't what is happening to you? Also you are carrying out this analysis over only 4 to 8 cycles. An accurate analysis would require many more cycles than that I would think.
>
> Roger Stafford
Hey Roger,
I fail to understand (mostly due to ignorance) how you have made the estimate over an infinite number of cycles, although the time lag of 0.15 is correct when f equals 2(using the given code). Could you please explain in more detail the issue pointed out when f increases.Also do you reckon using correlation is the best way to estimate time lag(phase difference) if large number of cycles are used??
cheers
