<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/309484</link>
    <title>MATLAB Central Newsreader - Sinusoidal phase difference issue.</title>
    <description>Feed for thread: Sinusoidal phase difference issue.</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.nl/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 20 Jun 2011 16:09:04 +0000</pubDate>
      <title>Sinusoidal phase difference issue.</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/309484#842002</link>
      <author>zoul </author>
      <description>Hey guys,&lt;br&gt;
&lt;br&gt;
Hope you doing alright. The following code calculates the phase difference between two sinusoids using correlation.&lt;br&gt;
&lt;br&gt;
t=[0:0.00001:4];&lt;br&gt;
f = 1;&lt;br&gt;
s1 = sin(2*pi*f*t);&lt;br&gt;
s2 = sin(2*pi*f*(t-0.35)); % s1 lags s2 by 0.35s&lt;br&gt;
subplot(2,1,1);&lt;br&gt;
plot(t,s1,'r',t,s2,'b');&lt;br&gt;
grid&lt;br&gt;
title('signals')&lt;br&gt;
% Now cross-correlate the two signals&lt;br&gt;
x = xcorr(s1,s2,'coeff');&lt;br&gt;
tx=[-4:.00001:4];&lt;br&gt;
subplot(2,1,2)&lt;br&gt;
plot(tx,x)&lt;br&gt;
grid&lt;br&gt;
% Determine the lag&lt;br&gt;
[mx,ix] = max(x);&lt;br&gt;
lag = tx(ix)&lt;br&gt;
hold on&lt;br&gt;
tm = [lag,lag];&lt;br&gt;
mm = [-1,1];&lt;br&gt;
plot(tm,mm,'k')&lt;br&gt;
hold off&lt;br&gt;
S = sprintf('Lag = %5.2f',lag);&lt;br&gt;
title(S)&lt;br&gt;
&lt;br&gt;
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??&lt;br&gt;
&lt;br&gt;
cheers</description>
    </item>
    <item>
      <pubDate>Mon, 20 Jun 2011 16:52:06 +0000</pubDate>
      <title>Re: Sinusoidal phase difference issue.</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/309484#842007</link>
      <author>Roger Stafford</author>
      <description>"zoul " &amp;lt;joelsubash@gmail.com&amp;gt; wrote in message &amp;lt;itnrb0$2ku$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hey guys,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope you doing alright. The following code calculates the phase difference between two sinusoids using correlation.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; t=[0:0.00001:4];&lt;br&gt;
&amp;gt; f = 1;&lt;br&gt;
&amp;gt; s1 = sin(2*pi*f*t);&lt;br&gt;
&amp;gt; s2 = sin(2*pi*f*(t-0.35)); % s1 lags s2 by 0.35s&lt;br&gt;
&amp;gt; subplot(2,1,1);&lt;br&gt;
&amp;gt; plot(t,s1,'r',t,s2,'b');&lt;br&gt;
&amp;gt; grid&lt;br&gt;
&amp;gt; title('signals')&lt;br&gt;
&amp;gt; % Now cross-correlate the two signals&lt;br&gt;
&amp;gt; x = xcorr(s1,s2,'coeff');&lt;br&gt;
&amp;gt; tx=[-4:.00001:4];&lt;br&gt;
&amp;gt; subplot(2,1,2)&lt;br&gt;
&amp;gt; plot(tx,x)&lt;br&gt;
&amp;gt; grid&lt;br&gt;
&amp;gt; % Determine the lag&lt;br&gt;
&amp;gt; [mx,ix] = max(x);&lt;br&gt;
&amp;gt; lag = tx(ix)&lt;br&gt;
&amp;gt; hold on&lt;br&gt;
&amp;gt; tm = [lag,lag];&lt;br&gt;
&amp;gt; mm = [-1,1];&lt;br&gt;
&amp;gt; plot(tm,mm,'k')&lt;br&gt;
&amp;gt; hold off&lt;br&gt;
&amp;gt; S = sprintf('Lag = %5.2f',lag);&lt;br&gt;
&amp;gt; title(S)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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??&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; cheers&lt;br&gt;
- - - - - - - - -&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Mon, 20 Jun 2011 17:59:06 +0000</pubDate>
      <title>Re: Sinusoidal phase difference issue.</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/309484#842026</link>
      <author>zoul </author>
      <description>"Roger Stafford" wrote in message &amp;lt;itntrm$a1j$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "zoul " &amp;lt;joelsubash@gmail.com&amp;gt; wrote in message &amp;lt;itnrb0$2ku$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hey guys,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Hope you doing alright. The following code calculates the phase difference between two sinusoids using correlation.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; t=[0:0.00001:4];&lt;br&gt;
&amp;gt; &amp;gt; f = 1;&lt;br&gt;
&amp;gt; &amp;gt; s1 = sin(2*pi*f*t);&lt;br&gt;
&amp;gt; &amp;gt; s2 = sin(2*pi*f*(t-0.35)); % s1 lags s2 by 0.35s&lt;br&gt;
&amp;gt; &amp;gt; subplot(2,1,1);&lt;br&gt;
&amp;gt; &amp;gt; plot(t,s1,'r',t,s2,'b');&lt;br&gt;
&amp;gt; &amp;gt; grid&lt;br&gt;
&amp;gt; &amp;gt; title('signals')&lt;br&gt;
&amp;gt; &amp;gt; % Now cross-correlate the two signals&lt;br&gt;
&amp;gt; &amp;gt; x = xcorr(s1,s2,'coeff');&lt;br&gt;
&amp;gt; &amp;gt; tx=[-4:.00001:4];&lt;br&gt;
&amp;gt; &amp;gt; subplot(2,1,2)&lt;br&gt;
&amp;gt; &amp;gt; plot(tx,x)&lt;br&gt;
&amp;gt; &amp;gt; grid&lt;br&gt;
&amp;gt; &amp;gt; % Determine the lag&lt;br&gt;
&amp;gt; &amp;gt; [mx,ix] = max(x);&lt;br&gt;
&amp;gt; &amp;gt; lag = tx(ix)&lt;br&gt;
&amp;gt; &amp;gt; hold on&lt;br&gt;
&amp;gt; &amp;gt; tm = [lag,lag];&lt;br&gt;
&amp;gt; &amp;gt; mm = [-1,1];&lt;br&gt;
&amp;gt; &amp;gt; plot(tm,mm,'k')&lt;br&gt;
&amp;gt; &amp;gt; hold off&lt;br&gt;
&amp;gt; &amp;gt; S = sprintf('Lag = %5.2f',lag);&lt;br&gt;
&amp;gt; &amp;gt; title(S)&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; 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??&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; cheers&lt;br&gt;
&amp;gt; - - - - - - - - -&lt;br&gt;
&amp;gt;   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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
Hey Roger,&lt;br&gt;
&lt;br&gt;
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??&lt;br&gt;
cheers</description>
    </item>
    <item>
      <pubDate>Mon, 20 Jun 2011 18:58:04 +0000</pubDate>
      <title>Re: Sinusoidal phase difference issue.</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/309484#842036</link>
      <author>Roger Stafford</author>
      <description>"zoul " &amp;lt;joelsubash@gmail.com&amp;gt; wrote in message &amp;lt;ito1pa$mpo$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 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??&lt;br&gt;
&amp;gt; cheers&lt;br&gt;
- - - - - - - - -&lt;br&gt;
&amp;nbsp;&amp;nbsp;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:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Nyquist&amp;#8211"&gt;http://en.wikipedia.org/wiki/Nyquist&amp;#8211&lt;/a&gt;;Shannon_sampling_theorem&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.)&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
  </channel>
</rss>
