%pes
clear;
O = load('txt/voice03/03.txt')';

states = 7;

mu=zeros(39,states);
vr=zeros(39,states);
cv=zeros(39,39,states);

mu(:,2) = [ -7.313049e+000 -9.398881e-001 -4.510057e-001 -7.288507e-001 -2.897759e-001 -7.751375e-002 -3.618123e-003 -1.390386e-001 2.084902e-001 -1.320959e-001 8.817019e-002 -1.228079e-001 6.917090e+001 -3.107722e-002 8.907292e-003 9.247836e-003 1.905289e-002 1.233459e-002 1.001064e-002 1.002233e-002 8.150973e-003 3.113299e-003 3.548607e-003 2.532211e-003 1.085190e-003 -1.025690e-001 7.951854e-003 -8.182072e-003 -6.170174e-003 -1.514464e-002 -6.019351e-003 -8.480256e-003 -3.986343e-003 -5.266401e-003 -1.843392e-003 -2.877901e-003 -8.476687e-004 -6.497114e-004 8.289810e-002]';
vr(:,2) = [3.163979e+000 2.168602e+000 1.371846e+000 1.517981e+000 9.038908e-001 8.897811e-001 6.909518e-001 5.652684e-001 5.599021e-001 5.125359e-001 4.591469e-001 4.670824e-001 3.629802e+001 1.388832e-001 9.953099e-002 7.722628e-002 7.820329e-002 6.347974e-002 6.216895e-002 5.599645e-002 5.214563e-002 4.893730e-002 4.565161e-002 4.265450e-002 4.052969e-002 8.744203e-001 2.787767e-002 2.031806e-002 1.623932e-002 1.644616e-002 1.327494e-002 1.286692e-002 1.156766e-002 1.087449e-002 1.014029e-002 9.546626e-003 8.929418e-003 8.537616e-003 1.812941e-001]';
cv(:,:,2) = diag(vr(:,2));
gconst(2) = -1.317014e+001;

mu(:,3) = [-5.706808e+000 -1.632700e+000 -1.323623e+000 -2.071136e+000 -1.106072e+000 -7.702732e-001 -7.924293e-002 -2.477285e-001 8.220086e-002 -1.945719e-001 3.895010e-002 -1.986868e-001 7.569669e+001 -4.303290e-001 -5.184830e-001 -1.041288e-001 -1.225248e-001 8.835100e-002 2.119334e-003 1.287675e-001 4.923478e-002 9.786386e-002 5.668704e-003 4.585921e-002 3.575344e-003 1.877275e+000 8.858949e-002 -2.266813e-001 -1.873447e-001 -1.790507e-001 -7.171319e-002 -1.260199e-001 -4.283990e-002 -5.091142e-002 -1.930598e-002 -4.159170e-002 -2.212860e-002 -2.337058e-002 1.856363e+000]';
vr(:,3) = [7.833735e+000 6.089544e+000 2.591224e+000 2.902048e+000 1.666114e+000 1.453201e+000 1.252079e+000 1.024298e+000 9.141239e-001 7.197049e-001 6.498387e-001 5.754127e-001 8.189402e+001 1.162364e+000 1.006405e+000 5.111086e-001 5.247118e-001 2.135822e-001 2.747779e-001 1.542309e-001 1.413999e-001 1.002515e-001 9.464195e-002 7.712689e-002 6.654263e-002 3.209077e+001 2.541938e-001 1.388697e-001 6.614034e-002 5.362068e-002 3.291022e-002 3.221403e-002 2.969450e-002 2.397764e-002 2.068081e-002 1.708270e-002 1.604358e-002 1.280637e-002 2.239141e+000]';
cv(:,:,3) = diag(vr(:,3));

mu(:,4) = [-5.652693e+000 -4.615658e+000 -2.951687e+000 -6.895529e+000 -2.690160e+000 -1.732640e+000 -4.101386e-001 -1.908145e+000 4.470546e-002 -1.065471e+000 -5.548403e-001 -4.460121e-001 1.102027e+002 2.847615e-001 1.058228e-001 5.352417e-002 3.017063e-002 -6.559902e-002 -2.231777e-002 -4.896455e-002 -2.233832e-002 -5.249316e-002 -6.145196e-004 -3.299612e-002 -1.492214e-002 -5.427094e-001 -6.388226e-002 1.414389e-001 5.564144e-002 1.609482e-001 6.562461e-002 -5.549205e-003 -2.051019e-003 5.329981e-002 -9.825671e-003 2.160343e-002 2.673190e-002 3.019699e-003 -1.050667e+000]';
vr(:,4) = [1.016467e+001 8.372550e+000 3.267163e+000 3.284851e+000 3.946333e+000 2.220042e+000 1.971026e+000 1.683012e+000 1.512034e+000 1.961291e+000 1.249286e+000 1.248523e+000 1.649046e+002 9.557528e-001 7.732920e-001 2.755146e-001 4.788254e-001 2.462779e-001 1.959681e-001 1.378614e-001 1.330008e-001 1.054550e-001 1.039458e-001 9.952246e-002 8.163809e-002 2.124301e+001 1.870248e-001 1.010609e-001 4.240693e-002 5.646939e-002 4.331052e-002 3.720109e-002 2.576043e-002 2.202496e-002 1.933648e-002 1.898772e-002 1.799011e-002 1.529474e-002 1.844348e+000]';
cv(:,:,4) = diag(vr(:,4));

mu(:,5) = [-1.087007e+001 -2.180822e+000 -1.396862e+000 -2.446643e+000 -1.181949e+000 -7.996896e-001 -1.016762e-001 -7.419972e-001 3.919988e-001 -5.215828e-001 1.565765e-001 -3.377319e-001 8.671799e+001 -9.298926e-002 2.901134e-002 4.130533e-002 1.034688e-001 7.809825e-002 9.596026e-002 8.092353e-002 7.977331e-002 3.625449e-002 4.781009e-002 3.325168e-002 2.852417e-002 -9.843664e-001 2.642885e-001 -4.812316e-002 -6.192347e-002 -7.445950e-002 -4.591020e-002 -6.312832e-002 -1.340696e-002 -1.335488e-002 -2.142544e-002 -5.056712e-003 -1.740669e-002 -1.929692e-003 3.522585e-001]';
vr(:,5) = [1.441225e+001 6.153384e+000 3.242299e+000 3.330192e+000 2.216910e+000 1.515056e+000 1.263190e+000 1.195761e+000 9.233552e-001 9.070599e-001 7.297287e-001 6.392971e-001 1.302825e+002 1.897619e+000 4.155956e-001 2.419320e-001 2.701677e-001 1.928546e-001 1.944531e-001 1.143128e-001 1.093282e-001 8.951026e-002 7.734641e-002 7.008259e-002 6.078850e-002 6.987491e+000 1.973060e-001 7.574771e-002 4.830537e-002 4.031886e-002 3.337642e-002 2.884867e-002 2.319209e-002 2.034803e-002 1.702675e-002 1.536277e-002 1.364653e-002 1.249755e-002 9.661368e-001]';
cv(:,:,5) = diag(vr(:,5));

mu(:,6) = [ -7.313049e+000 -9.398881e-001 -4.510057e-001 -7.288507e-001 -2.897759e-001 -7.751375e-002 -3.618123e-003 -1.390386e-001 2.084902e-001 -1.320959e-001 8.817019e-002 -1.228079e-001 6.917090e+001 -3.107722e-002 8.907292e-003 9.247836e-003 1.905289e-002 1.233459e-002 1.001064e-002 1.002233e-002 8.150973e-003 3.113299e-003 3.548607e-003 2.532211e-003 1.085190e-003 -1.025690e-001 7.951854e-003 -8.182072e-003 -6.170174e-003 -1.514464e-002 -6.019351e-003 -8.480256e-003 -3.986343e-003 -5.266401e-003 -1.843392e-003 -2.877901e-003 -8.476687e-004 -6.497114e-004 8.289810e-002]';
vr(:,6) = [3.163979e+000 2.168602e+000 1.371846e+000 1.517981e+000 9.038908e-001 8.897811e-001 6.909518e-001 5.652684e-001 5.599021e-001 5.125359e-001 4.591469e-001 4.670824e-001 3.629802e+001 1.388832e-001 9.953099e-002 7.722628e-002 7.820329e-002 6.347974e-002 6.216895e-002 5.599645e-002 5.214563e-002 4.893730e-002 4.565161e-002 4.265450e-002 4.052969e-002 8.744203e-001 2.787767e-002 2.031806e-002 1.623932e-002 1.644616e-002 1.327494e-002 1.286692e-002 1.156766e-002 1.087449e-002 1.014029e-002 9.546626e-003 8.929418e-003 8.537616e-003 1.812941e-001]';
cv(:,:,6) = diag(vr(:,6));
gconst(6) = -1.317014e+001;


trans = [
    0.000000e+000 1.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 9.649747e-001 3.502526e-002 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 8.346829e-001 1.653171e-001 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 8.426186e-001 1.573814e-001 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 8.972309e-001 1.027692e-001 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 9.649747e-001 3.502526e-002;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
    ];
A=trans;

trans2 = [
    9.649747e-001 3.502526e-002 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 8.346829e-001 1.653171e-001 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 8.426186e-001 1.573814e-001 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 8.972309e-001 1.027692e-001;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 1.000000e+000;
    ];

% trans = [
%     0.000000e+000 1.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
%     0.000000e+000 9.649747e-001 3.502526e-002 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
%     0.000000e+000 0.000000e+000 8.346829e-001 1.653171e-001 0.000000e+000 0.000000e+000 0.000000e+000;
%     0.000000e+000 0.000000e+000 0.000000e+000 8.426186e-001 1.573814e-001 0.000000e+000 0.000000e+000;
%     0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 8.972309e-001 1.027692e-001 0.000000e+000;
%     0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 1.000000e+000 0.000000e+000;
%     0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
%     ];


B=zeros(states,size(O,2));

for i = 1 : size(O,2)
    o = O(:,i);
    for j = 2 : states-1
        B(j,i) =  (1/((((2*pi)^39)*det(cv(:,:,j)))^(1/2)))*exp(-0.5*(o-mu(:,j))'*inv(cv(:,:,j))*(o-mu(:,j)));
    end

end

% pr = eye(states);
% pr2 = eye(states-2);
% pr = [1;0;0;0;0;0;0];
pr = [0;1;0;0;0;0;0];
pr2 = [1;0;0;0;0];

mu2=mu(:,2:6);
cv2=cv(:,:,2:6);
%B2=mk_ghmm_obs_mat(O,mu2,cv2);

[ALPHA, BETA] = f_b(trans, B);
%[ALPHA2, BETA2, GAMMA2, XI2, LOGLIK2] = forwards_backwards(pr2, trans2, B2);
%[ALPHA2, BETA2] = f_b(pr, trans, B);

% trans(1,:)*(B(:,1).*BETA(:,1))
% trans2(1,:)*(B2(:,1).*BETA2(:,1))

p=0;
%p2=0;
for i=1 : 1 : size(O,2)
    
    p = p + ALPHA(:,i)'*BETA(:,i);
    %p2= p2 + (GAMMA2(:,i)'*B2(:,i));
end
%p2
p

v=viter(A,B)