📄 path1.m
字号:
%PATH1 : Path I's Frame match by Dynamic time warping.
%FUNCTION: [m,adis]=path1(cofa1,cofa2,speech2,basic,show);
%
%Input cofa1 = linear prediction coefficient matrix for speech1.
% cofa2 = linear prediction coefficient matrix for speech2.
% speech2 = source speech
% basic = [measure constraint path order fram_len over_len lens], the
% basic parameters.
% show = display the searching process if show == 1
%
%Output m = Dynamic time warping function i-->w, w=m(i)
% adis = final accumulated distance
%
%Please see page 211 of "fundamentals of speech recogniction" by Rabiner
% and Juang for this local constraints and the resulting path.
%
function [m,adis]=path1(cofa1,cofa2,speech2,basic,show);
% variables used in the function:
% i = index for cofa1.
% j = index for cofa2.
% M = number of frames for cofa2.
% N = number of frames for cofa1.
% path = possible path, [accumulated distances, i_moving, j_jump....]
% p_path = path matrix of previous frame
% t_path = path of the current point
%%%%%%%%% PLEASE DO NOT EDIT THE FOLLOWING %%%%%%%%%%%
speech2=speech2(:)'; % convert to row vector
[N dummy]=size(cofa1);
[M dummy]=size(cofa2);
%retrieve the basic parameters
measure=basic(1);
path_const=basic(3);
order=basic(4);
fram_len=basic(5);
over_len=basic(6);
lens=basic(7);
%%%%%%%%%%%%%%% FINDING the OPTIMAL PATH by Dynamic Programming
%%%%%%% construct the distortion measure matrix
for i=1:N
for j=1:M
a=cofa1(i,:);
b=cofa2(j,:);
test=speech2( (j-1)*lens+order+1:j*lens+order );
zi=speech2( (j-1)*lens+1: (j-1)*lens+order );
DIS(i,j)=distance(a,b,test,zi,measure);
end
end
NM=2*N+2*M+1;
path=ones(M, NM);
path=-1*path;
%%%%%%% Initial condition, first frame
i=1;
j=1;
path(j,1)=DIS(i,j);
for j=2:M
t_path=path(j-1,:);
t_path=t_path(t_path>-1);
t_path=[t_path 0 1];
t_path(1)=DIS(i,j)+path(j-1,1);
path(j,1:length(t_path))=t_path;
end
%%%%%%% runing from 2nd frame to the end
for i=2:N
p_path=path; % the path matrix of previous frame
j=1;
% there is only one path to reach this (i,1) point
t_path=p_path(j,:);
t_path=t_path( t_path>-1 );
t_path=[t_path 1 0];
t_path(1)=t_path(1)+DIS(i,j);
path(j,1:length(t_path))=t_path;
for j=2:M
% there are three path to this current (i,j) point
dis(1)=p_path(j,1)+0.5*DIS(i,j);
dis(2)=p_path(j-1,1);
dis(3)=path(j-1,1)+0.5*DIS(i,j);
[dummy,indx]=sort(dis);
if indx(1)==1
t_path=p_path(j,:);
t_path=t_path(t_path>-1);
t_path=[t_path 1 0];
elseif indx(1)==2
t_path=p_path(j-1,:);
t_path=t_path(t_path>-1);
t_path=[t_path 1 1];
elseif indx(1)==3
t_path=path(j-1,:);
t_path=t_path(t_path>-1);
t_path=[t_path 0 1];
end
t_path(1)=dummy(1)+DIS(i,j);
nl=length(t_path);
path(j, 1:nl)=t_path;
fdis(j)=path(j,1);
% delete the remaining path
if (nl+1)<NM & path(j,nl+1)~=-1
path(j,nl+1:NM)=-1*ones(1,NM-nl);
end
end
clear p_path;
if show==1
idx=find( fdis>0 );
[dum,idxidx]=min( fdis(idx) );
tmpath=path(idx(idxidx),:);
tmpath=tmpath(tmpath>-1);
x=[1 tmpath(2:2:length(tmpath))];
y=[1 tmpath(3:2:length(tmpath))];
plot(integ(x),integ(y),'+');
axis([1 N 1 M]);
title('Finding the optimal path!');
drawnow;
fdis=zeros(1,M);
end
end
t_path=path(M,:); % final path!
t_path=t_path(t_path >-1);
x=[1 t_path(2:2:length(t_path))];
y=[1 t_path(3:2:length(t_path))];
adis=t_path(1,1);
x=integ(x);
y=integ(y);
% check the final x-axis since (0,1) path is legal
if length(x)>N
len=length(x);
i=1;
while len>N
i=i+1;
if ( x(i)-x(i-1) )==0
x(i-1)=[];
y(i-1)=[];
i=i-1;
len=len-1;
end
end
end
m=[x;y];
if show==1
plot(x,y,'k+')
xlabel('target')
ylabel('source')
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -