http://nl.mathworks.com/matlabcentral/newsreader/view_thread/309484
MATLAB Central Newsreader  Sinusoidal phase difference issue.
Feed for thread: Sinusoidal phase difference issue.
enus
©19942017 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://nl.mathworks.com/images/membrane_icon.gif

Mon, 20 Jun 2011 16:09:04 +0000
Sinusoidal phase difference issue.
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/309484#842002
zoul
Hey guys,<br>
<br>
Hope you doing alright. The following code calculates the phase difference between two sinusoids using correlation.<br>
<br>
t=[0:0.00001:4];<br>
f = 1;<br>
s1 = sin(2*pi*f*t);<br>
s2 = sin(2*pi*f*(t0.35)); % s1 lags s2 by 0.35s<br>
subplot(2,1,1);<br>
plot(t,s1,'r',t,s2,'b');<br>
grid<br>
title('signals')<br>
% Now crosscorrelate the two signals<br>
x = xcorr(s1,s2,'coeff');<br>
tx=[4:.00001:4];<br>
subplot(2,1,2)<br>
plot(tx,x)<br>
grid<br>
% Determine the lag<br>
[mx,ix] = max(x);<br>
lag = tx(ix)<br>
hold on<br>
tm = [lag,lag];<br>
mm = [1,1];<br>
plot(tm,mm,'k')<br>
hold off<br>
S = sprintf('Lag = %5.2f',lag);<br>
title(S)<br>
<br>
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??<br>
<br>
cheers

Mon, 20 Jun 2011 16:52:06 +0000
Re: Sinusoidal phase difference issue.
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/309484#842007
Roger Stafford
"zoul " <joelsubash@gmail.com> wrote in message <itnrb0$2ku$1@newscl01ah.mathworks.com>...<br>
> Hey guys,<br>
> <br>
> Hope you doing alright. The following code calculates the phase difference between two sinusoids using correlation.<br>
> <br>
> t=[0:0.00001:4];<br>
> f = 1;<br>
> s1 = sin(2*pi*f*t);<br>
> s2 = sin(2*pi*f*(t0.35)); % s1 lags s2 by 0.35s<br>
> subplot(2,1,1);<br>
> plot(t,s1,'r',t,s2,'b');<br>
> grid<br>
> title('signals')<br>
> % Now crosscorrelate the two signals<br>
> x = xcorr(s1,s2,'coeff');<br>
> tx=[4:.00001:4];<br>
> subplot(2,1,2)<br>
> plot(tx,x)<br>
> grid<br>
> % Determine the lag<br>
> [mx,ix] = max(x);<br>
> lag = tx(ix)<br>
> hold on<br>
> tm = [lag,lag];<br>
> mm = [1,1];<br>
> plot(tm,mm,'k')<br>
> hold off<br>
> S = sprintf('Lag = %5.2f',lag);<br>
> title(S)<br>
> <br>
> 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??<br>
> <br>
> cheers<br>
        <br>
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.<br>
<br>
Roger Stafford

Mon, 20 Jun 2011 17:59:06 +0000
Re: Sinusoidal phase difference issue.
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/309484#842026
zoul
"Roger Stafford" wrote in message <itntrm$a1j$1@newscl01ah.mathworks.com>...<br>
> "zoul " <joelsubash@gmail.com> wrote in message <itnrb0$2ku$1@newscl01ah.mathworks.com>...<br>
> > Hey guys,<br>
> > <br>
> > Hope you doing alright. The following code calculates the phase difference between two sinusoids using correlation.<br>
> > <br>
> > t=[0:0.00001:4];<br>
> > f = 1;<br>
> > s1 = sin(2*pi*f*t);<br>
> > s2 = sin(2*pi*f*(t0.35)); % s1 lags s2 by 0.35s<br>
> > subplot(2,1,1);<br>
> > plot(t,s1,'r',t,s2,'b');<br>
> > grid<br>
> > title('signals')<br>
> > % Now crosscorrelate the two signals<br>
> > x = xcorr(s1,s2,'coeff');<br>
> > tx=[4:.00001:4];<br>
> > subplot(2,1,2)<br>
> > plot(tx,x)<br>
> > grid<br>
> > % Determine the lag<br>
> > [mx,ix] = max(x);<br>
> > lag = tx(ix)<br>
> > hold on<br>
> > tm = [lag,lag];<br>
> > mm = [1,1];<br>
> > plot(tm,mm,'k')<br>
> > hold off<br>
> > S = sprintf('Lag = %5.2f',lag);<br>
> > title(S)<br>
> > <br>
> > 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??<br>
> > <br>
> > cheers<br>
>         <br>
> 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.<br>
> <br>
> Roger Stafford<br>
<br>
Hey Roger,<br>
<br>
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??<br>
cheers

Mon, 20 Jun 2011 18:58:04 +0000
Re: Sinusoidal phase difference issue.
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/309484#842036
Roger Stafford
"zoul " <joelsubash@gmail.com> wrote in message <ito1pa$mpo$1@newscl01ah.mathworks.com>...<br>
> 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??<br>
> cheers<br>
        <br>
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:<br>
<br>
<a href="http://en.wikipedia.org/wiki/Nyquist–">http://en.wikipedia.org/wiki/Nyquist–</a>;Shannon_sampling_theorem<br>
<br>
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.)<br>
<br>
Roger Stafford