how can I find time delay between two signals with corrolation?

12 views (last 30 days)
I have two signal and they have a time delay like following graph. t is red one and v is blue one in the code. I wrote following code. I uploaded also mat files which are t26.mat and v16.mat on the graph. So I tried to find index but ı obtained '1' but it is wrong answer. Can anybody help me about this problem ?
if true
t=[t zeros(19,(length(v)-length(t)))];
lt=length(t);
for i=1:lt
c_v{i} = xcorr( v(1,:),[zeros(1,i),t(1,1:(lt-i+1))]) ; % calculate corrolation
end
c=cell2mat(c_v); %cell to mat convertion
[p,q] =max(c); %find max correlation index in cell array
index=floor(q/(2*lt+1)) % how many iteration passed
end
  1 Comment
dpb
dpb on 24 Dec 2014
Edited: dpb on 24 Dec 2014
That plot really looks like the two traces are reversed in time vis a vis each other...otherwise there's very little correlation visible or the blue one wasn't recorded long enough for the second, wider peak area to have been fully collected before the recording ended.
ADDENDUM
OK, spoke a little too hastily...if one tries
plot(t(:,1))
hold on
plot([nan(1,45) v(:,1],'r')
one can see some correlation.
I've got another commitment at the moment (it is Christmas Eve day, after all :) ) at the moment so can't spend much more time at the moment to really play at it but I think you need to attack this by trying to find the places where the two tend to have the max/min segments first, before the correlation. Then, I think you'll have better luck if you truncate the nonoverlapping areas sorta' like I did with the NaN above so they don't show in the plot instead of introducing zeros. Altho that might work if in this case you prepended them instead of postpending...
Is there some trigger signal associated with these that can be used, perhaps as a corollary variable to aid in finding that point? I just crudely estimated the 45 offset value used above by looking at the location of the two trailing edges on the original plot. Perhaps, in fact, finding a large negative slope region between the two traces might be the locating mechanism...

Sign in to comment.

Accepted Answer

dpb
dpb on 24 Dec 2014
Okay, I got back... :)
Looking at a couple of these traces, it appears that basically the trigger for the shorter (v) was perhaps the rising peak of the longer (t)?
Anyway, if I make that assumption for the sample first trace I get
>> delta=length(t)-length(v);
>> [cor,lags]=xcorr([zeros(1,delta) v(1,:)],t(1,:),'coeff');
>> [~,ix]=max(cor);
>> lags(ix)
ans =
7
>> figure
>> plot([nan(1,delta-lags(ix)) v(1,:)],'r')
>> hold on,plot(t(1,:),'b')
>>
looks pretty good. Note take the lag of the max correlation from the offset used to begin the alignment.
If the assumption about the triggering is incorrect, adapt as makes sense knowing the setup/system that generated the data.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!