Hello,
I am trying to find out the delay between two signals. I have the following Python example:
# 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:
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
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.
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.
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.
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).
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.