# alignsignals

Align two signals by delaying earliest signal

## Syntax

• ```[Xa,Ya] = alignsignals(X,Y)``` example
• ```[Xa,Ya] = alignsignals(X,Y,maxlag)``` example
• ```[Xa,Ya] = alignsignals(X,Y,maxlag,'truncate')``` example
• ```[Xa,Ya,D] = alignsignals(___)``` example

## 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` and `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

### Aligning two signals where the second signal lags by two samples

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)```

### Aligning two signals where the first signal lags by three samples

Align signal `Y` with respect to `X` by advancing 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)```

### Aligning two signals where the second signal is noisy

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)```

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

### Aligning two signals where the second signal is a periodic repetition of the first signal

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` is exactly the same as `X` with some noise added to it. 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)```

### Aligning two signals using the ‘truncate' option

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')```

## Input Arguments

collapse all

### `X` — First input signalvector of numeric values

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

### `Y` — Second input signalvector of numeric values

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

### `maxlag` — Maximum window size or lagscalar integer | `[]`

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

### `Xa` — Aligned first signalvector of numeric values

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.

### `Ya` — Aligned second signalvector of numeric values

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.

### `D` — Estimated delay between input signalsscalar integer

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

collapse all

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

## References

[1] Orfanidis, S.J., Optimum Signal Processing. An Introduction. 2nd Edition, Prentice-Hall, Englewood Cliffs, NJ, 1996.