clc;
clear all;
close all;
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=100;
F=14;
%MODEL THE FIRST WORD
for word=1:1:3 
DATA=DATA1(:,(word-1)*70+1:1:(word-1)*70+70);
[M,COV,INDEX,COL,P]=kmeansforspeech(DATA,S,ITER);
S=length(M);
for gmmiteration=1:1:100
   for w=1:1:5    
WORD=DATA(:,(w-1)*F+1:1:(w-1)*F+F);
for i=1:1:F
DEN=0;    
for j=1:1:S
DEN=DEN+P(j)*gaussvalue(WORD(:,i),M{j},COV{j});
end
for k=1:1:S
A{w}(i,k)=P(k)*gaussvalue(WORD(:,i),M{k},COV{k})/(DEN+eps);
end
end
end

for k=1:1:S
DEN=0;
for t=1:1:F
for x=1:1:5    
DEN=DEN+A{x}(t,k);        
end
end
for x=1:1:5
    for t=1:1:F
m(x,k,t)=A{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)*F+1:1:(x-1)*F+F);
    for t=1:1:F        
    MEAN{k}=MEAN{k}+m(x,k,t)*WORD(:,t);
    end
end
for x=1:1:5
    WORD=DATA(:,(x-1)*F+1:1:(x-1)*F+F);
    for t=1:1:F        
    COV{k}=COV{k}+m(x,k,t)*(WORD(:,t)-MEAN{k})*(WORD(:,t)-MEAN{k})';
    end
end
end
DEN=0;
for k=1:1:S
    for x=1:1:5
        for t=1:1:F
DEN=DEN+A{x}(t,k);            
        end
    end
end
for k=1:1:S
    NUM=0;
for x=1:1:5
    for t=1:1:F
        NUM=NUM+A{x}(t,k);
    end
end
P(k)=(NUM)/(DEN+eps);
end
end
GMMMODEL{word}{1}=P;
GMMMODEL{word}{2}=COV;
GMMMODEL{word}{3}=M;
end
save GMMMODEL mx E
            
