Code covered by the BSD License  

Highlights from
Isolated Digits Record

image thumbnail

Isolated Digits Record

by

 

14 Feb 2014 (Updated )

Implements a speaker trained, template-based, isolated digit recognizer

[distance,paths,distances]=dtw_backtrack_dist(cref,nref,ctest,ntest,L)
function [distance,paths,distances]=dtw_backtrack_dist(cref,nref,ctest,ntest,L)
%   distance=dtw distance accumulated along the best path
%   paths=best path through grid
%   distances=accumulated distance on the best path
%   cref=feature set for reference utterance
%   nref=number of frames of reference
%   ctest=feature set for test utterance
%   ntest=number of frames of test
%   L=number of feature vector components
%
%   assuming simple Euclidean distance (as most appropriate for cepstral
%   coefficient as the feature set
%
%   initialize accumulated distances to large number
% first eliminate all attempts to match mis-matched reference and test
% where nref/ntest > 1.95 or ntest/nref>1.95

    if (nref/ntest > 1.95 | ntest/nref > 1.95)
        distance=1;
        paths(1:ntest)=-4;
        distances(1:ntest)=1;
    else
    dacc(1:ntest,1:nref+2)=1.e5;
    dlocal(1:ntest,1:nref+2)=1.e5;
    path(1:ntest,1:nref+2)=10;
    dacc(1,3)=sqrt(sum((cref(1,:)-ctest(1,:)).^2));
    dacc(2,4)=sqrt(sum((cref(2,:)-ctest(2,:)).^2))+dacc(1,3);
    dacc(2,5)=sqrt(sum((cref(3,:)-ctest(2,:)).^2))+dacc(1,3);
    dlocal(1,3)=dacc(1,3);
    dlocal(2,4)=dacc(2,4);
    dlocal(2,5)=dacc(2,5);
    path(1,3)=0;
    path(2,4)=1;
    path(2,5)=2;
    
% solve from test index 2 to ntest
% first specify range of reference index according to global constraints
    for indtest=3:ntest
        indrefl=2+max(1+floor((indtest-1)/2),nref+2*(indtest-ntest));
        indrefh=2+min(1+2*(indtest-1),nref+ceil((indtest-ntest)/2));
        if (indrefh >= indrefl)            
            for indref=indrefl:indrefh
                dloc=sqrt(sum((cref(indref-2,:)-ctest(indtest,:)).^2));
                dlocal(indtest,indref)=dloc;
                dlocp=sqrt(sum((cref(indref-2,:)-ctest(indtest-1,:)).^2));
                dlocal(indtest-1,indref)=dlocp;
                dmin=min([dacc(indtest-1,indref-1),dacc(indtest-1,indref-2),...
                    dacc(indtest-2,indref-1)+dlocp,dacc(indtest-2,indref-2)+dlocp]);
                dacc(indtest,indref)=dloc+dmin;
% fprintf('indtest,indref: %d %d, dloc,dmin,dacc: %6.2f %6.2f %6.2f \n',indtest,indref-2,dloc,dmin,dacc(indtest,indref));
                if(dmin == dacc(indtest-1,indref-1))
                    path(indtest,indref)=1;
                elseif (dmin == dacc(indtest-1,indref-2))
					path(indtest,indref)=2;
                elseif (dmin == dacc(indtest-2,indref-1)+dlocp)
                    path(indtest,indref)=-1;
                elseif (dmin == dacc(indtest-2,indref-2)+dlocp)
                    path(indtest,indref)=-2;
                end
% fprintf('indtest,indref,dloc,dlocp,dmin,path: %d %d %6.2f %6.2f %6.2f %d \n',indtest,indref,...
%    dloc,dlocp,dmin,path(indtest,indref));
            end
        else
            fprintf('break since nref,ntest: %d %d \n',nref,ntest);
            break;
        end
    end
    distance=dacc(ntest,nref+2)/ntest;
% fprintf('nref,ntest,distance: %d %d %6.2f \n',nref,ntest,distance);
    paths(ntest)=nref;
    distances(ntest)=dacc(ntest,nref+2);
    indref=nref;
    indtest=ntest;
    while (indtest > 1)
% fprintf('indtest,indref,pathpoint: %d %d %d \n',indtest,indref,path(indtest,indref+2));
        if (path(indtest,indref+2) >= 1)
            indref=indref-path(indtest,indref+2);
            paths(indtest-1)=indref;
            distances(indtest-1)=dacc(indtest-1,indref+2);
            indtest=indtest-1;
        elseif (path(indtest,indref+2) == -1)
            paths(indtest-1)=indref;
            paths(indtest-2)=indref-1;
            distances(indtest-1)=dacc(indtest,indref+2)-dlocal(indtest-1,indref+2);
            distances(indtest-2)=dacc(indtest-2,indref+1);
            indtest=indtest-2;
            indref=indref-1;
        elseif (path(indtest,indref+2) == -2)
            paths(indtest-1)=indref;
            paths(indtest-2)=indref-2;
            distances(indtest-1)=dacc(indtest,indref+2)-dlocal(indtest-1,indref+2);
            distances(indtest-2)=dacc(indtest-2,indref);
            indtest=indtest-2;
            indref=indref-2;
        end
    end
    end
end

Contact us