alignsignals - Align two signals by delaying earliest signal

Syntax

[Xa Ya] = alignsignals(X,Y)
[Xa Ya] = alignsignals(X,Y,MAXLAG)
[Xa Ya] = alignsignals(X,Y,MAXLAG,'truncate')
[Xa Ya D] = alignsignals(...)

Description

[Xa Ya] = alignsignals(X,Y), where X and Y are row or column vectors of length LX and LY, respectively, aligns the two vectors by estimating the delay D between the two. If Y is delayed with respect to X, D is positive, and X is delayed by D samples. If Y is advanced with respect to X, D is negative, and Y is delayed by -D samples. The aligned signals Xa and Ya are returned. Delays in X and Y can be introduced by pre-pending zeros.

[Xa Ya] = alignsignals(X,Y,MAXLAG) uses MAXLAG as the maximum window size used to find the estimated delay D between X and Y. MAXLAG is an integer-valued scalar. By default, MAXLAG is equal to MAX(LX, LY)-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.

[Xa Ya] = alignsignals(X,Y,MAXLAG,'truncate') keeps the lengths of Xa and Ya the same as those of X and Y, respectively. If D is positive, D zeros are pre-pended to X, and the last D samples of X are truncated. If D is negative, -D zeros are pre-pended to Y, and the last -D samples of Y are truncated. Note: If , Xa will consist of LX zeros, and all samples of X are lost. Similarly, if , Ya will consist of LY zeros, and all samples of Y are lost. To avoid assigning a specific value to MAXLAG when using the 'truncate' option, set MAXLAG to [].

[Xa Ya D] = alignsignals(...) returns the estimated delay D.

Theory and Algorithm

The theory on delay estimation can be found in the specification of the finddelay function (see Theory and Algorithm).

The alignsignals function simply uses the estimated delay 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.

Examples

The following illustrates how X is aligned when Y is delayed with respect to X by two samples.

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

The resulting values are:

Xa = [0 0 1 2 3]
Ya = [0 0 1 2 3]
D = 2

The following is a case where Y is advanced with respect to X by three samples.

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

The resulting values are:

Xa = [0 0 0 1 2 3 0 0]'
Ya = [0 0 0 1 2 3 0]' 

The following illustrates a signal Y that is aligned with respect to X but is noisy.

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

The resulting values are:

Xa = [0 0 1 2 3 0]
Ya = [0.02 0.12 1.08 2.21 2.95 -0.09];
D = 0	

The following shows that when Y is a periodic repetition 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];
[Xa Ya D] = alignsignals(X, Y)

The resulting values are:

Xa = [0 1 2 3];
Ya = [0 1 2 3 0 0 0 0 1 2 3 0 0];
D = -1	

Here is an example of alignsignals using the 'truncate' option.

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

The resulting values are:

Xa = [0 0 1];
Ya = [0 0 1 2 3];
D = 2	

In the case where using the 'truncate' option ends up truncating all the original data of X, a warning will be issued. The following example makes MATLAB issue such a warning.

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

See Also

finddelay

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS