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

• 

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=1paibilog2ailog2bi

• 

For metric=euclidean:

dA&comma;B=i=1paibi2

• 

For metric=taxicab:

dA&comma;B=i=1paibi

• 

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,VDynamicTimeWarpingA&comma;B

U,V?,?

(3)

DynamicTimeWarpingA&comma;B&comma;output=cost

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,VDynamicTimeWarpingA&comma;B

U,V?,?

(7)

DynamicTimeWarpingA&comma;B&comma;output=cost

0.

(8)

Example 3

• 

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

fsin2πt+5

fsin2πt+5

(9)

a0&colon;

b1&colon;

m101&colon;

n201&colon;

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

YGenerateSignalf&comma;t=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;method=standard&comma;output=record&colon;

Runwarpedplot

Rwarpedplot

Rcost

1.96858148935266630

(10)

Rrmse

0.0155106442271451764

(11)

Example 4

X0.&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.&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;output=unwarpedplot

DynamicTimeWarpingX&comma;Y&comma;output=warpedplot

DynamicTimeWarpingX&comma;Y&comma;output=cost

1.88596173996218686

(12)

DynamicTimeWarpingX&comma;Y&comma;output=rmse

0.0888199609500530812

(13)

Example 5

• 

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

gsin2πt

gsin2πt

(14)

a0&colon;

b1&colon;

m128&colon;

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

n256&colon;

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

RDynamicTimeWarpingX&comma;Y&comma;output=record&colon;

Runwarpedplot

Rwarpedplot

Rcost

50.9728672222889685

(15)

Rrmse

0.277207925405362321

(16)

Example 6

• 

Here, we will match a pair of larger signals:

XGenerateSignal5+sqrt1t14&comma;t=0..2&comma;200&comma;mirror=antisymmetric&comma;copies=4&colon;

numelemsX

1593

(17)

YGenerateSignal6abst1&comma;t=0..2&comma;200&comma;mirror=antisymmetric&comma;copies=4&colon;

numelemsY

1593

(18)

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

memory used=24.18MiB, alloc change=19.36MiB, cpu time=79.00ms, real time=79.00ms, gc time=0ns

Runwarpedplot

Rwarpedplot

Rrmse

0.0220493405352973661

(19)

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:

fexp10πIt

f&ExponentialE;10Iπt

(20)

gexp10πIt2

g&ExponentialE;10Iπt2

(21)

a0&colon;

b1&colon;

m27&colon;

n29&colon;

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

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

• 

Now, match the signals:

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

Runwarpedplot

Rwarpedplot

• 

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

Rrmse

0.0704699789240905150

(22)
• 

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:

Rcost

1.59455359895351978

(23)

NormDifferenceRwarpedsignal1&comma;Rwarpedsignal2&comma;2

1.59455359895351978

(24)

X1&comma;2&comma;3

X1&comma;2&comma;3

(25)

DynamicTimeWarpingX&comma;X&comma;method=pruning&comma;output=costmatrix

(26)

DynamicTimeWarpingX&comma;X&comma;method=standard&comma;output=costmatrix

(27)

X1&comma;2I&comma;3

X1&comma;2I&comma;3

(28)

DynamicTimeWarpingX&comma;X&comma;method=pruning&comma;output=costmatrix

(29)

DynamicTimeWarpingX&comma;X&comma;method=standard&comma;output=costmatrix

(30)

XGenerateSignal5+sqrt1t14&comma;t=0..2&comma;10&comma;mirror=antisymmetric&comma;copies=4

YGenerateSignal6abst1&comma;t=0..2&comma;15&comma;mirror=antisymmetric&comma;copies=4

r4

r4

(31)

c1DynamicTimeWarpingX&comma;Y&comma;method=pruning&comma;radius=r&comma;output=cost

c119.2545035870638195

(32)

c2DynamicTimeWarpingY&comma;X&comma;method=pruning&comma;radius=r&comma;output=cost

c219.2545035870638195

(33)

c3DynamicTimeWarpingX&comma;Y&comma;method=standard&comma;radius=r&comma;output=cost

c319.2545035870638195

(34)

c3DynamicTimeWarpingY&comma;X&comma;method=standard&comma;radius=r&comma;output=cost

c319.2545035870638195

(35)

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

• 

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

SignalProcessing[CrossCorrelation]