clc;clear all;
close all;
%hmmforspeech.m
%TRAINLPC{1} is the LPC speech data collected from the word 1. Each column of %Formulating Hidden Markov Model

load TRAINLPC
s=[];
for i=1:1:3
s=[s size(TRAINLPC{i},1)];
end
mx=min(s);
DATA=[];
for i=1:1:3
DATA=[DATA TRAINLPC{i}(1:1:mx,:)];
end
DATA1=[];
for i=1:1:size(DATA,2)
temp=DATA(:,i);
DATA1=[DATA1 reshape(temp,13,14)];
end
[E,D]=eigs(cov(DATA1'),6);
DATA1=E'*DATA1;
%Initializing the mean and the co-variance matrix for 5 states.
S=5;
ITER=50;
[M,COV,INDEX,COL]=kmeansforspeech(DATA1,S,ITER);
%Initializing the transition probabilities (p) for 5 states
S=length(COV);
for i=1:1:S-1
r=rand;
p(i,:)=[zeros(1,i-1) r 1-r zeros(1,S-1-i)];
end
p=[p;zeros(1,size(p,2)-1) 1];
S=length(M);
%HMM for the word 1
%F(m,n) is the probability of generating the word 'WORD1' upto 'm' frame
%with mth frame is generated from the nth state.
F=zeros(14,S);
R=zeros(13,S);
R=[R; ones(1,S)];
for word=1:1:3
DATA=DATA1(:,(word-1)*70+1:1:(word-1)*70+70);
for iteration=1:1:100
iteration
for w=1:1:5
WORD1=DATA(:,(w-1)*14+1:1:(w-1)*14+14);
F(1,1)=abs(gaussvalue(WORD1(:,1),M{1},COV{1}));
for i=2:1:13
F(i,1)=logsum(F(i-1,1),p(1,1)*gaussvalue(WORD1(:,i),M{1},COV{1}));
for j=2:1:S
F(i,j)=logsum(logsum(F(i-1,j-1),p(j-1,j)*gaussvalue(WORD1(:,i),M{j},...
COV{j})),F(i-1,j)+p(j,j)*gaussvalue(WORD1(:,3),M{j},COV{j}));
end
end
F(14,S)=logsum(F(13,S-1),p(S-1,S)*gaussvalue(WORD1(:,14),M{S},COV{S}));
FF{w}=F;
%R(m,n) is the probability of generating the word WORD1 from (m+1)th frame
%to the final frame with the condition mth frame is generated from the nth
%state.
R(13,S)=abs((p(S,S)*gaussvalue(WORD1(:,14),M{S},COV{S})));
for i=12:-1:1
R(i,S)=logsum(log(p(S,S)*gaussvalue(WORD1(:,i+1),M{S},COV{S})),R(i+1,S));
for j=S-1:-1:1
R(i,j)=logsum(logsum(p(j,j)*gaussvalue(WORD1(:,i+1),M{j},COV{j}),...
R(i+1,j)),logsum(p(j,j+1)*...
gaussvalue(WORD1(:,i+1),M{j+1},COV{j+1}),R(i+1,j+1)));
end
end
RR{w}=R;
for i=1:1:13
for j=1:1:S
num=logsum(F(i,j),R(i,j));
den=0;
for k=1:1:S
den=logsum(den,logsum(F(i,k),R(i,k)));
end
A(i,j)=exp(num-den);
end
end
for i=1:1:size(A,1)
A(i,:)= A(i,:)/sum(A(i,:));
end
AA{w}=A;
for i=1:1:13
for j=1:1:S-1
for k=j:1:j+1
num=logsum(logsum(F(i,j),p(j,k)*gaussvalue(WORD1(:,i+1),...
M{j+1},COV{j+1})),F(i+1,k));
den=0;
for l=1:1:S-1
for m=l:1:l+1
den=logsum(den, logsum(logsum(F(i,l),p(l,m)...
    *gaussvalue(WORD1(:,i+1),M{l+1},COV{l+1})),F(i+1,m)));
end
end
B(i,j,k)=exp(num-den);
end
end
for i=1:1:size(B,1)
s=0;
for j=1:1:size(B,2)
for k=1:1:size(B,3)
s=s+B(i,j,k);
end
end
B(i,:,:)=B(i,:,:)/s;
end
BB{w}=B;
end
end
for i=1:1:S-1
for j=i:1:i+1
num=0;
den=0;
for k=1:1:5
for l=1:1:13
num=logsum(num,BB{k}(l,i,j));
den=logsum(den,AA{k}(l,i));
end
end
p(i,j)=exp(num-den);
end
end
for i=1:1:size(p,1)
p(i,:)= p(i,:)/sum(p(i,:));
end
for k=1:1:S
DEN=0;
for t=1:1:13
for x=1:1:5
DEN=DEN+AA{x}(t,k);
end
end
for x=1:1:5
for t=1:1:13
m(x,k,t)=AA{x}(t,k)/(DEN+eps);
end
end
end
for k=1:1:S
MEAN{k}=0;
COV{k}=0;
for x=1:1:5
WORD=DATA(:,(x-1)*14+1:1:(x-1)*14+14);
for t=1:1:13
MEAN{k}=MEAN{k}+m(x,k,t)*WORD(:,t);
end
end
for x=1:1:5
WORD=DATA(:,(x-1)*14+1:1:(x-1)*14+14);
for t=1:1:13
COV{k}=COV{k}+m(x,k,t)*(WORD(:,t)-MEAN{k})...
*(WORD(:,t)-MEAN{k})';
end
end
end
end
HMMMODEL{word}{1}=p;
HMMMODEL{word}{2}=COV;
HMMMODEL{word}{3}=M;
end
save HMMMODEL mx E            

