%-----------------------------------------------------------------
% Speech Recognition - Numbers 1 to 5
% LPC Cepstral coefficients codebook generator
% 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 speech.mat
M=80;
N=240;
p=10;
for ii=1:5
   cvector(ii,1:(4800-240)/80+1,1:fix(3*p/2))=zeros((4800-240)/80+1,fix(3*p/2));
   for jj=1:30
      windnumb=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
         %plot(rl)
         % 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
         %subplot(2,1,1);
         %stem(-alpha)
         %subplot(2,1,2);
         %stem(aaa);
         %rl(1)
         %pause
         % 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);
         %q=30;
         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 k=1:length(c)
         cvector(ii,windnumb,k)=cvector(ii,windnumb,k)+c(k);
         end
         windnumb=windnumb+1;
      end
end