How can i calculate max correlation and time delay between two signals?

33 次查看(过去 30 天)
So, for my code, I am asked to calculate max correlation and time delay between two signals. I already calculated the max correlation, but I need to find the lag value (time delay) at the index the max correlation happened. After doing this, I need to multiply the lag value at the index with the time step to be able to get the time delay. The problem is, I am unsure on how to do this. I'd appreciate some help. Thank you!
Here is my code:
clear;
clc;
load("DeltaP_Vel.txt");
time=DeltaP_Vel(:,1);
press=DeltaP_Vel(:,2);
vrbc=DeltaP_Vel(:,3);
ekg=DeltaP_Vel(:,4);
[cycles,locs]=findpeaks(ekg);
num_cycles= length(cycles)-1;
for i=1:39
diff(i)=locs(i+1)-locs(i);
end
maxsize=max(diff);
p=zeros(num_cycles, maxsize+1);
v=zeros(num_cycles,maxsize+1);
for i=1:num_cycles
start=locs(i);
last=locs(i+1);
p(i,1:diff(i)+1)=press(start:last)';
v(i,1:diff(i)+1)=vrbc(start:last)';
end
en_p=mean(p);
en_v=mean(v);
I_t=length(en_v)*0.005;
t_cycle=0:0.005:I_t-0.005;
% A)
[r,lags]=xcorr(en_p,en_v);
plot(lags,r);
xlabel('Lag Index');
ylabel('Cross Correlation');
title('Lag Index VS Cross Correlation:');
% B)
[maxvalue,index]=max(r);
display(index);
display(maxvalue);

回答(1 个)

KSSV
KSSV 2023-9-29
REad about crosscorr.

Community Treasure Hunt

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

Start Hunting!

Translated by