DSPRelated.com
Forums

how to find phase shift between two signals

Started by Unknown August 21, 2006
Hi to all,
I am presently facing problem with finding the phase shift in two
Digital signals in MATLAB. I have two signals one is standard sine wave
and other is output of my mechanical system which is again sine wave
with some phase shift. can any body tell me how can I find exact phase
shift in MATLAB.

Thank you in advance

with regards
suhas deshmukh
IIT Bombay
India

suhas.deshmukh@gmail.com wrote:

> Hi to all, > I am presently facing problem with finding the phase shift in two > Digital signals in MATLAB. I have two signals one is standard sine wave > and other is output of my mechanical system which is again sine wave > with some phase shift. can any body tell me how can I find exact phase > shift in MATLAB. > > Thank you in advance > > with regards > suhas deshmukh > IIT Bombay > India >
I would suggest that you "forget about" MATLAB for the moment. You should think more about describing your problem. Some 'leading' questions ;) 1. Is system LTI? 2. Are you interested in time delay? 3. Are you interested in phase shift? 4. Why did I propose these questions? 5. What is QUESTION 3.5 which I did not ask? A side question of my own to the group -- am I learning how to lead others to asking/solving the "RIGHT" question?
<suhas.deshmukh@gmail.com> wrote in message
news:1156184111.810330.74720@h48g2000cwc.googlegroups.com...
> Hi to all, > I am presently facing problem with finding the phase shift in two > Digital signals in MATLAB. I have two signals one is standard sine wave > and other is output of my mechanical system which is again sine wave > with some phase shift. can any body tell me how can I find exact phase > shift in MATLAB. >
Hi suhas, See what happens when you multiply them together! Cheers, Syms.
Suhas -

    Use the Matlab "xcorr" function.  The cross-correlation is maximal when
the two signals are shifted with respect to each other by some amount.  The
amount of shift that produces the maximum is the amount by which one signal
lags, or leads, the other.  Be careful - periodic signals may have periodic
peaks in the cross-correlation.  Check the Matlab documentation for the
details.

    One can also fit the output to the form A*sin(wt + phi) to get A, w, and
the shift phi.

HTH,
Bill




<suhas.deshmukh@gmail.com> wrote in message
news:1156184111.810330.74720@h48g2000cwc.googlegroups.com...
> Hi to all, > I am presently facing problem with finding the phase shift in two > Digital signals in MATLAB. I have two signals one is standard sine wave > and other is output of my mechanical system which is again sine wave > with some phase shift. can any body tell me how can I find exact phase > shift in MATLAB. > > Thank you in advance > > with regards > suhas deshmukh > IIT Bombay > India >
Bill wrote:
> Use the Matlab "xcorr" function. The cross-correlation is maximal when > the two signals are shifted with respect to each other by some amount. The > amount of shift that produces the maximum is the amount by which one signal > lags, or leads, the other. Be careful - periodic signals may have periodic > peaks in the cross-correlation. Check the Matlab documentation for the > details.
this, and Symon's suggestion. you will also want to divide by the amplitude of each sinusoid. let: x1(t) = A1 * cos(w*t + phi1) and x2(t) = A2 * cos(w*t + phi2) x1(t)*x2(t) = A1*A2 * cos(w*t + phi1)*cos(w*t + phi2) = A1*A2/2 * ( cos(2*w*t + phi1+phi2) + cos(phi1-phi2) ) if you low pass filter (average) the result the high frequency term goes away. another way to compute the mean is to integrate over one period and divide by the length of that period. A1*A2/2 * cos(phi1-phi2) = mean{ x1(t)*x2(t) } cos(phi1-phi2) = 2 * mean{ x1(t)*x2(t) } / A1*A2 |phi1-phi2| = arccos( 2 * mean{ x1(t)*x2(t) } / A1*A2 ) so you'll still have to find a way to determine the amplitudes A1 and A2, but i'll bet you can figure that out. one last problem, this doesn't tell you if phi1-phi2 is positive or negative (because cos(phi1-phi2) = cos(phi2-phi1)), if x1(t) is leading or lagging x2(t). the really fancy mathematical method to determine who is leading whom is to compute the Hilbert transform of either x1(t) or x2(t) (whichever you think is the reference signal) and do this same correlation. the math is very similar. r b-j
<suhas.deshmukh@gmail.com> wrote in message 
news:1156184111.810330.74720@h48g2000cwc.googlegroups.com...
> Hi to all, > I am presently facing problem with finding the phase shift in two > Digital signals in MATLAB. I have two signals one is standard sine wave > and other is output of my mechanical system which is again sine wave > with some phase shift. can any body tell me how can I find exact phase > shift in MATLAB. >
You have implied that the sinusoids are of the same frequency but have not stated it explicitly. One way is to clip and XOR and average. The measurement is equivalent to measuring the time between positive (or negative) zero-crossings but with some filtering. You will have the problem of deciding whether one leads or lags the other in order to resolve phase to within +/- 2pi. Fred
robert bristow-johnson wrote:
> Bill wrote: > > Use the Matlab "xcorr" function. The cross-correlation is maximal when > > the two signals are shifted with respect to each other by some amount. The > > amount of shift that produces the maximum is the amount by which one signal > > lags, or leads, the other. Be careful - periodic signals may have periodic > > peaks in the cross-correlation. Check the Matlab documentation for the > > details. > > this, and Symon's suggestion. > > you will also want to divide by the amplitude of each sinusoid. > > let: > > x1(t) = A1 * cos(w*t + phi1) > and > x2(t) = A2 * cos(w*t + phi2) > > x1(t)*x2(t) = A1*A2 * cos(w*t + phi1)*cos(w*t + phi2) > > = A1*A2/2 * ( cos(2*w*t + phi1+phi2) + > cos(phi1-phi2) ) > > if you low pass filter (average) the result the high frequency term > goes away. another way to compute the mean is to integrate over one > period and divide by the length of that period. > > A1*A2/2 * cos(phi1-phi2) = mean{ x1(t)*x2(t) } > > cos(phi1-phi2) = 2 * mean{ x1(t)*x2(t) } / A1*A2 > > |phi1-phi2| = arccos( 2 * mean{ x1(t)*x2(t) } / A1*A2 ) > > so you'll still have to find a way to determine the amplitudes A1 and > A2, but i'll bet you can figure that out. > > one last problem, this doesn't tell you if phi1-phi2 is positive or > negative (because cos(phi1-phi2) = cos(phi2-phi1)), if x1(t) is leading > or lagging x2(t). the really fancy mathematical method to determine > who is leading whom is to compute the Hilbert transform of either x1(t) > or x2(t) (whichever you think is the reference signal) and do this same > correlation. the math is very similar.
That sounds awfully complicated. I would just project the two sine waves (input and output) onto a complex phasor at the given frequency, sort of like computing a single bin of a DFT, except that the frequency is not a multiple of the sampling frequency (make sure that enough periods are covered in the projection to reduce edge effects). This returns the amplitudes and phases of input and output (if phase is known for the input, then the projection need only be computed for the output sine wave). As Richard suggested, this assumes that the system is LTI (ie the two frequencies are equal). Regards, Andor
"robert bristow-johnson" <rbj@audioimagination.com> wrote in 
news:1156217312.117417.17220@m73g2000cwd.googlegroups.com:

> this, and Symon's suggestion. >
Or, take the fft of both signals, and look at the phases of the frequency of interest-- In my experience, if no other info is needed, the xcorr is probably the quickest to implement.
> the really fancy mathematical method to determine >who is leading whom is to compute the Hilbert transform of either x1(t) >or x2(t) (whichever you think is the reference signal) and do this same >correlation. the math is very similar.
Assuming you're talking about using the Hilbert Transform of the product of the two sinusoids, you can't do this when the two sinusoids are of the same frequency. More accurately, you can, of course, take the Hilbert Transform, but the phase you get out won't be what you want it to be. Aliasing, you know. -- Scott Reverse name to reply
suhas.deshmukh@gmail.com wrote:
> Hi to all, > I am presently facing problem with finding the phase shift in two > Digital signals in MATLAB. I have two signals one is standard sine wave > and other is output of my mechanical system which is again sine wave > with some phase shift. can any body tell me how can I find exact phase > shift in MATLAB.
I assume from what you wrote that your reference signal drives a linear system, and you want to find the relative phase of the steady-state response. How clean are the signals? If the SNR is high, then the information you want is in the zero crossings. If the sample rate is high enough, interpolating the exact time of zero crossing should introduce little error. (The actual zero crossing is a point of inflection, so linear interpolation will probably be quite accurate.) Let the time between successive positive-going zero crossings of either sinusoid be T, and the time between the positive-going zero crossings ot the driving and driven waveforms be T'. Then the phase shift is 2pi*T'/T. The measurement of T can be improved by estimating the time of n cycles instead of one, but you probably know the driving frequency /a priori/. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Cross-correlation is not the method of choice anymore.

State-space embedding is...

Read US Patent Application Pub. 20030088401 at
http://www.uspto.gov/patft
(Already allowed with all of the original and some new claims)

Also, ICASSP 2002 paper and Matlab demo can be found at:
http://www.soundmathtech.com/pitch

Works equally well for measuring the period of any periodic signal or
just measuring the phase shift between two signals
Figure it out for yourself :)

As far as patent is concerned, you don't need to worry as long as you
stay away from measuring the actual period/frequency of your sine wave
and just measure the phase shift between two different sine waves.

You can safely disregard all comments by comp.dsp "experts": some of
them are clueless, others are jealous :)

Best Regards


suhas.deshmukh@gmail.com wrote:
> Hi to all, > I am presently facing problem with finding the phase shift in two > Digital signals in MATLAB. I have two signals one is standard sine wave > and other is output of my mechanical system which is again sine wave > with some phase shift. can any body tell me how can I find exact phase > shift in MATLAB. > > Thank you in advance > > with regards > suhas deshmukh > IIT Bombay > India