SignalProcessing
DynamicTimeWarping
matches two signals
Calling Sequence
Parameters
Options
Description
Examples
References
Compatibility
DynamicTimeWarping( signal1, signal2, options )
signal1, signal2
-
1-D rtables or lists of data of type complexcons
method: Either standard or pruning, specifies the algorithm used. The default is pruning.
metric: One of canberra, chebyshev, entropy, euclidean, and taxicab, specifies the metric used when computing the optimal path. The default is taxicab.
radius: Either a non-negative integer or infinity, specifies the index radius used when computing the optimal path. The default is infinity.
unwarpedplotoptions: Additional plot options to be passed when creating the plot(s) of the original signals. The default is [].
unwarpedplotimaginaryoptions: Additional plot options to be passed when creating the plot of the imaginary parts of the original signals when they are complex. Note unwarpedplotoptions is applied first, followed by unwarpedplotimaginaryoptions. The default is [].
unwarpedplotrealoptions: Additional plot options to be passed when creating the plot of the real parts of the original signals when they are complex. Note unwarpedplotoptions is applied first, followed by unwarpedplotrealoptions. The default is [].
warpedplotoptions: Additional plot options to be passed when creating the plot(s) of the time-warped signals. The default is [].
warpedplotimaginaryoptions: Additional plot options to be passed when creating the plot of the imaginary parts of the time-warped signals when they are complex. Note warpedplotoptions is applied first, followed by warpedplotimaginaryoptions. The default is [].
warpedplotrealoptions: Additional plot options to be passed when creating the plot of the real parts of the time-warped signals when they are complex. Note warpedplotoptions is applied first, followed by warpedplotrealoptions. The default is [].
warpingpathplotoptions: Additional plot options to be passed when creating the plot of the time-warping path. The default is [].
warpingvectorsplotoptions: Additional plot options to be passed when creating the plot of the time-warping Vectors. The default is [].
output: The type of output. Let X and Y be, respectively, signal1 and signal2, and let m and n be their respective sizes. The supported options are:
costmatrix: Matrix, of datatype float[8], with element Ci,j representing the cost (with respect to metric) of matching Xi with Yj.
cost: The cost of matching Xm to Yn, that is, Cm,n, where C is the cost Matrix.
rmse: The root-mean-square (RMS) error between U (the time-warped version of X) and V (the time-warped version of Y).
signal1: Vector, of size m and datatype float[8] or complex[8], containing the first original signal X.
signal2: Vector, of size n and datatype float[8] or complex[8], containing the second original signal Y.
size: The size k of the time-warped signals.
unwarpedplot: Plot of the original signals.
warpedplot: Plot of the time-warped signals.
warpedsignal1: Vector, of size k and datatype float[8] or complex[8], containing the first time-warped signal U.
warpedsignal2: Vector, of size k and datatype float[8] or complex[8], containing the second time-warped signal V.
warpingpathplot: Plots of the path which time-warps X to U and Y to V and the time-warping Vectors G and H.
warpingvector1: Vector, of size k and datatype integer[4], containing the indices G of X (with repeats) which are used to create U.
warpingvector2: Vector, of size k and datatype integer[4], containing the indices H of Y (with repeats) which are used to create V.
record: Returns a record with the previous options.
list of any of the above options: Returns an expression sequence with the corresponding outputs, in the same order.
The default is [warpedsignal1,warpedsignal2].
The DynamicTimeWarping command takes two signals X and Y, of respective size m and n, and determines the time-warped signals U and V, both of some common size k, which are closest with respect to the given metric.
To match signals, the DynamicTimeWarping command inserts zero or more successive copies of every sample for each signal in such a way that the time-warped signals have the same length and the error between them (as measured by metric) is as small as possible. Note that if two signals are identical except for the number of copies of successive samples, then they can be matched exactly. More precisely, the command "warps" X into U and Y into V by computing Vectors of indices G and H, both of the same length k which satisfy G1=1, Gk=m, H1=1, Hk=n, and Gi+1−Gi∈0,1 and Hi+1−Hi∈0,1 for each i.
For the signals X and Y, both require two or more elements each, and all the elements must be defined and finite.
For the algorithm to work, the signals need to agree at their endpoints, that is, X1=Y1 and Xm=Yn. If the passed signal do not already match at their endpoints, the values there are averaged, that is, X1 and Y1 are replaced with X12+Y12, and Xm and Yn are replaced with Xm2+Yn2.
When method=standard, the regular algorithm for Dynamic Time Warping (DTW), based on techniques of Dynamic Programming, is used:
Construct the m-by-n cost Matrix C, with element Ci,j representing the cost of matching Xi with Yj. First, set each element to infinity. Then, recursively take Ci,j=d⁡Xi,Yj+μ, where d⁡x,y is the given metric, and μ=0 when i=1 and j=1, μ=Ci−1,1 when 1<i and j=1, μ=C1,j−1 when i=1 and 1<j, and μ=min⁡Ci−1,j−1,Ci−1,j,Ci,j−1 when 1<i and 1<j. The value of Cm,n is the optimal cost of the best match between X and Y.
Construct the time-warping vectors G and H, which store the indices for the optimal path. Starting at the end, namely m,n, for each index i,j, take p,q to be the index pair which gives the smallest value in Ci−1,j−1,Ci,j−1,Ci−1,j, and append p to G and q to H. Continue this process recursively until index pair 1,1 is reached.
Compute the time-warped signals, U=X[G] and V=Y[H].
When method=pruning, the Pruning Method is employed, which is a variation of the above method whereby index pairs are pruned (skipped) during construction of the cost Matrix when the values are too large for the pairs to be a part of the optimal path. This method is exact, in the sense that the optimal path found is the same as for the standard method, and tends to execute a fair bit faster than the standard method. Note that when this method is used, the cost Matrix will (likely) still have elements that are infinity, corresponding to index pairs that have been pruned.
Let ρ be the larger of m−n and the value of radius. For a given index i, Ci,j is computed only for indices j that satisfy i−j≤ρ. The m−n component of ρ is needed to ensure that the index pair m,n can be reached. The method is inexact, in the sense that the optimal path found is (likely) not the same as for the standard method, with a larger total cost for the optimal path. Note that when this method is used with a lower value of ρ, the cost Matrix will (likely) still have elements that are infinity, corresponding to index pairs that fall outside of the window.
The metric option is used by the optimizer to determine the optimal match for the two signals. Consider two Vectors A and B, both of size p. For the error between them, we have a few options. See below.
For metric=canberra:
d⁡A,B=∑i=1p⁡0ai=0andbi=0ai−biai+biotherwise
For metric=chebyshev:
d⁡A,B=max⁡ai−bi,i=1..p
For metric=entropy, which requires that A and B both have real and positive elements:
d⁡A,B=∑i=1p⁡ai−bi⁢log2⁡ai−log2⁡bi
For metric=euclidean:
d⁡A,B=∑i=1p⁡ai−bi2
For metric=taxicab:
d⁡A,B=∑i=1p⁡ai−bi
Maple attempts to coerce signal1 and signal2 to 1-D Vectors that are either both of float[8] datatype or both of complex[8] datatype, and an error is thrown if this is not possible. For this reason, it is most efficient for the passed input to use the appropriate datatypes.
The inputs signal1 and signal2 cannot have an indexing function, and must use rectangular storage.
The DynamicTimeWarping command is not thread safe.
with⁡SignalProcessing:
Example 1
A≔4,6,8
B≔4,6,8
U,V≔DynamicTimeWarping⁡A,B
U,V≔?,?
DynamicTimeWarping⁡A,B,output=cost
0.
Example 2
A≔10,20,20,30,40,40,40,40
B≔10,20,30,30,30,40
Example 3
Consider two signals from the same source, with the second signal having twice the sample rate as the first:
f≔sin⁡2⁢π⁢t+5
a≔0:
b≔1:
m≔101:
n≔201:
X≔GenerateSignal⁡f,t=a..b,m:
Y≔GenerateSignal⁡f,t=a..b,n:
We expect that to match the two signals, X will have to double-up its samples:
R≔DynamicTimeWarping⁡X,Y,method=standard,output=record:
Runwarpedplot
Rwarpedplot
Rcost
1.96858148935266630
Rrmse
0.0155106442271451764
Example 4
X≔0.,0.219524329376085,1.03001175166964,1.39983625176281,1.56282473947696,1.71391133806283,2.16122789596062,1.82680548525239,1.69258876504047,1.39113328513140,1.11342717751392,0.784123157751215,0.707558810044168,0.637971404121882,0.471821268913782,0.439648316356553,0.371086959057091,0.356440067184568,0.331495924881517,0.278188317684323,0.206959858555780,0.198804342406105,0.196327439371469,0.178098039554947,0.157204681962224,0.156915834747560,0.155238209012307,0.150762580284409,0.146161658642928,0.124443983283263:
Y≔0.,0.206570824948915,0.637986423299256,1.10834870066160,1.52137526568988,1.83543428085871,2.04072174684286,2.14466682238484,2.16284894433726,2.11355568930851,2.01470348690095,1.88225721255719,1.72957163140713,1.56727507692996,1.40345035833876,1.24395870646777,1.09281305072597,0.952546557611176,0.824547806896206,0.709349871139647,0.606870106284449,0.516602858479741,0.437770042558240,0.369435657404901,0.310590439714759,0.260212454378748,0.217308752794220,0.180942469474214,0.150248971963549,0.124443983283263:
DynamicTimeWarping⁡X,Y,output=unwarpedplot
DynamicTimeWarping⁡X,Y,output=warpedplot
DynamicTimeWarping⁡X,Y,output=cost
1.88596173996218686
DynamicTimeWarping⁡X,Y,output=rmse
0.0888199609500530812
Example 5
The DynamicTimeWarping command can also match noisy signals (and a noisy signal to its pure source):
g≔sin⁡2⁢π⁢t
m≔128:
X≔GenerateSignal⁡g,t=a..b,m,noisetype=additive,noisedeviation=0.25:
n≔256:
Y≔GenerateSignal⁡g,t=a..b,n,noisetype=multiplicative,noisedeviation=0.50:
R≔DynamicTimeWarping⁡X,Y,output=record:
50.9728672222889685
0.277207925405362321
Example 6
Here, we will match a pair of larger signals:
X≔GenerateSignal⁡5+sqrt⁡1−t−14,t=0..2,200,mirror=antisymmetric,copies=4:
numelems⁡X
1593
Y≔GenerateSignal⁡6−abs⁡t−1,t=0..2,200,mirror=antisymmetric,copies=4:
numelems⁡Y
R≔CodeTools:-Usage⁡DynamicTimeWarping⁡X,Y,method=pruning,radius=100,output=record:
memory used=24.18MiB, alloc change=19.36MiB, cpu time=79.00ms, real time=79.00ms, gc time=0ns
0.0220493405352973661
Example 7
Consider two complex signals, with the first having a constant sample rate and the second having a sample rate that increases with time:
f≔exp⁡10⁢π⁢I⁢t
f≔ⅇ10⁢I⁢π⁢t
g≔exp⁡10⁢π⁢I⁢t2
g≔ⅇ10⁢I⁢π⁢t2
m≔27:
n≔29:
Y≔GenerateSignal⁡g,t=a..b,n:
Now, match the signals:
R≔DynamicTimeWarping⁡X,Y,method=standard,metric=euclidean,output=record:
As we can see from the plot of the time-warping path, the matching is achieved by slowing down X, with more slowing at the beginning:
Rwarpingpathplot
0.0704699789240905150
We used the Euclidean metric to obtain the matched signals, and the cumulative cost determined during the computation equals the Euclidean distance between the two final signals:
1.59455359895351978
NormDifference⁡Rwarpedsignal1,Rwarpedsignal2,2
X≔1,2,3
DynamicTimeWarping⁡X,X,method=pruning,output=costmatrix
DynamicTimeWarping⁡X,X,method=standard,output=costmatrix
X≔1,2−I,3
X≔GenerateSignal⁡5+sqrt⁡1−t−14,t=0..2,10,mirror=antisymmetric,copies=4
Y≔GenerateSignal⁡6−abs⁡t−1,t=0..2,15,mirror=antisymmetric,copies=4
r≔4
c1≔DynamicTimeWarping⁡X,Y,method=pruning,radius=r,output=cost
c1≔19.2545035870638195
c2≔DynamicTimeWarping⁡Y,X,method=pruning,radius=r,output=cost
c2≔19.2545035870638195
c3≔DynamicTimeWarping⁡X,Y,method=standard,radius=r,output=cost
c3≔19.2545035870638195
c3≔DynamicTimeWarping⁡Y,X,method=standard,radius=r,output=cost
Silva, Diego F., and Gustavo E.A.P.A. Batista. "Speeding up all-pairwise dynamic time warping matrix calculation". Proceedings of the 2016 SIAM International Conference on Data Mining, pp. 837-845. Society for Industrial and Applied Mathematics, 2016.
Starting with Maple 2023, external C code is used for the auxiliary procedures that were formerly implemented in Maple and could be compiled by passing the compiled option. The compiled option is now deprecated, but it is still accepted for backwards compatibility.
The SignalProcessing[DynamicTimeWarping] command was introduced in Maple 2022.
For more information on Maple 2022 changes, see Updates in Maple 2022.
The SignalProcessing[DynamicTimeWarping] command was updated in Maple 2023.
See Also
SignalProcessing[CrossCorrelation]
Download Help Document