Phase shift correction between 2 signals using cross-correlation

Hello
I'm in need of some help if you please.
I have 2 sinsoidal signals with a phase shift of 2Pi/3 between them. Basically, I need to eliminate the phase shift and obtain 2 signals with the same phase. I'm looking forward to use the cross-correlation, that represents the differnce between both signals (the phase shift, in our case), to do so.
My problem is to know how to use the cross-correlation to "correct" the shifted signal by eliminating the phase shift indiquated by the cross-correlation. Which operation to use to do so?
Here is a portion of the code for further understanding :
t = 1 : 1000;
phaseshift= 2*pi/3;
s1 = sin (2*pi*t/500);
s2 = sin (2*pi*t/500+ phaseshift);
figure(1); clf;
plot (t,s1, t,s2);
Axis_x = [-length(s1)+1 : 1 : length(s1)-1];
cross_corr = xcorr(s1,s2,'coeff');
figure(2); clf;
plot( Axe_x, coss_corr,'r');
Thanks in advance

 Accepted Answer

[c,lags]=xcorr(s1,s2); % compute cross correlation; keep lags vector
[~,iLag]=max(c(find(lags==0):end)); % find the max in one-sided
s3=circshift(s2,[0 iLag]); % correct for the shift
For your case I get
>> [~,iLag]=max(c(1000:end))
iLag =
162
>> 2*pi*iLag/500
ans =
2.0358
>> phaseshift
phaseshift =
2.0944
This isn't perfect; illustrates even with pure sine

8 Comments

Hi,
This is exactly what I found during a research.. Is there a other way to find the lag with MatLab? This shift is "minimal". However if you increase the dataset it deviates enormous!!!
Will be very dependent on the dataset; if there isn't a clear "winner" as to the correlation, it's possible that another is larger than that which one expects.
whereas in my case I have the exact same problem. I also need the index of the actual dataset where the correlation is highest. For some reason it deviates with increasing datasets.
Again would have to see the actual dataset to be able to tell for certain.
To understand what you get from the xcorr function for that dataset, try the following--
plot(x1), hold on,plot(x2,'r') % show the two signals...
[c,l]=xcorr(x1,x2); % compute correl, save lags
figure,plot(l,c) % plot correlations vs lag
Now align the two figures vertically one under the other so both are visible and note where the peak is on the correlation at lag==4. Now note that if you count from 1 and move the LH signal over it aligns with the other at position 5 which is FOUR steps (lags) over from the original position. Hence owing to the indices in the signal arrays counting from one while the lags count from zero, there's an "off by one" error built in.
Now, this is for a deterministic signal; if there is noise and/or multiple subsets within a pair of signals that do show linear correlation the estimation will show that and the actual peak isn't so definitive as in that example.
Hi,
Can I ask for the code >> [~,iLag]=max(c(1000:end))
iLag = 162
>> 2*pi*iLag/500
ans = 2.0358
>> phaseshift
phaseshift = 2.0944
Is the '500' in 2*pi*iLag/500 sampling frequency?
If possible, can explain why the phase shift equation 2*pi*iLag/500?
It's from the OP's original signal...
"Here is a portion of the code for further understanding: "
t = 1 : 1000;
phaseshift= 2*pi/3;
s1 = sin (2*pi*t/500);
s2 = sin (2*pi*t/500+ phaseshift);
Hi dpb,
What if I don't know the functions of these two lines, but only two set of data like these two lines. Then I can have the lags, how do I know the phase shift? Thanks!
You know the number of lags and the sampling time, don't you?

Sign in to comment.

More Answers (1)

Thanks a lot dpb. I tried to keep the lags vector before and I haven't succeded doing so. Now Thanks to you, it's clear. I shall therefore test the code.

Asked:

on 18 Feb 2015

Commented:

dpb
on 17 May 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!