File Exchange

## Dynamic Time Warping (DTW)

version 1.4.0.0 (3.18 KB) by
This package implements Dynamic Time Warping (DTW).

Updated 13 Nov 2014

View Version History

DTW is implemented in both Matlab and C/MEX. The C/MEX function is very fast. The algorithm we use is the standard one from wiki: http://en.wikipedia.org/wiki/Dynamic_time_warping
There is a demo in the package. To use, just call:
d=dtw(s,t,w) or d=dtw_c(s,t,w)

### Cite As

Quan Wang (2020). Dynamic Time Warping (DTW) (https://www.mathworks.com/matlabcentral/fileexchange/43156-dynamic-time-warping-dtw), MATLAB Central File Exchange. Retrieved .

m qasim

The function does not work for vectors of different length

Eduardo Santos

Something is wrong...

should be 12

t=0:.001:3;
f=10;
s1=0.5*(square(2*pi*f*t)+1)';
s2=0.5*(square(2*pi*f*t+pi/4)+1)';
dtw_wiki = dtw(s2',s1',10)
plot(s1,'DisplayName','s1');hold on;plot(s2,'DisplayName','s2');grid;hold off;

Rui Li

Hi Quan, thanks for your contribution. I have a request which I am not sure it is proper. Do you mind adding the warping path to the output? It could be appreciated a lot if you could. But if it takes lots of effort, never mind. Thanks for your contribution.

Rui Li

Works well. Thank you very much. Especially the C/MEX one is very fast.

Mesho AlTham

Mesho AlTham

Peng

Yongjun Wang

clear;
clc;
x = -10:0.1:10;
y = sin(x);
len = length(y);
y2 = interp1(1:len,y,linspace(1,len,2*len));
dtimew(y2',y')
plot(1:len,y,'-r.',1:2*len,y2,'-b.')

Zheng

Hi Quan,

I'm very new to dynamic time warping. Does "D" indicate the similarity between two vectors?

Thanks,

Zheng

Beil Zheng

kalarmago

What is a common value for the w parameter?

yabing hu

good job

Yijun Yan

Hi Quan,

Just wonder, which is the corresponding paper for this code?

Yuanliang Meng

Hi Quan,
It is nice. It would be useful if you can add an option to force the alighment of start/end points of two signals. Or force the alignment of selected points (so it becomes interpolation in a sense).

Chanmann Lim

Hi Quan, This is a great piece of work and I have made slight changes to normalize the DTW distance by its warping path for both matlab and c versions for my project. Please let me know if I can contribute that here.

Thanks!

Grace Pan

Hi,Quan Wang
Could i ask you a question? The code works perfectly when two input data have the same length. But if two input data have different length, such as [10 12 14 18 14 12 10 10 12 14 18 14 12 10] and [NaN 12 14 18 14 12 10 10 12 14 18 NaN NaN NaN], the output is NaN. Could you think how to deal with it?

Sahan Priyanga

nguyen anh

Hi Quan Wang.
I have a question:
What the paper does algorithm DTW using adapt window size belong to ?

Thank you.

Haoyi Xu

Tipu Rahaman

I am getting error as
'Maximum variable size allowed by the program is exceeded.'

for below line.

D=zeros(ns+1,nt+1)+Inf; % cache matrix
D(1,1)=0;

Quan Wang

Hi Yongqiang,

The window parameter w in function dtw(s,t,w) is the parameter that prevents big gradients.

Yongqiang

Hi Quan,

Could I ask you a question? I just want to confirm with you, so that I correctly understand what is written in your file. You didn't implement the slope constraint (the one that prevents too much or too little gradient, namely the fifth condition in Sakoe & Chiba 1978), did you?

Sakoe & Chiba 1978: Dynamic programming algorithm optimization for spoken word recognition.

Chang hsiung

haha

Quan Wang

Hi Daniel,

You need to learn what is a MATLAB function first.

Daniel

What does MATLAB Compiler do? When I try to run the code, I get an error message for Line 1 (function d=dtw(a,b,w)).

Thanks

Quan Wang

Hi Steven du,

Yes, you are right. I forgot to update it when I extended it for multi-dimensional signals.

Steven du

I thinks line 19 of dtw.m
ns=length(s);
Ought to be ns=size(s,1)

As length(s)=max(size(s))

ryphodoras

great job!

Blaise

works perfectly!

Yan Ou

Siqi

Quan Wang

Hi Leonel Rozo, it now supports multidimensional signals.

Leonel Rozo

Does it consider multidimensional signals?

##### MATLAB Release Compatibility
Created with R2012b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux