Skip to content
Dynamic Time Warping in Python / C (using ctypes)
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

DTW - Dynamic Time Warping in Python / C (using ctypes)

The Dynamic Time Warping (DTW)[1,2] is a time-normalisation algorithm initially designed to eliminate timing differences between two speech patterns. This normalisation, or correction, is done by warping the time axis of one time series to match the other. The correction (time warping) makes it easier to compare two signals in a similar way to the method human beings[3].

DTW example Example of 2D trajectory-matching generated by the DTW method. Although looking perfect in the figure on the left, the cardioid was modified to have a constant value zone from time step $50$ to $150$. The DTW correctly matches the values as can be seen as a straight blue line in the Accumulated Distance plot (right).

Above is presented an example where a cardioid is compared to a circle. The cardioid also had a time delay inserted (values were kept constant). The DTW calculates the distance (here the Euclidean one) between all the points of the two time series and, then, generates another matrix with the accumulated distances. The total distance defined by the path formed with the minimum values of the accumulated distance (right-hand side of the figure) can be easily applied to compare different shapes.

This version of the algorithm uses a C kernel, supporting multidimensional arrays and Euclidean distance, to speed up the calculations with a Python wrapper as the user interface. More details and sample code can be found in this Jupyter notebook:

if you are not happy with my explanations above, one of the best explanations about how the DTW works I've found on a presentation by Elena Tsiporkova.

How to install:

  1. Clone the repository, or download it as a zip file and unzip it.
  2. Inside the directory dtw_python execute make.
  3. Now, to install the library and make it accessible to all users you need to execute sudo make install.
  4. After that you don't need the dtw_python directory anymore and you can test it using the jupyter notebook.

Related works:


  1. Sakoe, H., and S. Chiba. “Dynamic Programming Algorithm Optimization for Spoken Word Recognition.” IEEE Transactions on Acoustics, Speech and Signal Processing 26, no. 1 (February 1978): 43–49.
  2. Meinard Müller. “Dynamic Time Warping.” In Information Retrieval for Music and Motion, ch. 4, 69-82. New York: Springer-Verlag, 2007.
  3. Ratcliff, Roger. “Continuous versus Discrete Information Processing: Modeling Accumulation of Partial Information.” Master Thesis, Radboud University Nijmegen, 2004.

You can’t perform that action at this time.