alignsignals

Align two signals by delaying earliest signal

Description

example

[Xa,Ya] = alignsignals(X,Y) estimates the delay, D, between the two input signals, X and Y, and returns the aligned signals, Xa and Ya.

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

Delays in X or Y can be introduced by prepending zeros.

example

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

example

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

Notes

X 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 [].

example

[Xa,Ya,D] = alignsignals(___) returns the estimated delay, D. This syntax can include any of the input arguments used in previous syntaxes.

Examples

collapse all

Align signal Y with respect to X by delaying it three samples.

Create two signals, X and Y. X is exactly the same as Y, except X has three leading zeros and one additional following zero. Align the two signals.

X = [0 0 0 1 2 3 0 0];
Y = [1 2 3 0];

[Xa,Ya] = alignsignals(X,Y)
Xa = 1×8

0     0     0     1     2     3     0     0

Ya = 1×7

0     0     0     1     2     3     0

Align signal X when Y is delayed with respect to X by two samples.

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

X = [1 2 3];
Y = [0 0 1 2 3];
maxlag = 2;

[Xa,Ya,D] = alignsignals(X,Y,maxlag)
Xa = 1×5

0     0     1     2     3

Ya = 1×5

0     0     1     2     3

D = 2

Align signal Y with respect to X, despite the fact that Y is a noisy signal.

Create two signals, X and Y. Y is exactly the same as X with some noise added to it. Align the two signals.

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)
Xa = 1×6

0     0     1     2     3     0

Ya = 1×6

0.0200    0.1200    1.0800    2.2100    2.9500   -0.0900

D = 0

You do not need to change the input signals to produce the output signals. The delay D is zero.

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')
Xa = 1×3

0     0     1

Ya = 1×5

0     0     1     2     3

D = 2

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.

Y = [0 0 0 0 1 2 3];

[Xa,Ya,D] = alignsignals(X,Y,[],'truncate')
Warning: All original data in the first input X has been truncated because the length of X is smaller than the estimated delay D: to avoid truncating this data do not use the 'truncate' option.
Xa = 1×3

0     0     0

Ya = 1×7

0     0     0     0     1     2     3

D = 4

Align signal Y with respect to X, despite the fact that Y is a periodic repetition of X. Return the smallest possible delay.

Create two signals, X and Y. Y consists of two copies of the nonzero portion of X separated by zeros. Align the two signals.

X = [0 1 2 3];
Y = [1 2 3 0 0 0 0 1 2 3 0 0];

[Xa,Ya,D] = alignsignals(X,Y)
Xa = 1×4

0     1     2     3

Ya = 1×13

0     1     2     3     0     0     0     0     1     2     3     0     0

D = -1

Input Arguments

collapse all

First input signal, specified as a numeric vector of length LX.

Example: [1 2 3]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

Second input signal, specified as a numeric vector of length LY.

Example: [0 0 1 2 3]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

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.

Example: 2

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

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 is also a row vector. If input argument X is a column vector, then Xa is also 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.

Algorithms

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

• If your signals have features such as pulses or transitions, you can align them more effectively using measurement functions instead of correlation. For an example, see Align Two Bilevel Waveforms.

 Orfanidis, Sophocles J. Optimum Signal Processing. An Introduction. 2nd Ed. Englewood Cliffs, NJ: Prentice-Hall, 1996.