https://github.com/dizcza/cdtw-python
The simplest Dynamic Time Warping in C with Python bindings
https://github.com/dizcza/cdtw-python
dtw dynamic-time-warping
Last synced: 3 months ago
JSON representation
The simplest Dynamic Time Warping in C with Python bindings
- Host: GitHub
- URL: https://github.com/dizcza/cdtw-python
- Owner: dizcza
- License: mit
- Created: 2021-11-08T18:35:08.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-05-18T20:20:51.000Z (about 1 year ago)
- Last Synced: 2026-03-04T18:53:46.089Z (4 months ago)
- Topics: dtw, dynamic-time-warping
- Language: Python
- Homepage: https://cdtw-python.readthedocs.io
- Size: 154 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://app.circleci.com/pipelines/github/dizcza/cdtw-python)

[](https://cdtw-python.readthedocs.io/en/latest/?badge=latest)
# Dynamic Time Warping in C with Python bindings
The simplest (and perhaps the fastest) Dynamic Time Warping C implementation with Python bindings.
The behavior is equivalent to the [dtaidistance](https://github.com/wannesm/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
```
## Documentation
https://cdtw-python.readthedocs.io/en/latest/
## Quick start
```python
>>> 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]]
```
See [Tutorial.ipynb](./docs/Tutorial.ipynb) for further explanation.
## Benchmarking
```python
>>> import dtaidistance
>>> import numpy as np
>>> import cdtw
>>> np.random.seed(0)
>>> x = np.random.randn(5_000)
>>> y = np.random.randn(10_000)
>>> %timeit cdtw.dtw_dist(x, y)
157 ms ± 218 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit dtaidistance.dtw.distance_fast(x, y)
296 ms ± 2.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> %timeit cdtw.dtw_mat(x, y)
404 ms ± 37 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> %timeit dtaidistance.dtw.warping_paths_fast(x, y)
991 ms ± 47.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
```
The *cdtw* package converts everything to the `float` data type (if needed) while *dtaidistance* requires `double`. The *cdtw* compiled with the `double` precision is X1.5 faster than *dtaidistance*.