# Determine the delay between two signals

**URL:** https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140
**Category:** Signal and Image Processing
**Tags:** question
**Created:** [January 23, 2024, 1:52pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140 "2024-01-23T13:52:38Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [January 23, 2024, 1:52pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/1 "2024-01-23T13:52:38Z")

</div>

Hello,  
I am trying to find out the delay between two signals. I have the following Python example:

```julia
# Cross-correlate 
correlation = correlate(x, y, 'full')

# Get the lag vector that corresponds to the correlation vector
lags = correlation_lags(x.size, y.size, mode="full")

# Find the lag at the peak of the correlation
lag = lags[np.argmax(correlation)]
print(lag)

```

My Julia code so far:

```julia
using StatsBase

function delay(x, y)
    z = crosscor(x, y)
    ind = argmax(z)
end

```

But there is still something like the Python function `correlation_lags` missing, see: [scipy.signal.correlation\_lags — SciPy v1.12.0 Manual](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.correlation_lags.html)  
If I call my delay function with the same vector for both arguments I get a result that is not zero, but the delay in this test case should be zero.

Any idea?

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [January 23, 2024, 2:10pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/2 "2024-01-23T14:10:44Z")

</div>

Try [DSP.xcorr.](https://docs.juliadsp.org/stable/convolutions/)

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [January 23, 2024, 2:19pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/3 "2024-01-23T14:19:30Z")

</div>

Different result, but not better. I want to get the delay between the two signals in time-steps.

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [January 23, 2024, 2:28pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/4 "2024-01-23T14:28:10Z")

</div>

```julia
crosscor(x, y)

When left unspecified, the lags used are the integers from `-min(size(x,1)-1, 10*log10(size(x,1)))` to `min(size(x,1), 10*log10(size(x,1)))`

```

so it means zero lag is in the middle of z if you use delay with the same vector for both arguments.

---

<div class="post-metadata">

### Author: ![mgkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgkuhn/32/6276_2.png) [@mgkuhn](https://discourse.julialang.org/u/mgkuhn)
#### Post date: [January 23, 2024, 2:36pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/5 "2024-01-23T14:36:55Z")

</div>

There probably ought to be a variant of `DSP.xcorr` that returns an `OffsetArray`, such that the indices are lags. And that should ideally also work correctly if any of the inputs is `OffsetArray`. Also a `maxlag` option to truncate the output would be nice.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [January 23, 2024, 2:39pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/6 "2024-01-23T14:39:43Z")

</div>

```julia
using StatsBase

T=0:0.1:10
X=sin.(T)
Y=sin.(T.-0.5)
plot(T,X)
plot(T,Y)

z = StatsBase.crosscor(X, Y)
delay=argmax(z)

println(delay)

```

This prints a delay of 26, but the expected delay is 0.5s = 5 time steps. How can I get the correct result?

---

<div class="post-metadata">

### Author: ![mgkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgkuhn/32/6276_2.png) [@mgkuhn](https://discourse.julialang.org/u/mgkuhn)
#### Post date: [January 23, 2024, 2:46pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/7 "2024-01-23T14:46:28Z")

</div>

In your case, the answer should probably be:

```julia
delay=argmax(z) - length(T)

```

It is more tricky if `length(X) != length(Y)`, as then there is the question what the zero alignment between the two sequences should be (e.g. at start/end/center).

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [January 23, 2024, 2:47pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/8 "2024-01-23T14:47:07Z")

</div>

With the default lags used in corrcor, the zero delay is in the center, so you could just subtract that index:

```julia
using StatsBase
T=0:0.1:10
X=sin.(T)
Y=sin.(T.-0.5)

z = StatsBase.crosscor(X, Y)
delay=argmax(z)

println(delay-div((size(z,1)+1),2))

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [January 23, 2024, 2:48pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/9 "2024-01-23T14:48:29Z")

</div>

Sorry, that doesn’t work.

---

<div class="post-metadata">

### Author: ![gabo-di](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabo-di/32/206427_2.png) [@gabo-di](https://discourse.julialang.org/u/gabo-di)
#### Post date: [January 23, 2024, 2:56pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/10 "2024-01-23T14:56:36Z")

</div>

Something like this?

```julia
using StatsBase

function delay(x, y)
    lags = (-length(y)+1):(length(x)-1)
    z = crosscor(x, y, lags)
    ind = argmax(z)
    lags[ind]
end

```

but scipy’s correlate has more options, this is just an sketch.

---

<div class="post-metadata">

### Author: ![mgkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgkuhn/32/6276_2.png) [@mgkuhn](https://discourse.julialang.org/u/mgkuhn)
#### Post date: [January 23, 2024, 3:05pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/11 "2024-01-23T15:05:08Z")

</div>

Ah, `StatsBase.crosscor` has a weird default range for `lags`, which you may need to override for longer inputs.

```julia
T=0:0.1:10
X=sin.(T)
Y=sin.(T.-0.5)
z = StatsBase.crosscor(X, Y, -(length(T)-1):(length(T)-1))
delay=argmax(z)-length(T)

5

```

But note that this may be biased in favour of large overlaps.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [January 23, 2024, 3:29pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/12 "2024-01-23T15:29:55Z")

</div>

See also

> **[GitHub - baggepinnen/SignalAlignment.jl: Align signals to each other](https://github.com/baggepinnen/SignalAlignment.jl/)**
>
> Align signals to each other. Contribute to baggepinnen/SignalAlignment.jl development by creating an account on GitHub.

for more ways of computing the delay between signals

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 24, 2024, 7:39pm UTC](https://discourse.julialang.org/t/determine-the-delay-between-two-signals/109140/13 "2024-01-24T19:39:51Z")

</div>

The cross-correlation method presented above provides an integer delay (whole samples). To refine this and also obtain the fractional delay (fraction of a sample period), a simple method is to fit a parabola around the cross-correlation peak and obtain the abscissa of the apex.
