%%Doug Van Nort   MUMT 611 Final Project
%
%We train our Trumpet Model Here, using Baum-Welch
%
%dhmm_em is from the HMM toolbox of Kevin Murphy


O = 73;  % we first quantize to 16 possible obervations
Q = 100; % we use 10 states: 1 for each analysis frame


% initial guess of parameters - random matrices
prior1_T = [1 zeros(1,Q-1)];      %we use a left-to-right model, and so this is our initial state distribution
transmat1_T = mk_stochastic(rand(Q,Q));
obsmat1_T = mk_stochastic(rand(Q,O));

%2,4,5,13,17,22,27,30,32 - left out for testing later

trdata_T = [OTL(1,:);OTL(3,:);OTL(6:12,:);OTL(14:16,:);OTL(18:19,:);OTL(21,:);OTL(23:26,:);OTL(28:29,:);OTL(31,:);OTL(33:34,:);...
            OTR(1,:);OTR(3,:);OTR(6:12,:);OTR(14:16,:);OTR(18:19,:);OTR(21,:);OTR(23:26,:);OTR(28:29,:);OTL(31,:);OTR(33:34,:)];

% improve guess of parameters(i.e. training) using EM (Baum-Welch)

[LL_T, prior_T, transmat_T, obsmat_T] = dhmm_em(trdata_T, prior1_T, transmat1_T, obsmat1_T, 'max_iter', 5);



obsmat_T(:,28) = .00001;        %enforce a threshold so that no zero probabilities occurr (which they do at 					%O=28,29,55,65)
obsmat_T(:,29) = .00001; 
obsmat_T(:,55) = .00001;
obsmat_T(:,65) = .00001;