finddelay
Estimation of the delay between signals.
| Library |
|
Arguments
Input arguments
# x is the reference input signal
+
vector | the matrix
Details
A reference input signal specified as a vector or matrix.
# maxlag — maximum correlation window size
+
scalar | vector
Details
The maximum size of the correlation window, set as an integer scalar or vector. If any element maxlag If it is negative, then it is replaced by its absolute value. If any element maxlag is not an integer, is complex, is equal to Inf or NaN Then finddelay returns an error.
Output arguments
# d — delay
+
scalar | vector
Details
The delay between the input signals, returned as an integer scalar or vector. If y delayed relative x Then d positively. If y ahead of x Then d negative. If multiple delays are possible, as in the case of periodic signals, then the delay with the lowest absolute value is returned. If both positive and negative delays with the same absolute values are possible, a positive delay is returned.
Examples
Input signals X and Y — vectors, the maximum size of the correlation window maxlag not specified
Details
An example when Y delayed relative X for two counts:
import EngeeDSP.Functions: finddelay
X = [1 2 3]
Y = [0 0 1 2 3]
D = finddelay(X,Y)
print("D = ", D)
D = 2
An example when Y ahead of X For three counts:
import EngeeDSP.Functions: finddelay
X = [0 0 0 1 2 3 0 0]'
Y = [1 2 3 0]'
D = finddelay(X,Y)
print("D = ", D)
D = -3
An example when Y matches with X, but it is noisy:
import EngeeDSP.Functions: finddelay
X = [0 0 1 2 3 0]
Y = [0.02 0.12 1.08 2.21 2.95 -0.09]
D = finddelay(X,Y)
print("D = ", D)
D = 0
An example when Y It is a periodic version X and the lowest possible delay is returned.:
import EngeeDSP.Functions: finddelay
X = [0 1 2 3]
Y = [1 2 3 0 0 0 0 1 2 3 0 0]
D = finddelay(X,Y)
print("D = ", D)
D = -1
The input signal X — vector, Y — matrix, the maximum size of the correlation window maxlag — scalar
Details
An example when the signal X — vector, Y — matrix, the maximum size of the correlation window maxlag — scalar:
import EngeeDSP.Functions: finddelay
X = [0 1 2]
Y = [0 1 0 0;
1 2 0 0;
2 0 1 0;
0 0 2 1]
maxlag = 3
D = finddelay(X,Y,maxlag)
print("D = ", D)
D = [0 -1 1 1]
Input signals X and Y — matrices, the maximum size of the correlation window maxlag not specified
Details
An example when the signal X and Y — matrices of the same dimension:
import EngeeDSP.Functions: finddelay
X = [0 1 0 0;
1 2 0 0;
2 0 1 0;
1 0 2 1;
0 0 0 2]
Y = [0 0 1 0;
1 1 2 0;
2 2 0 1;
1 0 0 2;
0 0 0 0]
D = finddelay(X,Y)
print("D = ", D)
D = [0 1 -2 -1]
Now add to Y one more line:
import EngeeDSP.Functions: finddelay
X = [0 1 0 0;
1 2 0 0;
2 0 1 0;
1 0 2 1;
0 0 0 2]
Y = [0 0 1 0;
0 0 0 0;
1 1 2 0;
2 2 0 1;
1 0 0 2;
0 0 0 0]
D = finddelay(X,Y)
print("D = ", D)
D = [1 2 -1 0]
Input signals X and Y — matrices, the maximum size of the correlation window maxlag — vector
Details
Let’s create two multi-channel signals X and Y so that each channel Y had a delayed identical copy of each channel X. Let’s set the maximum size of the correlation window 8 for each channel, we will calculate the delays by columns:
import EngeeDSP.Functions: finddelay
X = [1 3 2 0 0 0 0 0;
0 0 0 0 0 1 3 2]'
Y = [0 0 0 1 3 2;
1 3 2 0 0 0]'
maxlag = [8 8]
D = finddelay(X,Y,maxlag)
print("D = ", D)
D = [3 -5]
Reduce the size of the correlation window to 3 for the first channel and before 5 for the second one:
import EngeeDSP.Functions: finddelay
X = [1 3 2 0 0 0 0 0;
0 0 0 0 0 1 3 2]'
Y = [0 0 0 1 3 2;
1 3 2 0 0 0]'
maxlag = [3 5]
D = finddelay(X,Y,maxlag)
print("D = ", D)
D = [3 -5]
Increase the size of the correlation window to 5 for the first channel and reduce it to 3 for the second one:
import EngeeDSP.Functions: finddelay
X = [1 3 2 0 0 0 0 0;
0 0 0 0 0 1 3 2]'
Y = [0 0 0 1 3 2;
1 3 2 0 0 0]'
maxlag = [5 3]
D = finddelay(X,Y,maxlag)
print("D = ", D)
D = [3 -3]
Recommendations
-
Arguments
xandythey don’t have to be exact retarded copies of each other, becausefinddelay(x, y)returns an estimate of the delay by cross-correlation. However, this estimate of the delay only makes sense if there is a sufficient correlation between the delayed versions.xandy. -
Calculation of the vector of estimated delay values
ddepends onx,yandmaxlagas shown in the table.maxlagxydAn integer scalar
Row vector, column vector, or matrix
Row vector, column vector, or matrix
Cross-correlation of columns
xandyin the range of the delay period from−maxlagbeforemaxlag.A row vector or column vector with integer values
Row vector, column vector of length
A matrix of dimension on ( )
Cross-correlation
xand the column matricesyin the range of the delay period from−maxlagbeforemaxlagfor .A row vector or column vector with integer values
A matrix of dimension on ( )
Row vector, column vector of length
Column cross-correlation matrices
xandyin the range of the delay period from−maxlagbeforemaxlagfor .A row vector or column vector with integer values
A matrix of dimension on ( )
A matrix of dimension on ( )
Column cross-correlation matrices
xand the column matricesyin the range of the delay period from−maxlagbeforemaxlagfor . -
If you want to consider a vector string
xlengths as containing a single count from of the various channels, it is necessary to add toxone or more rows of zeros so that it takes the form of a matrix. Then each columnxit will be considered a channel.For example,
x = [1 1 1 1]It is considered to be one channel containing four samples. To treat it as four different channels, each containing one sample, define a new matrixxm, such that each columnxmcorresponds to one channel, each of which contains samples1and0.
xm = [1 1 1 1;
0 0 0 0]
Algorithms
Function finddelay uses the function xcorr to determine the cross-correlation between each pair of signals at all possible delays specified by the user. Then the normalized cross-correlation between each pair of signals is calculated. The estimated delay is defined as the negative value of the delay for which the normalized cross-correlation has the highest absolute value.
If multiple delays are possible, as in the case of periodic signals, then the delay with the lowest absolute value is returned. If both positive and negative delays with the same absolute values are possible, a positive delay is returned.
Pairs of signals do not have to be exact retarded copies of each other. However, the estimated delay is meaningful only if there is sufficient correlation between at least one pair of delayed signals.