Dynamic Time Warping in C¶

The simplest Dynamic Time Warping C implementation with Python bindings.

The behavior is equivalent to the dtaidistance package but with much simpler (basic) API and X2 faster.

Installation¶

pip install -e git+https://github.com/dizcza/cdtw-python.git#egg=cdtw

Dynamic Time Warping Python API¶

 dtw_dist(x, y) Computes the DTW distance between x and y aligned in time, using dynamic programming. dtw_mat(x, y) Computes the full cost (distance) matrix D between all elements of x and y according to dtw_path(cost_mat) Finds the best path (an array of x- and y-coordinates) to align x to y, given the cost matrix cost_mat = dtw_mat(x, y).
>>> from cdtw import dtw_mat, dtw_dist, dtw_path
>>> x = [1, 2, 3, 4, 5]
>>> y = [2, 3, 4]

# total distance
>>> dtw_dist(x, y)
1.4142135381698608

# full distance (cost) matrix
>>> cost = dtw_mat(x, y)
>>> cost
array([[1.       , 2.236068 , 3.7416575],
[1.       , 1.4142135, 2.4494898],
[1.4142135, 1.       , 1.4142135],
[2.4494898, 1.4142135, 1.       ],
[3.8729835, 2.4494898, 1.4142135]], dtype=float32)

# best warp path
>>> dtw_path(cost).tolist()
[[0, 0], [1, 0], [2, 1], [3, 2], [4, 2]]