Skip to content

How to temporally align two signals

Some applications require signals to be temporally aligned. For example, when analyzing and comparing the current and the template skeleton for producing a feedback, joints must be aligned.

Given two 1D signals, whose temporal samples are stored in vectors v1 and v2, you can use the following code snippet to get the aligned versions w1 and w2:

assistive_rehab::Dtw dtw(-1);
std::vector<double> w_v1,w_v2;
dtw.align(v1,v2,w_v1,w_v2);
double d = dtw.getDistance();

Tip

In the example, no adjustment window condition is applied. Therefore the search of the warping path is done along the whole distance matrix. To limit the search, you can create the obejct dtw by passing the desired window, e.g. assistive_rehab::Dtw dtw(8).

The following images show two signals before and after the application of DTW:

Before DTW

before-dtw

After DTW

after-dtw

For the multidimensional case, the code can be adapted as following:

assistive_rehab::Dtw dtw(-1);
std::vector<std::vector<double>> w_v1,w_v2;
dtw.align(v1,v2,w_v1,w_v2);
double d = dtw.getDistance();

Note

v1 and v2 are defined as std::vector<std::vector>.