DynamicTimeWarping - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


SignalProcessing

  

DynamicTimeWarping

  

matches two signals

 

Calling Sequence

Parameters

Options

Description

Examples

References

Compatibility

Calling Sequence

DynamicTimeWarping( signal1, signal2, options )

Parameters

signal1, signal2

-

1-D rtables or lists of data of type complexcons

Options

• 

compiled: Either true or false, specifies if auxiliary procedures are to be compiled if they are currently uncompiled. The default is false.

• 

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

Description

• 

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+1Gi0,1 and Hi+1Hi0,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:

1. 

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=dXi&comma;Yj+μ, where dx&comma;y is the given metric, and μ=0 when i=1 and j=1, μ=Ci1,1 when 1<i and j=1, μ=C1,j1 when i=1 and 1<j, and μ=minCi1,j1&comma;Ci1,j&comma;Ci,j1 when 1<i and 1<j. The value of Cm,n is the optimal cost of the best match between X and Y.

2. 

Construct the time-warping vectors G and H, which store the indices for the optimal path. Starting at the end, namely m&comma;n, for each index i&comma;j, take p&comma;q to be the index pair which gives the smallest value in Ci1,j1&comma;Ci,j1&comma;Ci1,j, and append p to G and q to H. Continue this process recursively until index pair 1&comma;1 is reached.

3. 

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 mn and the value of radius. For a given index i, Ci,j is computed only for indices j that satisfy ijρ. The mn component of ρ is needed to ensure that the index pair m&comma;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:

dA&comma;B=i=1p0ai=0andbi=0aibiai+biotherwise

• 

For metric=chebyshev:

dA&comma;B=maxaibi&comma;i=1..p

• 

For metric=entropy, which requires that A and B both have real and positive elements:

dA&comma;B=i=1paibiTypesetting:-_Hold%log2aiTypesetting:-_Hold%log2bi

• 

For metric=euclidean:

dA&comma;B=i=1paibi2

• 

For metric=taxicab:

dA&comma;B=i=1paibi

• 

When DynamicTimeWarping is called in the session for the first time with compiled=true, the internal auxiliary routines which otherwise would be called with evalhf will instead be compiled. If DynamicTimeWarping has already been called in the session with compiled=true, it will continue to use the compiled versions of the internal commands, even if called with compiled=false. Use of the compiled=true option is recommended when the sizes of the signals are large, or the DynamicTimeWarping command is to be called numerous times. In these scenarios, the speed increase in execution is likely worth the cost of the overhead for compilation.

• 

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.

Examples

withSignalProcessing&colon;

Example 1

A4&comma;6&comma;8

A4&comma;6&comma;8

(1)

B4&comma;6&comma;8

B4&comma;6&comma;8

(2)

U&comma;VDynamicTimeWarpingA&comma;B

U,V4.6.8.,4.6.8.

(3)

DynamicTimeWarpingA&comma;B&comma;&apos;output&apos;&equals;&apos;cost&apos;

0.

(4)

Example 2

A10&comma;20&comma;20&comma;30&comma;40&comma;40&comma;40&comma;40

A10&comma;20&comma;20&comma;30&comma;40&comma;40&comma;40&comma;40

(5)

B10&comma;20&comma;30&comma;30&comma;30&comma;40

B10&comma;20&comma;30&comma;30&comma;30&comma;40

(6)

U&comma;VDynamicTimeWarpingA&comma;B

U,V10.20.20.30.30.30.40.40.40.40.,10.20.20.30.30.30.40.40.40.40.

(7)

DynamicTimeWarpingA&comma;B&comma;&apos;output&apos;&equals;&apos;cost&apos;

0.

(8)

Example 3

• 

Consider two signals from the same source, with the second signal having twice the sample rate as the first:

fsin2Pit&plus;5

fsin2πt+5

(9)

a0&colon;

b1&colon;

m101&colon;

n201&colon;

XGenerateSignalf&comma;t&equals;a..b&comma;m&colon;

YGenerateSignalf&comma;t&equals;a..b&comma;n&colon;

• 

We expect that to match the two signals, X will have to double-up its samples:

RDynamicTimeWarpingX&comma;Y&comma;&apos;method&apos;&equals;&apos;standard&apos;&comma;&apos;output&apos;&equals;&apos;record&apos;&colon;

R&apos;unwarpedplot&apos;

R&apos;warpedplot&apos;

R&apos;cost&apos;

1.96858148935275601

(10)

R&apos;rmse&apos;

0.0155106442271454488

(11)

Example 4

X0.0&comma;0.219524329376085&comma;1.03001175166964&comma;1.39983625176281&comma;1.56282473947696&comma;1.71391133806283&comma;2.16122789596062&comma;1.82680548525239&comma;1.69258876504047&comma;1.39113328513140&comma;1.11342717751392&comma;0.784123157751215&comma;0.707558810044168&comma;0.637971404121882&comma;0.471821268913782&comma;0.439648316356553&comma;0.371086959057091&comma;0.356440067184568&comma;0.331495924881517&comma;0.278188317684323&comma;0.206959858555780&comma;0.198804342406105&comma;0.196327439371469&comma;0.178098039554947&comma;0.157204681962224&comma;0.156915834747560&comma;0.155238209012307&comma;0.150762580284409&comma;0.146161658642928&comma;0.124443983283263&colon;

Y0.0&comma;0.206570824948915&comma;0.637986423299256&comma;1.10834870066160&comma;1.52137526568988&comma;1.83543428085871&comma;2.04072174684286&comma;2.14466682238484&comma;2.16284894433726&comma;2.11355568930851&comma;2.01470348690095&comma;1.88225721255719&comma;1.72957163140713&comma;1.56727507692996&comma;1.40345035833876&comma;1.24395870646777&comma;1.09281305072597&comma;0.952546557611176&comma;0.824547806896206&comma;0.709349871139647&comma;0.606870106284449&comma;0.516602858479741&comma;0.437770042558240&comma;0.369435657404901&comma;0.310590439714759&comma;0.260212454378748&comma;0.217308752794220&comma;0.180942469474214&comma;0.150248971963549&comma;0.124443983283263&colon;

DynamicTimeWarpingX&comma;Y&comma;&apos;output&apos;&equals;&apos;unwarpedplot&apos;

DynamicTimeWarpingX&comma;Y&comma;&apos;output&apos;&equals;&apos;warpedplot&apos;

DynamicTimeWarpingX&comma;Y&comma;&apos;output&apos;&equals;&apos;cost&apos;

1.88596173996218686

(12)

DynamicTimeWarpingX&comma;Y&comma;&apos;output&apos;&equals;&apos;rmse&apos;

0.0888199609500530812

(13)

Example 5

• 

The DynamicTimeWarping command can also match noisy signals (and a noisy signal to its pure source):

gsin2Pit

gsin2πt

(14)

a0&colon;

b1&colon;

m128&colon;

XGenerateSignalg&comma;t&equals;a..b&comma;m&comma;&apos;noisetype&apos;&equals;&apos;additive&apos;&comma;&apos;noisedeviation&apos;&equals;0.25&colon;

n256&colon;

YGenerateSignalg&comma;t&equals;a..b&comma;n&comma;&apos;noisetype&apos;&equals;&apos;multiplicative&apos;&comma;&apos;noisedeviation&apos;&equals;0.50&colon;

RDynamicTimeWarpingX&comma;Y&comma;&apos;output&apos;&equals;&apos;record&apos;&colon;

R&apos;unwarpedplot&apos;

R&apos;warpedplot&apos;

R&apos;cost&apos;

50.9728672222889756

(15)

R&apos;rmse&apos;

0.277092093482195323

(16)

Example 6

• 

Here, we will match a pair of larger signals. First, though, we will compile the internal procedures separately using a token call of DynamicTimeWarping to measure the time it takes:

CodeTools:-UsageDynamicTimeWarping1&comma;2&comma;3&comma;4&comma;&apos;compiled&apos;

memory used=33.53MiB, alloc change=0 bytes, cpu time=1.27s, real time=1.46s, gc time=0ns

2.3.,2.3.

(17)

XGenerateSignal5&plus;1t14&comma;t&equals;0..2&comma;200&comma;&apos;mirror&apos;&equals;&apos;antisymmetric&apos;&comma;&apos;copies&apos;&equals;4&colon;

numelemsX

1593

(18)

YGenerateSignal6t1&comma;t&equals;0..2&comma;200&comma;&apos;mirror&apos;&equals;&apos;antisymmetric&apos;&comma;&apos;copies&apos;&equals;4&colon;

numelemsY

1593

(19)

RCodeTools:-UsageDynamicTimeWarpingX&comma;Y&comma;&apos;method&apos;&equals;&apos;pruning&apos;&comma;&apos;radius&apos;&equals;100&comma;&apos;output&apos;&equals;&apos;record&apos;&colon;

memory used=24.08MiB, alloc change=19.38MiB, cpu time=100.00ms, real time=100.00ms, gc time=0ns

R&apos;unwarpedplot&apos;

R&apos;warpedplot&apos;

R&apos;rmse&apos;

0.0220493272388582497

(20)

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&ExponentialE;10PiIt

f&ExponentialE;10Iπt

(21)

g&ExponentialE;10PiIt2

g&ExponentialE;10Iπt2

(22)

a0&colon;

b1&colon;

m27&colon;

n29&colon;

XGenerateSignalf&comma;t&equals;a..b&comma;m&colon;

YGenerateSignalg&comma;t&equals;a..b&comma;n&colon;

• 

Now, match the signals:

RDynamicTimeWarpingX&comma;Y&comma;&apos;method&apos;&equals;&apos;standard&apos;&comma;&apos;metric&apos;&equals;&apos;euclidean&apos;&comma;&apos;output&apos;&equals;&apos;record&apos;&colon;

R&apos;unwarpedplot&apos;

R&apos;warpedplot&apos;

• 

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:

R&apos;warpingpathplot&apos;

R&apos;rmse&apos;

0.0704699789240906399

(23)
• 

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:

R&apos;cost&apos;

1.59455359895352333

(24)

NormDifferenceR&apos;warpedsignal1&apos;&comma;R&apos;warpedsignal2&apos;&comma;2

1.59455359895352244

(25)

References

  

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.

Compatibility

• 

The SignalProcessing[DynamicTimeWarping] command was introduced in Maple 2022.

• 

For more information on Maple 2022 changes, see Updates in Maple 2022.

See Also

SignalProcessing

SignalProcessing[CrossCorrelation]