% The program implements synthetic HMMs. 

function speech_mm3_model(modelnumber,filename)

% Generates a random observation sequence

N = 3; 	% Number of urns
M = 3; 	% Number of colored balls in each urn
T= 10; 	% Sequencelength of the observations

% Definition of the state transition probability matrix A for three models. The
% total probability for the rows and columns are equal to 1.
% Definition of the observation symbol probability distribution in the state B.
% PI indicates the initial state probability and the probabilities is 
% generated in the inteval between 0 to 1.

A1=[0.4 0.2 0.4; 0.2 0.3 0.5; 0.4 0.5 0.1]
B1=[0.4 0.1 0.5; 0.3 0.6 0.1; 0.3 0.3 0.4]
PI1 = [0.2 0.3 0.5]

A2=[0.1 0.1 0.8; 0.5 0.4 0.1; 0.4 0.5 0.1]
B2=[0.6 0.1 0.3; 0.3 0.6 0.1; 0.1 0.3 0.6]
PI2 = [0.3 0.1 0.6]

A3=[0.7 0.2 0.1; 0.2 0.5 0.3; 0.1 0.3 0.6]
B3=[0.4 0.5 0.1; 0.3 0.2 0.5; 0.3 0.3 0.4]
PI3= [0.1 0.7 0.2]

% Generation of the state transition probability matrix A.
%A = rand(N);
%for i = 1:N,
%  A(i,1:N) = A(i,1:N)./sum(A(i,1:N));
%end

% Generation of the observation symbol probability distribution in the state.
%B = rand([M,N]);
%for i = 1:N,
%  B(1:M,i) = B(1:M,i)./sum(B(1:M,i));
%end

% PI indicates the initial state probability and the probabilities is 
% generated in the inteval between 0 to 1.
%PI = rand(1,N);
%PI = PI1./sum(PI1)


% Selection of model, so different observation sequences can be created for 
% the same model.

if modelnumber == 1
   A = A1;
   B = B1;
   PI = PI1;
elseif modelnumber == 2
   A = A2;
   B = B2;
   PI = PI2;
elseif modelnumber == 3
   A = A3;
   B = B3;
   PI = PI3;
end

% Initialisation of the statevector and observationvector.
statevector = zeros(1,T);
O = zeros(1,T);

% A state (urn) is chosen and this is done by summing the PI probabilities
% until the a random_number i reased. The random number is generated between
% 0 and 1.
random_number = rand(1);

i = 1;
while sum(PI(1:i)) <= random_number,
  i=i+1;
end
statevector(1) = i;

% A ball is extracted within the state chosen. Finding the ball is done the
% same way as with the statevector. O() indicates the observation vector, 
% which correspond to the observation sequence that should be generated. It 
% should be noted that the total probability of the individual columns is 
% equal to 1.

random_number = rand(1);
i = 1;
while sum(B(1:i,statevector(1))) <= random_number,   
  i=i+1;
end
O(1) = i;

% The last entries into the statevector and observationvector is made. It's 
% done a little different because the first statevector entry is based on PI,
% while the rest is based on the state transition probabilities.

for t = 2:T,
  	% New state is chosen.
  	random_number = rand(1);
  	i = 1;
  	while sum(A(statevector(t-1),1:i)) <= random_number,   
   	i=i+1;
  	end
  	statevector(t) = i;
 
  	% New ball is extracted.
  	random_number = rand(1);
  	i = 1;
  	while sum(B(1:i,statevector(t))) <= random_number,   
   	i=i+1;
  	end
   O(t) = i;
end

% The variabels are saved for use in the viterbi algoritmen.

save (filename, 'A', 'B', 'PI', 'N', 'M', 'T', 'O', 'statevector', 'modelnumber') 
save model A1 B1 PI1 A2 B2 PI2 A3 B3 PI3
