[Xa,Ya]
= alignsignals(X,Y,maxlag) uses maxlag as
the maximum window size to find the estimated delay D between
the two input signals, X and Y.
It returns the aligned signals, Xa and Ya.

[Xa,Ya]
= alignsignals(X,Y,maxlag,'truncate') keeps
the lengths of the aligned signals, Xa and Ya,
the same as those of the input signals, X and Y,
respectively.

If the estimated delay D is positive,
then D zeros are prepended to X and
the last D samples of X are
truncated.

If the estimated delay D is negative,
then –D zeros are prepended to Y and
the last –D samples of Y are
truncated.

NotesX and Y are row or
column vectors of length LX and LY,
respectively.

If D ≥ LX,
then Xa consists of LX zeros.
All samples of X are lost.

If –D ≥ LY,
then Ya consists of LY zeros.
All samples of Y are lost.

To avoid assigning a specific value to maxlag when
using the 'truncate' option, set maxlag to [].

Invoke the 'truncate' option
when calling the alignsignals function.

Create two signals, X and Y. Y is
exactly the same as X, except Y has
two leading zeros. Align the two signals, applying the 'truncate' directive.

X = [1 2 3];
Y = [0 0 1 2 3];
[Xa Ya D] = alignsignals(X, Y, [], 'truncate');

Observe that the output signal Xa has a length
of 3, the same length as input signal X.

In the case where using the 'truncate' option
ends up truncating all the original data of X,
a warning is issued. To make alignsignals issue
such a warning, run the following example.

X = [1 2 3];
Y = [0 0 0 0 1 2 3];
[Xa Ya D] = alignsignals(X, Y, [], 'truncate')

Maximum window size, or lag, specified as an integer-valued
scalar. By default, maxlag is equal to max(length(X),length(Y))-1.
If maxlag is input as [],
it is replaced by the default value. If maxlag is
negative, it is replaced by its absolute value. If maxlag is
not integer valued, or is complex, Inf, or NaN,
then alignsignals returns an error.

Aligned first signal, returned as a numeric vector that is aligned
with the second output argument Ya. If input
argument X is a row vector, then Xa will
also be a row vector. If input argument X is
a column vector, then Xa will also be a column
vector. If you specify the 'truncate' option and
the estimated delay D is positive, then Xa is
equivalent to the input signal X with D zeros
prepended to it and its last D samples truncated.

Aligned second signal, returned as a numeric vector that is
aligned with the first output argument Xa. If
input argument Y is a row vector, then Ya is
also a row vector. If input argument Y is a column
vector, then Ya is also a column vector. If you
specify the 'truncate' option and the estimated
delay D is negative, then Ya is
equivalent to the input signal Y with –D zeros
prepended to it and its last –D samples
truncated.

Estimated delay between input signals, returned as a scalar
integer. This integer represents the number of samples by which the
two input signals, X and Y are
offset.

If Y is delayed with respect
to X, then D is positive
and X is delayed by D samples.

If Y is advanced with respect
to X, then D is negative
and Y is delayed by –D samples.

If X and Y are
already aligned, then D is zero and neither X nor Y are
delayed.

If you specify a value for the input argument maxlag,
then D must be less than or equal to maxlag.

You can find the theory on delay estimation in the
specification of the finddelay function
(see Algorithms).

The alignsignals function uses
the estimated delay D to delay the earliest signal
such that the two signals have the same starting point.

As specified for the finddelay function,
the pair of signals need not be exact delayed copies of each other.
However, the signals can be successfully aligned only if there is
sufficient correlation between them.

For more information on estimating covariance and correlation
functions, see [1].