Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Sinusoidal phase difference issue.

Subject: Sinusoidal phase difference issue.

From: zoul

Date: 20 Jun, 2011 16:09:04

Message: 1 of 4

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*(t-0.35)); % s1 lags s2 by 0.35s
subplot(2,1,1);
plot(t,s1,'r',t,s2,'b');
grid
title('signals')
% Now cross-correlate 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

Subject: Sinusoidal phase difference issue.

From: Roger Stafford

Date: 20 Jun, 2011 16:52:06

Message: 2 of 4

"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*(t-0.35)); % s1 lags s2 by 0.35s
> subplot(2,1,1);
> plot(t,s1,'r',t,s2,'b');
> grid
> title('signals')
> % Now cross-correlate 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

Subject: Sinusoidal phase difference issue.

From: zoul

Date: 20 Jun, 2011 17:59:06

Message: 3 of 4

"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*(t-0.35)); % s1 lags s2 by 0.35s
> > subplot(2,1,1);
> > plot(t,s1,'r',t,s2,'b');
> > grid
> > title('signals')
> > % Now cross-correlate 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

Subject: Sinusoidal phase difference issue.

From: Roger Stafford

Date: 20 Jun, 2011 18:58:04

Message: 4 of 4

"zoul " <joelsubash@gmail.com> wrote in message <ito1pa$mpo$1@newscl01ah.mathworks.com>...
> 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
- - - - - - - - -
  You are dealing with a periodic function in the sine function and therefore such matters as time lag are inherently indeterminate when they involve time periods of greater than half that period. As you increase the frequency of the sine wave you decrease the value at which that time lag computation becomes indeterminate. Check out:

 http://en.wikipedia.org/wiki/Nyquist–;Shannon_sampling_theorem

  My remark about the total number of cycles involved is based on the idea that if only a few cycles are included in the correlation computation, then other periodicities are present in your signal and can skew your results. You need many cycles to minimize the effect of such aperiodicities (if that is a valid word.)

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us