天天看點

基于matlab的dtw算法,下載下傳的用MATLAB實作的DTW算法,不會用,跪求大神幫忙

該樓層疑似違規已被系統折疊 隐藏此樓檢視此樓

這是下載下傳的用MATLAB實作的DTW算法,問題是怎麼用這個算法簡單測試一下,例如兩個序列(3,5,6,7,7,1)(3,6,6,7,8,1,1)用這個算法計算這兩個序列的相似性?哪個大神幫幫忙,本人菜鳥一枚

function [Dist,D,k,w,rw,tw]=dtw(r,t)

%

% [Dist,D,k,w,rw,tw]=dtw(r,t,pflag)

%

% Dynamic Time Warping Algorithm 動态時間規整算法

% Dist is unnormalized distance between t and r Dist是t和r之間的非标準化距離

% D is the accumulated distance matrix D是累積的距離矩陣

% k is the normalizing factor k是正态因子

% w is the optimal path w是最佳路徑

% t is the vector you are testing against t是你要測試的矢量

% r is the vector you are testing r是你正在測試的矢量

% rw is the warped r vector rw是扭曲的r矢量

% tw is the warped t vector tw是扭曲的t矢量

% pflag plot flag: 1 (yes), 0(no) pflag圖示記:1(yes),0(no)

%

% Version comments: 文本的評論:

% rw, tw and pflag added by Pau Mic

[row,M]=size(r); if (row > M) M=row; r=r'; end;

[row,N]=size(t); if (row > N) N=row; t=t'; end;

d=sqrt((repmat(r',1,N)-repmat(t,M,1)).^2); %this makes clear the above instruction Thanks Pau Mic

D=zeros(size(d));

D(1,1)=d(1,1);

for m=2:M

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

end

for n=2:N

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

end

for m=2:M

for n=2:N

D(m,n)=d(m,n)+min(D(m-1,n),min(D(m-1,n-1),D(m,n-1))); % this double MIn construction improves in 10-fold the Speed-up. Thanks Sven Mensing

end

end

Dist=D(M,N);

n=N;

m=M;

k=1;

w=[M N];

while ((n+m)~=2)

if (n-1)==0

m=m-1;

elseif (m-1)==0

n=n-1;

else

[values,number]=min([D(m-1,n),D(m,n-1),D(m-1,n-1)]);

switch number

case 1

m=m-1;

case 2

n=n-1;

case 3

m=m-1;

n=n-1;

end

end

k=k+1;

w=[m n; w]; % this replace the above sentence. Thanks Pau Mic

end

% warped waves

rw=r(w(:,1));

tw=t(w(:,2));

end