Estimate changing time lag between signals
If the signals are as you've drawn them (flat, with abrupt changes), then perhaps this approach might work:


Take the absolute value of the differences between successive time samples.
The non-zero entries are the transition times.
Find these times for both signals.
Subtract the two.


I've made an attempt to do this in the R code below.

Example output:

 &gt; print(differences_in_delay)
  [1]  2 -5  2 -4 -1  5 -4  3 -3  0
 &gt; print(t[which(abs(diff(x2))&gt;0)] - t[which(abs(diff(x1))&gt;0)])
  [1]  2 -5  2 -4 -1  5 -4  3 -3  0


It breaks down when the transitions are within a sample or two of each other, but otherwise seems to work OK.

Will also probably break down if there's noise on your signals.



R Code Below

T &lt;- 1024    
t &lt;- seq(1,T,1)    
Nchanges &lt;- 10    
times_for_changes_for_x1 &lt;- sort(runif(Nchanges,T/10,9*T/10))
differences_in_delay &lt;- round(runif(Nchanges,-5,5))
times_for_changes_for_x2 &lt;- times_for_changes_for_x1 + differences_in_delay
changes_for_x1 &lt;- runif(Nchanges,-5,5)

x1 &lt;- 0*t
x2 &lt;- 0*t
change_no_x1 &lt;- 1
change_no_x2 &lt;- 1

for (tm in t)
{
  if ( (change_no_x1 &lt;= Nchanges) &amp;&amp; (tm &gt; times_for_changes_for_x1[change_no_x1]))
  {
    x1[tm] &lt;- x1[tm] + changes_for_x1[change_no_x1]
    change_no_x1 &lt;- change_no_x1 + 1
  }
  else
  {
    if (tm &gt; 1)
    {
      x1[tm] = x1[tm-1]
    }
  }
  if ( (change_no_x2 &lt;= Nchanges) &amp;&amp; (tm &gt; times_for_changes_for_x2[change_no_x2]))
  {
    x2[tm] &lt;- x2[tm] + changes_for_x1[change_no_x2]
    change_no_x2 &lt;- change_no_x2 + 1
  }
  else
  {
    if (tm &gt; 1)
    {
      x2[tm] = x2[tm-1]
    }
  }
}

plot(x1,type='l')
lines(x2,col="red")

plot(abs(diff(x1)&gt;0)*1, type='l')
lines(abs(diff(x2)&gt;0)*1, col="red")


print(differences_in_delay)
print(t[which(abs(diff(x2))&gt;0)] - t[which(abs(diff(x1))&gt;0)])