%This is an example program that creates a GMM from training data.
%The data consists of 20 2-D random number generated from a Gaussian source
%with zero mean and unit variance and the GMM has 2 centers with diagonal
%covariance matrices.

%First, generate the data

disp('We now generate the data')
disp('Press any key to continuous')
pause;
clc

%choose number of data 
N=20;

data=zeros(N,2);

for i=1:N
 
    data(i,1)=randn;
    data(i,2)=randn;
end


%plot the data on a 2-D plan
plot(data(:,1),data(:,2),'go')
title('The generated data')



%Now, we create a GMM namely model

model=gmm(2,2,'diag');


%Set the error value
options = foptions;
options(1)  = 1;		% Prints out error values.
options(14) = 100;		% Number of maximum iterations.

disp('Then we create and initialize a GMM ')
disp('Press any key to continuous')
pause
clc

%After creating the model, we initialize it by using the training data

model=gmminit(model,data,options);

%The model have now been created succesfully
figure,plot(data(:,1),data(:,2),'go',model.centres(:,1),model.centres(:,2),'xk','MarkerSize',20);
legend('Data','Centres',-1);
title('The GMM model');


disp('Finally, we train the model using the EM algorithm')
disp('It can be seen that the centres have been changed')
disp('Press any key to end')
pause
clc

%After initialize the model, we can now apply the EM algorithm to optimize
%its parameters.

model=gmmem(model,data,options);
figure,plot(data(:,1),data(:,2),'go',model.centres(:,1),model.centres(:,2),'xk','MarkerSize',20);
legend('Data','Centres',-1);
title('The GMM model after EM algorithm');
%The model have been trained with EM algorithm successfully and all the
%training process was done.
