Documentation Center

  • Trial Software
  • Product Updates

finddelay

Estimate delay(s) between signals

Syntax

D = finddelay(X,Y)
D = finddelay(...,MAXLAG)

Description

D = finddelay(X,Y), where X and Y are row or column vectors, returns an estimate of the delay D between X and Y, where X serves as the reference vector. If Y is delayed with respect to X, then D is positive. If Y is advanced with respect to X, then D is negative. Delays in X and Y can be introduced by pre-pending zeros.

X and Y need not be exact delayed copies of each other, as finddelay(X,Y) returns an estimate of the delay via cross-correlation. However this estimated delay has a useful meaning only if there is sufficient correlation between delayed versions of X and Y. Also, if several delays are possible, as in the case of periodic signals, the delay with the smallest absolute value is returned. In the case that both a positive and a negative delay with the same absolute value are possible, the positive delay is returned.

D = finddelay(X,Y), where X is a matrix of size MX-by-NX (MX>1 and NX>1) and Y is a matrix of size MY-by-NY (MY>1 and NY>1), returns a row vector D of estimated delays between each column of X and the corresponding column of Y. With this usage the number of columns of X must be equal to the number of columns of Y (i.e., NX=NY).

D = finddelay(...,MAXLAG), uses MAXLAG as the maximum correlation window size used to find the estimated delay(s) between X and Y. The usage of MAXLAG is detailed in the table below.

By default, MAXLAG is equal to MAX(LX, LY)-1 for two vector inputs (where LX and LY are the lengths of X and Y, respectively), MAX(MX, MY)-1 for two matrix inputs, and MAX(LX, MY)-1 or MAX(MX, LY)-1 for one vector input and one matrix input. If MAXLAG is input as [], it is replaced by the default value. If any element of MAXLAG is negative, it is replaced by its absolute value. If any element of MAXLAG is not integer-valued, or is complex, Inf, or NaN, then finddelay returns an error.

The calculation of the vector of estimated delays, D, depends on X, Y, and MAXLAG as shown in the following table.

MAXLAGXYD is calculated by...
Integer-valued scalarRow or column vector or matrixRow or column vector or matrixCross-correlating the columns of X and Y over a range of lags -MAXLAG:MAXLAG.
Integer-valued row or column vectorRow or column vector of length Matrix of size MY-by-NY (MY>1, NY>1)Cross-correlating X and column j of Y over a range of lags -MAXLAG(j):MAXLAG(j), for j=1:NY.
Integer-valued row or column vectorMatrix of size MX-by-NX (MX>1, NX>1)Row or column vector of length Cross-correlating column j of X and Y over a range of lags -MAXLAG(j):MAXLAG(j), for j=1:NX.
Integer-valued row or column vectorMatrix of size MX-by-NX (MX>1, NX>1)Matrix of size MY-by-NY (MY>1, NY=NX>1)Cross-correlating column j of X and column j of Y over a range of lags -MAXLAG(j):MAXLAG(j), for j=1:NY.

Treating X as Multiple Channels

If you wish to treat a row vector X of length LX as comprising one sample from LX different channels, you need to append one or more rows of zeros to X so that it appears as a matrix. Then each column of X will be considered a channel.

For example, X = [1 1 1 1] is considered a single channel comprising four samples. To treat it as four different channels, each channel comprising one sample, define a new matrix Xm:

Xm = [1 1 1 1;
      0 0 0 0];

Each column of Xm corresponds to a single channel, each one containing the samples 1 and 0.

Examples

X and Y Are Vectors, and MAXLAG Is Not Specified

The following shows Y being delayed with respect to X by two samples.

X = [1 2 3];
Y = [0 0 1 2 3];
D = finddelay(X,Y)

The result is D = 2.

Here is a case of Y advanced with respect to X by three samples.

X = [0 0 0 1 2 3 0 0]';
Y = [1 2 3 0]';
D = finddelay(X,Y)

The result is D = -3.

The following illustrates a case where Y is aligned with X but is noisy.

X = [0 0 1 2 3 0];
Y = [0.02 0.12 1.08 2.21 2.95 -0.09];
D = finddelay(X,Y)

The result is D = 0.

If Y is a periodic version of X, the smallest possible delay is returned.

X = [0 1 2 3];
Y = [1 2 3 0 0 0 0 1 2 3 0 0];
D = finddelay(X,Y)

The result is D = -1.

X is a Vector, Y a Matrix, and MAXLAG Is a Scalar

MAXLAG is specified as a scalar (same maximum window sizes).

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)

The result is D = [0 -1 1 1].

X and Y Are Matrices, and MAXLAG Is Not Specified

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)

The result is D = [0 1 -2 -1].

X and Y Are Matrices, and MAXLAG Is Specified

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];
MAXLAG = [10 10 20 20];
D = finddelay(X,Y,MAXLAG)

The result is D = [0 1 -2 -1].

More About

expand all

Algorithms

The finddelay function uses the xcorr function of Signal Processing Toolbox to determine the cross-correlation between each pair of signals at all possible lags specified by the user. The normalized cross-correlation between each pair of signals is then calculated. The estimated delay is given by the negative of the lag for which the normalized cross-correlation has the largest absolute value.

If more than one lag leads to the largest absolute value of the cross-correlation, such as in the case of periodic signals, the delay is chosen as the negative of the smallest (in absolute value) of such lags.

Pairs of signals need not be exact delayed copies of each other. However, the estimated delay has a useful meaning only if there is sufficient correlation between at least one pair of the delayed signals.

See Also

|

Was this topic helpful?