assistive-rehab
|
Class for temporal alignment based on Dynamic Time Warping (DTW).
Class for temporal alignment based on Dynamic Time Warping (DTW).
The class Dtw can be used for aligning two temporal sequences using the Dynamic Time Warping (DTW). DTW calculates the optimal path which minimizes the distance between the two signals to be aligned. Given two signals and , with and samples respectively, DTW constructs the distance matrix , where , and . A warping path has to satisfy the following conditions:
The DTW distance is the total distance of the optimal warping path.
DTW can be also applied to multidimensional temporal sequences, by applying the same procedure independently to the corresponding components of the two signals. The DTW distance is the sum of the DTW distances of each component.
Additional constraints can be used to restrict the space of search of the warping path. The library includes the adjustment window condition, which enforces the search of the warping path inside a window around the distance matrix diagonal.
Given two vectors v1,v2, the following piece of code can be used to align them and get the DTW distance:
For the multidimensional case, given two signals with samples over time and components, v1 and v2 have to be defined as vector of vectors. The following piece of code can be used to align them and get the DTW distance: