Skip to content
Snippets Groups Projects
Commit 79c96b03 authored by marwan's avatar marwan
Browse files

*** empty log message ***

parent 91025790
No related branches found
No related tags found
No related merge requests found
function y=dtw(a,b)
% DTW Calculates the dynamic time warping distance.
% D=DTW(A,B) calculates the dynamic time warping (DTW)
% distance between row vectors A and B. If A and B are
% arrays, DTW distances are calculated for all rows in
% these array, thus D is a column vector of length(A).
% The size(A,1) has to match size(B,1).
%
% Reference:
% Myers, C. S, Rabiner, L. R.:
% A comparative study of several dynamic time-warping algorithms
% for connected word recognition, The Bell System Technical
% Journal, 60(7), 1982, 1389-1409.
% FastDTW: Toward Accurate Dynamic Time Warping in Linear Time
% and Space, Stan Salvador and Philip Chan. Intelligent
% Data Analysis, 2007.
% Copyright (c) 2008 by AMRON
% Norbert Marwan, Potsdam University, Germany
% http://www.agnld.uni-potsdam.de
%
% $Date$
% $Revision$
if size(a,1) ~= size(b,1)
error('Array''s dimension 1 does not match.')
end
Na = size(a,2); Nb = size(b,2);
y = zeros(size(a,1),1);
%h = waitbar(0, 'Calculation DTW distance');
for k = 1:size(a,1), %if k/100 == fix(k/100), waitbar(k/size(a,1)), end
aa = a(k,:)'; bb = b(k,:)';
eucDis = (repmat(aa,1,Nb) - repmat(bb',Na,1)).^2;
D = zeros(size(eucDis));
D(1,1) = eucDis(1,1);
D(2:Na,1) = eucDis(2:Na,1) + D((2:Na)-1,1);
D(1,2:Nb) = eucDis(1,2:Nb) + D(1,(2:Nb)-1);
for n = 2:Na
for m = 2:Nb
D(n,m) = eucDis(n,m) + min([D(n-1,m), D(n-1,m-1), D(n,m-1)]);
end
end
y(k) = D(Na,Nb);
end
%delete(h)
function y=levenshtein(a,b)
% LEVENSHTEIN Calculates the Levenshtein distance.
% D=LEVENSHTEIN(A,B) calculates the Levenshtein distance
% between row vectors A and B. If A and B are arrays,
% Levenshtein distances are calculated for all rows in
% these array, thus D is a column vector of length(A).
% The size(A,1) has to match size(B,1).
%
% Reference:
% Levenshtein, V. I.:
% Binary codes capable of correcting deletions, insertions,
% and reversals, Doklady Akademii Nauk SSSR, 163(4), 1965,
% 845-848, 1965 (Russian).
% English in: Soviet Physics Doklady, 10(8), 1966, 707-710.
% Copyright (c) 2008 by AMRON
% Norbert Marwan, Potsdam University, Germany
% http://www.agnld.uni-potsdam.de
%
% $Date$
% $Revision$
if size(a,1) ~= size(b,1)
error('Array''s dimension 1 does not match.')
end
Na = size(a,2); Nb = size(b,2);
y = zeros(size(a,1),1);
for k = 1:size(a,1)
D = zeros([Na+1, Nb+1]);
D(1,:) = 0:Nb; D(:,1) = 0:Na;
for i = 2:Na + 1
for j = 2:Nb + 1
cost = (b(k, j-1) ~= a(k, i-1));
D(i,j)=min([D(i-1,j-1) + cost, D(i-1,j)+1, D(i,j-1)+1]);
end
end
y(k) = D(end,end);
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment