Azzera filtri
Azzera filtri

Determine time lag from xcorr?

28 visualizzazioni (ultimi 30 giorni)
Alba Peris
Alba Peris il 16 Lug 2022
Risposto: Chunru il 17 Lug 2022
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!

Risposte (2)

Chunru
Chunru il 16 Lug 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
  1 Commento
Alba Peris
Alba Peris il 16 Lug 2022
If, for example, my data is as following:
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);
[c,lags] = xcorr(A,B);
figure; stem(lags,c)
[pk, idx] = max(c);
timelag = lags(idx); %My lag is -5
Would now my lag in seconds be -5*0.01 = 0.05 seconds?
I understand my fs for my vector time is 100 in the example I have given?
Sorry for the confusion. Thank you very much.

Accedi per commentare.


Chunru
Chunru il 17 Lug 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(nm)},
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
timelag = -5

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by