alignsignals
Align two signals by delaying earliest signal
Description
[
estimates the delay xa,ya,D]
= alignsignals(x,y)D between two input signals,
x and y, and returns
D along with the aligned signals xa
and ya. The function estimates the delay using
cross-correlation.
If
yis delayed with respect tox, thenDis positive andxis delayed byDsamples.If
yis advanced with respect tox, thenDis negative andyis delayed by –Dsamples.
The input signals need not be exact delayed copies of each other. However, the signals can be accurately aligned only if there is sufficient correlation between them. For more information on estimating covariance and correlation, see [1].
Examples
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
Create two signals, X and Y. Y is exactly the same as X, except Y is delayed by two samples. Align the two signals.
X = [1 2 3]; Y = [0 0 1 2 3]; [Xa,Ya,D] = alignsignals(X,Y)
Xa = 1×5
0 0 1 2 3
Ya = 1×5
0 0 1 2 3
D = 2
Align the signals using a maximum window size of 1.
maxlag = 1;
[Xa,Ya,D] = alignsignals(X,Y,Method="xcorr",MaxLag=maxlag)Xa = 1×4
0 1 2 3
Ya = 1×5
0 0 1 2 3
D = 1
Generate two signals that represent bilevel waveforms. The signals are sampled at 50 Hz for 20 seconds. For the first signal, the transition occurs 13 seconds after the start of the measurement. For the second signal, the transition occurs 5 seconds after the start of the measurement. The signals have different amplitudes and are embedded in white Gaussian noise of different variances. Plot the signals.
t = linspace(0,20,1001)'; e1 = 1.4*tanh(t-13)+randn(size(t))/3; e2 = tanh(3*(t-5))+randn(size(t))/5; plot(t,e1,t,e2) xlabel("Seconds") ylabel("Amplitude")

Align the signals so their transition times coincide. Correlation-based methods cannot align this type of signal adequately.
[y1,y2] = alignsignals(e1,e2); plot(y1) xlabel("Samples") ylabel("Amplitude") hold on plot(y2) hold off

Align the signals using the risetime method.
[y1,y2] = alignsignals(e1,e2,Method="risetime"); plot(y1) xlabel("Samples") ylabel("Amplitude") hold on plot(y2) hold off

Create two signals, X and Y. Y is exactly the same as X, except Y has two leading zeros. Align the two signals and set the truncate option to true. This option preserves the length of X.
X = [1 2 3]; Y = [0 0 1 2 3]; [Xa,Ya,D] = alignsignals(X,Y,Truncate=true)
Xa = 1×3
0 0 1
Ya = 1×5
0 0 1 2 3
D = 2
When the truncate option truncates all the original data of X, the function issues a warning.
Y = [0 0 0 0 1 2 3]; [Xa,Ya,D] = alignsignals(X,Y,Truncate=true)
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
Input Arguments
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
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: alignsignals(X,Y,Method="npeak",PeakNum=4) aligns
X and Y based on the location of the
fourth peak in each signal.
Method to estimate the delay between signals, specified as
"xcorr", "maxpeak",
"npeak", or "risetime". You
can specify additional name-value arguments based on the specified
Method.
"xcorr"— Estimate the delay using cross-correlation. If you specifyMethodas"xcorr", you can also specifyMaxLag."maxpeak"— Estimate the delay using the location of the highest peak in each signal. If you specifyMethodas"maxpeak", you can optionally specifyMinPeakProminenceandMinPeakHeight."npeak"— Estimate the delay using the location of the nth peak in each signal. If you specifyMethodas"npeak", you can optionally specifyPeakNum,MinPeakProminence, andMinPeakHeight."risetime"— Estimate the delay using the location of the rising edges in each signal. The function uses the specified values forStateLevelsto find the rising edges.
Example: Method="npeak",PeakNum=3
Maximum window size used to estimate the delay between signals,
specified as an integer scalar. By default, MaxLag
is equal to max(Lx,Ly)–1, where Lx is
the length of x and
Ly is the length of
y. If MaxLag is negative,
the function uses the absolute value.
This argument applies only when Method is set to
"xcorr".
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Peak number used to align the signals, specified as a positive
integer. This argument applies only when you specify
Method as "npeak".
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Minimum peak prominence used to identify peaks in the signals,
specified as a real nonnegative scalar. When you specify
MinPeakProminence, the function finds those
peaks that have a relative importance of at least the value specified.
For more information, see findpeaks and Prominence.
This argument applies only when you specify
Method as "npeak" or
"maxpeak".
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Minimum peak height used to identify peaks in the signals, specified
as a real scalar. When you specify MinPeakHeight,
the function finds those peaks with height greater than the specified
value. For more information, see findpeaks.
This argument applies only when you specify
Method as "npeak" or
"maxpeak".
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
State levels, specified as a real-valued two-element row vector. The
first element corresponds to the lower state levels of the input
signals, and the second element corresponds to the upper state levels of
the input signals. If you do not specify
StateLevels, the function estimates the state
levels of the input waveform by a histogram method. For more
information, see risetime.
This argument applies only when you specify
Method as "risetime".
Example: Method="risetime",StateLevels=[0.5
2]
Data Types: double
Option to truncate the input signals, specified as a numeric or
logical 1 (true) or
0 (false). When you specify
Truncate as true, the
lengths of xa and ya are equal
to the lengths of x and y, respectively.
If the estimated delay
Dis positive, the function prependsDzeros toxand truncates the lastDsamples ofx.If
Dis negative, the function prepends –Dzeros toyand truncates the lastDsamples ofy.If
D≥ Lx, thenxaconsists of Lx zeros and all samples ofxare lost. If –Dis ≥ Ly, thenyaconsists of Ly zeros and all samples ofyare lost.
Output Arguments
Aligned first signal, returned as a numeric vector that is aligned with
the second output argument ya.
If input argument
xis a row vector, thenxais also a row vector.If input argument
xis a column vector, thenxais also a column vector.
If you specify Truncate as
true 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
yis a row vector, thenyais also a row vector.If input argument
yis a column vector, thenyais also a column vector.
If you specify Truncate as
true 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 an integer. This
integer represents the number of samples by which the two input signals
x and y are offset.
If
yis delayed with respect tox, thenDis positive andxis delayed byDsamples.If
yis advanced with respect tox, thenDis negative andyis delayed by –Dsamples.If
xandyare already aligned, thenDis zero and neitherxnoryare delayed.
If you specify a value for input argument MaxLag,
then D is less than or equal to
MaxLag.
References
[1] Orfanidis, Sophocles J. Optimum Signal Processing. An Introduction. 2nd Ed. Englewood Cliffs, NJ: Prentice-Hall, 1996.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version History
Introduced in R2007aThese syntaxes will be removed in a future release.
| Syntax | Result | Use Instead |
|---|---|---|
alignsignals(x,y,maxlag) | Runs | alignsignals(x,y,Method="xcorr",MaxLag=maxlag) |
alignsignals(x,y,maxlag,'truncate') | Runs | alignsignals(x,y,Method="xcorr",MaxLag=maxlag,Truncate=true) |
Previously, alignsignals required Communications Toolbox™.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)