%-----------------------------------------------------------------
% Speech Recognition - Numbers 1 to 5
% Classification
% References:
% [1] L. Rabiner and B.H. Juang, Fundamentals of Speech
%     Recognition, Prentice-Hall, 1993.
% [2] P.E. Papamichalis, Practical Approaches to Speech
%     Coding, Prentice-Hall, 1987.
% [3] J.D. Markel and A.H. Gray, Linear Prediction of
%     Speech
%-----------------------------------------------------------------
close all
clear all
word = ['one  ';'two  ';'three';'four ';'five '];
load cvector.mat
load speech.mat
M=80;
N=240;
p=10;
certo=0;
errado=0;
for ii=1:5
   for jj=1:1:30
      soma=zeros(1,5);
      wn=1;
      for kk=1:M:4800-N+1
         % windowing
         xl=reshape(newsp(ii,jj,kk:kk+N-1),1,N)';
         xl=xl.*hamming(N);
         % autocorrelation analysis
         for m=0:p
            rl(m+1)=0;
            for n=0:N-1-m
               rl(m+1)=rl(m+1)+xl(n+1)*xl(n+m+1);
            end
         end
         % LPC analysis
         if (rl(1)~=0)
            autocor=toeplitz(rl(1:p));
            vecr=rl(2:p+1);
            alpha=inv(autocor)*vecr';
         else
            alpha=zeros(1,p);
         end
         % LPC parameter conversion to cepstral coefficients
         c=0;
         for m=1:p
            c(m)=alpha(m);
            for k=1:m-1
               c(m)=c(m)+(k/m)*c(k)*alpha(m-k);
            end
         end
         q=fix(3*p/2);
         for m=p+1:q
            c(m)=0;
            for k=m-p:m-1
               c(m)=c(m)+(k/m)*c(k)*alpha(m-k);
            end
         end
         for l=1:5
            for k=1:length(c)
               soma(l)=soma(l)+(cvector(l,wn,k)-c(k))^2;
            end
         end
         wn=wn+1;
      end
      [m,k]=min(soma);
      if(k==ii)
         ok=ok+1;
         disp('input and output');
         disp(ii);
      else
         error=error+1;
         disp('input');
         disp(ii);
         disp('output');
         disp(k);
      end
   end
end