-
Notifications
You must be signed in to change notification settings - Fork 1
/
dtw.m
38 lines (31 loc) · 991 Bytes
/
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
% Copyright (C) 2013 Quan Wang <[email protected]>,
% Signal Analysis and Machine Perception Laboratory,
% Department of Electrical, Computer, and Systems Engineering,
% Rensselaer Polytechnic Institute, Troy, NY 12180, USA
% dynamic time warping of two signals
function d=dtw(s,t,w)
% s: signal 1, size is ns*k, row for time, colume for channel
% t: signal 2, size is nt*k, row for time, colume for channel
% w: window parameter
% if s(i) is matched with t(j) then |i-j|<=w
% d: resulting distance
if nargin<3
w=Inf;
end
ns=size(s,1);
nt=size(t,1);
if size(s,2)~=size(t,2)
error('Error in dtw(): the dimensions of the two input signals do not match.');
end
w=max(w, abs(ns-nt)); % adapt window size
%% initialization
D=zeros(ns+1,nt+1)+Inf; % cache matrix
D(1,1)=0;
%% begin dynamic programming
for i=1:ns
for j=max(i-w,1):min(i+w,nt)
oost=norm(s(i,:)-t(j,:));
D(i+1,j+1)=oost+min( [D(i,j+1), D(i+1,j), D(i,j)] );
end
end
d=D(ns+1,nt+1);