Determine time lag from xcorr?
28 views (last 30 days)
Show older comments
Hello everyone,
I am still trying to interpret the lag/timings of my own data.
My two vectors e.g A and B are both of length 26, since each data point corresponds to my time vector (in seconds)[0.18:0.01:0.43] also containing a total of 26 time points.
If conducting xcorr(A,B) gives me a peak R correlation at a lag e.g 8, then would my actual lag in the timeseries in seconds be 8*0.01 i.e a lag of 80 ms?
Thank you very much!
0 Comments
Answers (2)
Chunru
on 16 Jul 2022
doc xcorr for details.
[r,lags] = xcorr(___)
% Find the peak or r
[pk, idx] = max(r); % for example
lags(idx)/fs % time lag in sec
Chunru
on 17 Jul 2022
circshift with a positive shift number is to delay the signal A to form B.
Now exeamine the defination of xcorr in MATLAB:
Rxy(m)=E{x(n+m)y'(n)}=E{x(n)'y(n−m)},
If the second signal y=B (or m is negative) is advanced, then it match with the first signal. So the tag at peak is negative.
clearvars; close all; clc
time = [0.18:0.01:0.43]; %my time vector in seconds i.e every time I have taken a data sample
%and obtained a corresponding value stored in vectors A and B.
n =1:26;
A = 0.84.^n;
B = circshift(A,5);
plot(time, A, 'r', time, B, 'b')
figure
[c,lags] = xcorr(A,B);
figure; stem(lags,c)
[pk, idx] = max(c);
timelag = lags(idx) %My lag is -5
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!