-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdtw.m
85 lines (75 loc) · 1.7 KB
/
dtw.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function [Dist,D,k,w]=dtw(t,r)
%% Dynamic Time Warping Algorithm
% Evaluates the dtw distance between two samples vector
% Uses either one dimensional or two dimensional euclidean distance,
% depending on the dimension of the input vectors.
% INPUT ARGS:
% t ---> vector you are testing against
% r ---> vector you are testing
% OUTPUT ARGS:
% Dist ---> unnormalized distance between t and r
% D ---> accumulated distance matrix
% k ---> normalizing factor
% w ---> optimal path
[rows,N]=size(t);
[rows,M]=size(r);
%% Cost matrix evaluation
d=zeros(N,M); %preallocated
if rows==2
% two-dimensional case, uses the two-dimensional euclidean distance
for n=1:N
for m=1:M
d(n,m)=euclidean(t(:,n),r(:,m));
end
end
else if rows==1
% one-dimensional case, uses one-dimensional euclidean distance
for n=1:N
for m=1:M
d(n,m)=abs(t(n)-r(m));
end
end
end
end
D=zeros(size(d)); %accumulate matrix, preallocated
%% accumulate matrix evaluation
D(1,1)=d(1,1);
for n=2:N
D(n,1)=d(n,1)+D(n-1,1);
end
for m=2:M
D(1,m)=d(1,m)+D(1,m-1);
end
for n=2:N
for m=2:M
D(n,m)=d(n,m)+min([D(n-1,m),D(n-1,m-1),D(n,m-1)]);
end
end
%% Distance between sample vectors
Dist=D(N,M);
%% Normalizing factor and path evaluation
n=N;
m=M;
k=1;
w=[];
w(1,:)=[N,M];
while ((n+m)~=2)
if (n-1)==0
m=m-1;
elseif (m-1)==0
n=n-1;
else
[~,number]=min([D(n-1,m),D(n,m-1),D(n-1,m-1)]);
switch number
case 1
n=n-1;
case 2
m=m-1;
case 3
n=n-1;
m=m-1;
end
end
k=k+1;
w=cat(1,w,[n,m]);
end