Main Content

finddelay

Estimate delay(s) between signals

Description

d = finddelay(x,y) returns an estimate of the delay d between input signals x and y. Delays in x and y can be introduced by prepending zeros.

example

d = finddelay(x,y,maxlag) uses maxlag to find the estimated delay(s) between x and y.

example

Examples

collapse all

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)
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)
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)
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)
D = 
-1

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)
D = 1×4

     0    -1     1     1

Specify X and Y of the same size. finddelay works column-by-column.

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)
D = 1×4

     0     1    -2    -1

Repeat the computation, but now add an extra row of zeros as the second row of Y.

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)
D = 1×4

     1     2    -1     0

Create two multichannel signals, X and Y, such that each channel of Y has a delayed identical copy of each channel of X.

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]';

Compute the column-by-column delays. Set a maximum correlation window size of 8 for each channel.

maxlag = [8 8];
D = finddelay(X,Y,maxlag)
D = 1×2

     3    -5

Decrease the correlation window size to 3 for the first channel and 5 for the second.

maxlag = [3 5];
D = finddelay(X,Y,maxlag)
D = 1×2

     3    -5

Increase the correlation window size to 5 for the first channel and decrease it to 3 for the second.

maxlag = [5 3];
D = finddelay(X,Y,maxlag)
D = 1×2

     3    -3

Input Arguments

collapse all

Reference input, specified as a vector or a matrix.

Input signal, specified as a vector or a matrix.

Maximum correlation window size, specified as an integer scalar or vector. 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.

Output Arguments

collapse all

Delay between input signals, returned as an integer scalar or 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. 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.

If 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), finddelay returns a row vector d of estimated delays between each column of x and the corresponding column of y. With this usage, x and y must have the same number of columns (NX = NY).

Tips

  • 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.

  • The calculation of the vector of estimated delays, d, depends on x, y, and maxlag as shown in the table.

    maxlagXYD
    Integer-valued scalarRow or column vector or matrixRow or column vector or matrixCross-correlation of the columns of X and Y over a range of lags from –maxlag to maxlag.
    Integer-valued row or column vectorRow or column vector of length LX ≥ 1Matrix of size MY-by-NY (MY > 1, NY > 1)Cross-correlation of X and column j of Y over a range of lags from –maxlag(j) to 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 LY ≥ 1Cross-correlation of column j of X and Y over a range of from lags –maxlag(j) to 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-correlation of column j of X and column j of Y over a range of lags from –maxlag(j) to maxlag(j), for j = 1, …, NY.

  • 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:

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

    xm = [1 1 1 1;
          0 0 0 0];
    

Algorithms

The finddelay function uses the xcorr function 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.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2007a

expand all