How to determine time lag by using xcorr ?

117 Ansichten (letzte 30 Tage)
Linda
Linda am 31 Mär. 2020
Kommentiert: Linda am 7 Apr. 2020
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.

Akzeptierte Antwort

David Goodmanson
David Goodmanson am 1 Apr. 2020
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 Kommentare
David Goodmanson
David Goodmanson am 2 Apr. 2020
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 am 7 Apr. 2020
Thanks a lot!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by