Code covered by the BSD License  

Highlights from
Dynamic Time Warping

4.09524

4.1 | 21 ratings Rate this file 151 Downloads (last 30 days) File Size: 1.47 KB File ID: #6516

Dynamic Time Warping

by Timothy Felty

 

08 Dec 2004 (Updated 12 Dec 2005)

Dynamic time warping program. Calculates the similarity between 2 vectors.

| Watch this File

File Information
Description

If you pass in 2 vectors it returns the unnormalized distance between the vectors, the accumulated distance between them, the length of the warping path (the normalizing factor), and the warping path points.

To compare 2 vectors A and B call:
[Dist,D,k,w]=dtw(A,B)
Dist is the unnormalized distance
D is the accumulated distance
k is the length of the warping path
(the normalizing factor)
w is a matrix containing the points along the warping path

Acknowledgements
This submission has inspired the following:
Constrained Dynamic Time Warping Distance Measure, Continuous Dynamic Time Warping
MATLAB release MATLAB 6.1 (R12.1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (28)
15 Dec 2004 zhlong l  
11 Feb 2005 mahajan bhushan  
29 Mar 2005 Carlos Franco  
06 Apr 2005 Georg Schmitz

The routine can be made 10 times faster by doing the d(n,m) calculation by

d=(repmat(t(:),1,M)-repmat(r(:)',N,1)).^2;

instead of the loop.

29 Apr 2005 Ivo Ihrke

The loop

for m=2:m
    D(1,m)=d(1,m)+D(1,m-1);
end

contains an error ( for statement ), should be:

for m=2:M
    D(1,m)=d(1,m)+D(1,m-1);
end

If you want to compare vectors, where the vectors themselves are unknown, but their distance matrix is known, you can use your distance matrix instead of "d"

04 May 2005 jamal habibi  
20 Feb 2006 Natan K

Can you tell me about speaker verification using mfcc and dtw. Please let me know.

08 Sep 2006 Rogelio Ramos  
16 Oct 2006 reza rezaww  
18 Dec 2006 Yuzeng Lv  
08 Jan 2007 Boat MiMi

...

24 Jan 2007 Sven Mensing

Good function
Another 10-fold Speed-up can be achived if you use a double-MIN construction for the Distance matrix

D(n,m)=d(n,m)+min(D(n-1,m),min(D(n-1,m-1),D(n,m-1)));

20 Feb 2007 weera kompreyaray  
07 Jun 2007 Pau Micó

I think it should be better to replace the sentence: w=cat(1,w,[n,m]); by: w=[n m; w];
In this way we get the w indexes in ascending order and it is easier to get t_warped and r_warped as: t_warped=t(w(:,1)); and r_warped=t(w(:,2));. In this way we can directly compare the warped sequences with the original ones without any index inversion.

28 Jan 2008 twinss ahmed  
26 Feb 2008 Sai Man

Great code thanks for writing. But, I noticed a small discrepancy with my C code. In the .m file line 45:
min([D(n-1,m),D(n,m-1),D(n-1,m-1)]);

it would be much better to have the D(n-1,m-1) element in the first argument like this:
min([D(n-1,m-1),D(n,m-1),D(n-1,m)]);
and also swap the cases on the following lines.

This is because we are trying to find the minimum path length, and in the case where D(n-1,m-1)=D(n,m-1) or D(n-1,m-1)=D(n-1,m) then it would make sense to preference the diagonal element over the offdiagonal.

25 Mar 2008 Ahmed Bderhman

I think this is a good working and had help me more.

I have this question: i like to change euclidean distance{d=(repmat(Test',1,N)-repmat(Ref,M,1)).^2;} with mahalanobis distance is it possipole? if yes how can i do that?

because i like to test the efficiency of my system when i use euclidean distance and when i used mahalnobis distance.

 

14 Oct 2008 Zhuo Feibao  
07 Apr 2009 Tony Mei

Can anyone explain how to use the normalizing factor? Thanks a lot.

13 Apr 2009 Wen

should not be, d(n,m)=sqrt(sum((t(:,n)-r(:,m)).^2)); instead of d(n,m)=(t(n)-r(m))^2;
t, r are sequences of rows-dim vectors

30 Mar 2010 Mano Samuel

Hi Timothy,
   could you let me know which article or book you used as a reference to code this ? . Thank you.
Mano

13 Aug 2010 Nandha

Can I know how can I implement lower bounding (LB_keogh) and global constraint to this algorithm ?

Also, how to use obtain normalized distance using the normalizing factor ?

17 Sep 2010 abdur

im trying to compare two vectors of audio samples but the matrix dimensions dont match n d code gives error. plz assist

14 Nov 2010 Lars

Maybe add code to ensure proper vectors, e.g. t=t(:)'; and r=r(:)'; in this case ;)

01 Aug 2011 Mohammed Khalilia

At least answer the previous questions!

10 Aug 2011 He  
24 Nov 2011 Vicky  
13 Jan 2012 Akhil Sudhakar

please send complete code for melodic contour matching....

Please login to add a comment or rating.
Updates
03 May 2005

Few minor updates

12 Dec 2005

Faster implementation thanks to review by Georg Schmitz.

Tag Activity for this File
Tag Applied By Date/Time
audio processing Timothy Felty 22 Oct 2008 07:37:46
video processing Timothy Felty 22 Oct 2008 07:37:46
dtw Timothy Felty 22 Oct 2008 07:37:46
dynamic time warping Timothy Felty 22 Oct 2008 07:37:46
signal processing Timothy Felty 22 Oct 2008 07:37:46
audio Timothy Felty 22 Oct 2008 07:37:47
dtw lalitha devi 08 Dec 2008 00:09:01
dynamic time warping lalitha devi 08 Dec 2008 00:09:08
dynamic time warping sarah 25 Apr 2009 14:32:37
dtw quentin tirroloni 13 Oct 2009 14:14:10
dynamic time warping Niamh 14 Jan 2010 11:40:33
dtw Atahan Tolunay 10 Jun 2010 04:55:08
dynamic time warping Rob R 16 Jun 2010 13:22:54
dynamic time warping Nandha 13 Aug 2010 21:46:30
dtw Colorado 20 Apr 2011 16:18:08
dynamic time warping Colorado 20 Apr 2011 16:18:11
dynamic time warping liang 05 Jun 2011 03:06:30
i need your help i want to align 3 sequences which will finally Vicky 24 Nov 2011 14:04:26
could you please help me Vicky 24 Nov 2011 14:04:39
very useful code Vicky 24 Nov 2011 14:05:26
warped transforms Alberto 16 Feb 2012 06:34:17
very useful code Atul Ingle 30 Mar 2012 13:16:05
dtw Saad 02 May 2012 04:44:55

Contact us at files@mathworks.com