MATLAB Answers

Problem of finding lag when using xcorr

67 views (last 30 days)
Zhikun Wang
Zhikun Wang on 11 Nov 2019
Answered: Linas Svilainis on 5 Dec 2019
Hi everyone,
Thank ou for your watching! I have some problem in using xcorr. Basically I have two groups of data, group1 follows the function sin(x) and group2 follows the function sin(x+3/50). The x region is from 0 to 2 and sampling frequency is 100 (201 dots in total). I can see the lags clearly through the function equation and the plot out, but when I try to use the xcorr to find the lags, the maximum always occurs at the center of the output matrix, which means the lag equals to zero. Is there anyway to solve this problem?
I find someone who has the same problem with me, but his data is a group of data close to a mean value, the method mentions in that page doesn'r work in my case.
Thanks for your reading and wish you have a nice day!
Best,
Zhikun

  0 Comments

Sign in to comment.

Answers (3)

Daniel M
Daniel M on 12 Nov 2019
Yes, running this gives a max lag at 0. That is indicating that y matches x the best in their current positions. Shifting y by any value will make it agree less. You're expecting to see a lag of 3/50 = 0.06 right? (This is equal to 6 samples).
clearvars
clc
close all
fs = 100;
t = 0:1/fs:2;
x = sin(t);
y = sin(t + 3/50);
[r1,lagsy] = xcorr(x,y);
[mr1,locmaxy] = max(r1);
lagsy(locmaxy)/fs
% ans = 0
y2 = circshift(y,6); % shift it by 6 samples
[r2,lagsy2] = xcorr(x,y2);
[mr2,locmaxy2] = max(r2);
lagsy2(locmaxy2)/fs
% ans = 0
% Plot the results
figure
plot(t,x,t,y,t,y2,'k--')
legend('x','y','y2','location','southeast')
Notice that mr1 > mr2. Look at the plot of y2. It's easy to see that any shift of y would lead to less correlation with x than the original signal.
HOWEVER, if you allow the oscillations to go through one full period, then yes, you get the correct value of 0.06.
clearvars
clc
close all
fs = 100;
t = 0:1/fs:2*pi;
x = sin(t);
y = sin(t + 3/50);
[r1,lagsy] = xcorr(x,y);
[mr1,locmaxy] = max(r1);
lagsy(locmaxy)/fs
% ans = 0.06

  4 Comments

Show 1 older comment
Daniel M
Daniel M on 13 Nov 2019
It depends on how specific your problem is then. Are you really just trying to measure the phase difference between two sinusoids of the same frequency and amplitude? That's very trivial (you could just use asin()), which is why I suspect your actual problem to be more sophisticated than that.
You can detect the phase by using the functions fft() and then angle(). Here is a thread you may find useful. https://www.mathworks.com/matlabcentral/answers/378174-how-to-find-phase-shift-and-do-phase-shift-correction-between-two-signals-in-frequency-domain
Here are some other functions of interest:
I didn't check, but I suspect these functions rely on xcorr internally.
Zhikun Wang
Zhikun Wang on 13 Nov 2019
Yes. Actually that segment of real data just "looks like" a sin wave but not. What I want to figure out it's the time delay from two diffraction intensity result. I will see the link you attached to figure out if it helps or not. Thanks again for your kindness and wish you have good at the rest of the day!
Best,
Zhikun
Daniel M
Daniel M on 16 Nov 2019
Well I can't provide a more specific answer unless you ask a more specific question. If you uploaded some data along with some examples of your expected outputs then I can try to help.

Sign in to comment.


Linas Svilainis
Linas Svilainis on 2 Dec 2019
Yes, essential problem is that signal is sampled wrong. Majority of signal processing techniques assume periodical signal (especially xcorr). Try to make several copies of your signal fragment and put them next to each other-does your real signal looks like this? Probably not.
1.Try to increase the sampling range.
2. Try using subsample delay estimation:
if you will combine 1 and 2 results should be acceptable

  1 Comment

Zhikun Wang
Zhikun Wang on 5 Dec 2019
Thanks for your advice! I will try that to see if it works.

Sign in to comment.


Linas Svilainis
Linas Svilainis on 5 Dec 2019
Of course, it would be helpfull to increase the record length first, if possible.

  0 Comments

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by