How to determine time lag by using xcorr ?

조회 수: 119 (최근 30일)
Linda
Linda 2020년 3월 31일
댓글: Linda 2020년 4월 7일
Hello everyone,
I am trying to determine the time lag between two signals delayed with the function xcorr but I'm not sure I'm doing it the right way.
Here's the code that i am using:
[cor,lag]=xcorr(SIGNAL,SIGNAL2);
[~,I]=max(abs(cor));
lagDiff=lag(I);
Time_Diff=lagDiff*DT;
Where DT is the sampling time in sec,
I am not sure si Time_Diff is giving the real time Lag between the two signals.

채택된 답변

David Goodmanson
David Goodmanson 2020년 4월 1일
Hello Linda,
the idea is basically correct, but for two signals of the same length, zero lag is at the center of the resulting xcorr array. The following example shows two identical waveforms with a realative time shift of 4 sec, and the xcorr plot has a peak at the right location.
delt = 1e-3;
t = (-20e3:20e3)*delt;
[tt n1] = meshgrid(t,rand(1,16));
[~, n2] = meshgrid(t,rand(1,16));
y1 = sum(exp(-((tt ).^2/10)).*(cos(77*(tt ).*n1 + 2*pi*n2)));
tsh = 4; % time shift for y2
y2 = sum(exp(-((tt-tsh).^2/10)).*(cos(77*(tt-tsh).*n1 + 2*pi*n2)));
tcorr = (-40e3:40e3)*delt; % time array for correlation function
figure(1)
plot(t,y1,t,y2)
grid on
figure(2)
plot(tcorr,xcorr(y2,y1))
grid on
  댓글 수: 3
David Goodmanson
David Goodmanson 2020년 4월 2일
Hi LInda,
It's really the same thing. Using a time array I constructed two signals, where one of them lags the other by 4 sec. So in line with how you are looking at this, suppose we plot each function in terms of its array index, and look at xcorr in terms of its array index
delt = 1e-3;
t = (-20e3:20e3)*delt;
[tt n1] = meshgrid(t,rand(1,16));
[~, n2] = meshgrid(t,rand(1,16));
y1 = sum(exp(-((tt ).^2/10)).*(cos(77*(tt ).*n1 + 2*pi*n2)));
tsh = 4; % time shift for y2
y2 = sum(exp(-((tt-tsh).^2/10)).*(cos(77*(tt-tsh).*n1 + 2*pi*n2)));
% same up to this point
%tcorr = (-40e3:40e3)*delt; % time array for correlation function
n = length(y1);
indices = 1:n;
figure(1)
plot(indices,y1,indices,y2)
grid on
indices_corr = 1:2*n-1; % corellation takes 2*n-1 points
figure(2)
plot(indices_corr,xcorr(y2,y1))
grid on
The y1 and t2 arrays happen to have n = 40001 points by constuction, The correlation array has 2*n-1 = 80001 points. For correlation, zero lag is right in the center of this array, which is point 40001.
One way or another you are going to have to find the maximum of xcorr in order to determine the lag. That can be done either by finding the position of the max or by looking at the plot. Zooming in on figure 2 the max is at array point 44001. That differs from the zero lag point by exactly 4000 points. Then in accord with the way you are looking at this, DT = .001 and, 4000*DT = 4 sec.
All I did was come up with a time array that mimics this process, so you can read time delay directly off the plot. Hope this helps.
Linda
Linda 2020년 4월 7일
Thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by