how to calculate time lag in two signal using cross correlation
Show older comments
hi all,i m facing some problem with finding the time lag between two signals. i imported the data and used the cross correlation function.it gives 74 but according to my calculations it should be 32. i don't understand where i m wrong. please chk it out
t=end100(:,1);
>> ux228=start228(:,2);
>> ux100=end100(:,2);
>> Y=xcorr(ux228,ux100);
>> plot(Y);
>> find(Y==max(Y))
ans =
74
>> N=length(t);
>> R=Y(1:N);
>> Rx=fliplr(R);
>> Plot (Rx);
Answers (1)
Wayne King
on 15 Sep 2012
Edited: Wayne King
on 15 Sep 2012
You have to keep in mind:
1.) xcorr is returning negative lags as well as positive, but for real-valued inputs the cross-correlation sequence is even
2.) you can return the lags argument
Create an example with a lag of 10 between the two vectors.
x = randn(100,1);
y = [zeros(10,1); x];
% xcorr returns 2*length of the longest vector-1 lags by default
% therefore lag 110 is the zero-th lag
[xc,lags] = xcorr(y,x);
xc = xc(110:end);
lags = lags(110:end);
[val,index] = max(abs(xc));
lags(index)
The above gives a lag of 10, which is the correct lag.
Also see the examples and how tos for estimating delay on this page:
< < https://www.mathworks.com/help/signal/convolution-and-correlation.html>
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!