You are here : matlabSignal Processingfinddelay

finddelay() - Signal Processing

```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 prepending 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 LX ≥ 1Matrix 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 LY ≥ 1Cross-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 ChannelsIf 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.```

Syntax

`D = finddelay(X,Y)D = finddelay(...,maxlag)`

Example

```X and Y Are Vectors, and maxlag Is Not SpecifiedOpen This Example
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

X Is a Vector, Y Is a Matrix, and maxlag Is a ScalarOpen This Example
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 =

0    -1     1     1

X and Y Are Matrices, and maxlag Is Not SpecifiedOpen This Example
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 =

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     2    -1     0

X and Y Are Matrices, and maxlag Is SpecifiedOpen This Example
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 =

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 =

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 =

3    -3```