# Approach to Determine Short-Term Delay Between Two Signals

If I have two signals(image) $A$ and $B$, how to determine their displacement(shift) by using a specific window size and search length? Figure below show two signals(green and red) with the locally matched displacement(blue, in pixels) using window of size $18$ pixels and search length of $10$ pixels both ways. How to perform this operation in MATLAB? Can the imabsdiff function in MATLAB do this?

Partial code:

A=rand(1,300);
B=rand(1,300);

plot(A);
hold on;
plot(B);
plot(imabsdiff(A,B));


Portion of actual data:

A=[8867.57405148489 9402.91911342364    9260.13066495026    9345.56275526595    9066.54029280105    8894.01936371044    8412.68945486749    8919.64865493583    8727.95696825579    9074.89217283470    9608.00631088673    9705.23552482552    9925.36706001762    10527.5169206531    10771.2414339404    10191.8372768611    10521.7823136932    10582.8872241675    10343.1075045963    9769.05178493597];

B=[9158.03544409186 9429.74701058754    9528.06160406599    9236.34072710901    9183.22768932609    8956.44915466926    8429.70083898040    9144.08305356044    9093.35936685047    8833.96896404156    9507.13829962295    9773.79833257334    10153.9486443920    10251.5748622237    10352.4685365480    9949.82669971615    10202.3036826683    10557.1335972593    10497.4113150230    10159.5236764117];


• I don't understand – in your question, you say $B$ is a shifted version of $A$, but in your code, the two are totally independent from each other... – Marcus Müller Mar 27 '17 at 13:11
• It should, I just conceptually represent the data. However I did attached the actual data of $A$ and $B$ of $1x20$(after edited), which should do. Thanks! – Gregor Isack Mar 27 '17 at 13:20
• so, what's the exact question, then? do you have an algorithm in your head but can't express it in Matlab? Or is there something else stopping you? – Marcus Müller Mar 27 '17 at 13:24
• I can't find the way to integrate the window and search length into the imabsdiff function. $A$ and $B$ are actually the summation of intensity on x-axis. Which from there I wanted to find the displacement as shown in the blue line. Direct usage of imabsdiff will give me the difference in the intensity, obviously. But from there I can't figure it out how to find the displacement in pixel for every pixel. – Gregor Isack Mar 27 '17 at 13:29
• I'm following the method suggested in this paper. (Section A paragraph number 2). Perhaps you could have a look. Thanks again. – Gregor Isack Mar 27 '17 at 13:33

Unless I misunderstood completely, you are looking for the lag between the two waveforms red and green over smaller windowed intervals of that function as the window is moved over the sequence? In this case it can simply be done with a moving window using the following function in Matlab:

[corr, lag] = xcorr(a,b)


This will return the cross-correlation as corr, and the lag variable is the delay between a and b associated with each cross correlation.

Given you want to do this as a moving window, that can be achieved by setting a window size and index, and then index the window through your data:

Note this may be pseudo code below as I haven't tested the actual code written, but should give you the idea, as shown is a moving window of size K indexing 1 sample at a time, and will compute the relative delay between the waveforms within the window view.

x= green-mean(green);    % remove mean
x=x(:)';                 % ensure row vector
y = red-mean(red);
y=y(:)';
[~,n]=size(x);

for shift = 1:n-K    % ensure x and y same size
xwin= x(shift:shift+K); % K is the window size
ywin= y(shift:shift+K);
[corr,lag]= xcorr(xwin,ywin);
disp(shift)= lag(corr==max(corr));
end


If this is on track to what you are looking for, I would also consider normalizing the correlation by the standard deviation of the two waveforms within each window, thus giving you the correlation coefficient. This could allow for the option of optimally weighting the results for estimation of overall delay (or ignoring regions where the match was poor etc).

• Hi Dan, any ideas why the disp(shift) returned all zeros values? From your profile I see you're expert in FPGA, since what I'm doing now is portion of FPGA, you might understood this equation: $SAD(x,y,u,v)=\sum^W_{dx=-W}\sum^W_{dy=-W}|I_{t}(x+dx,y+dy)-I_{t+1}(x+u+dx,y+v+dy)|$ ,$(u, v)$ which minimizes $SAD(x, y, u, v)$ gives the offset to the best matching region in the search area. Is this produce the same result to your method above? Thanks! – Gregor Isack Mar 28 '17 at 1:51
• Where did disp(shift) come from? I do not see it anywhere above? No that equation does not jump out at me as giving a better result than a correlation--- am I on track that you want the delay between the waveform specific to a windowed section of the waveform as you move the window through from the start to end of the sequence? – Dan Boschen Mar 28 '17 at 1:55
• See my additional updates to the code if you are trying it. – Dan Boschen Mar 28 '17 at 2:04
• Yes correct, the approach I listed was for matching a complete signal that is within the smaller window, to the other. However, given that there is noise in the image, I would think the approach I gave would give you a very good pixel by pixel result (since I shift one pixel at a time); it is just using all the pixels within the window to come up with that estimate. Please know that I have no real experience with 2D image processing if this requires that background then my post will may not be helpful (and I will delete it). I am approaching it from my "comm" signal processing background. – Dan Boschen Mar 28 '17 at 2:18
• oh that's great. So happy. If you're really curious look into the STFT as some of the concepts I did are similar there, just in the time domain instead of the frequency domain. – Dan Boschen Mar 28 '17 at 2:51