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

param = size(O,1);
states = 9;

mu=zeros(param,states);
vr=zeros(param,states);
cv=zeros(param,param,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) = [ -6.545323e+000 -3.892320e+000 -1.636953e+000 -2.222779e+000 -1.014096e+000 -9.023929e-001 1.103083e-003 -2.985399e-001 1.321728e-001 -2.284619e-001 5.709594e-002 -2.461104e-001 8.240534e+001 -1.893980e-002 -3.273061e-001 -1.775057e-001 -6.073304e-002 4.163495e-002 -4.962487e-002 3.911957e-002 3.734658e-002 3.037159e-002 -1.502426e-002 9.716090e-003 1.767162e-002 1.140246e+000 1.125629e-001 -1.703891e-002 -9.927938e-002 -1.330571e-001 -5.118115e-002 -7.256605e-002 -4.440103e-002 -3.517200e-002 -2.585248e-002 -2.186541e-002 -1.990273e-002 -1.605375e-002 8.911036e-001];
vr(:,3) = [1.270238e+001 1.381968e+001 5.881086e+000 5.416528e+000 2.727337e+000 2.071733e+000 1.482240e+000 1.328801e+000 1.007770e+000 7.911476e-001 6.821217e-001 5.817094e-001 1.626822e+002 1.510991e+000 1.742701e+000 5.902455e-001 7.072293e-001 3.172706e-001 2.990301e-001 1.996341e-001 1.575205e-001 1.155321e-001 9.620684e-002 8.330090e-002 7.047909e-002 2.746779e+001 2.696688e-001 3.676652e-001 9.530207e-002 9.549393e-002 5.675465e-002 4.484101e-002 3.335138e-002 2.812197e-002 2.218434e-002 1.852601e-002 1.630673e-002 1.398936e-002 4.171160e+000];
cv(:,:,3) = diag(vr(:,3));

mu(:,4) = [-2.077660e+000 -5.943670e+000 -5.619095e+000 -3.970376e+000 -2.300948e+000 -3.193937e+000 -5.405280e-001 -6.173729e-001 -2.782240e-001 -1.025004e+000 -4.223464e-001 -5.536336e-001 1.048845e+002 2.481232e-001 2.380227e-001 2.343757e-002 -4.793472e-003 -1.007345e-001 -8.967271e-002 -8.159436e-002 -2.947651e-002 -5.043452e-002 -2.403574e-002 -3.524349e-002 -3.222278e-002 -2.256212e-001 -2.169646e-001 1.886567e-001 2.033907e-001 6.488508e-002 5.190995e-002 8.473147e-002 7.982724e-003 -5.154938e-003 1.239212e-002 3.178590e-002 1.669582e-002 1.047845e-002 -1.028436e+000];
vr(:,4) = [1.003157e+001 7.110540e+000 6.175197e+000 2.996074e+000 2.943091e+000 2.364480e+000 2.402321e+000 2.101370e+000 1.451301e+000 1.196608e+000 1.028480e+000 9.830235e-001 1.746975e+002 1.328967e+000 9.059880e-001 1.020386e+000 3.343944e-001 2.310837e-001 2.622339e-001 1.925851e-001 1.550811e-001 1.193967e-001 1.114187e-001 9.716111e-002 8.393103e-002 2.191437e+001 2.028668e-001 1.234960e-001 1.171156e-001 5.406774e-002 4.283670e-002 4.009138e-002 3.749999e-002 2.948771e-002 2.371191e-002 2.061919e-002 1.876946e-002 1.656367e-002 2.170150e+000];
cv(:,:,4) = diag(vr(:,4));

mu(:,5) = [-1.245704e+001 1.727279e-001 -2.044427e+000 -3.347555e+000 -5.640296e-001 -1.499485e+000 9.318018e-002 -8.240026e-001 4.853948e-001 -6.545312e-001 2.885908e-001 -3.635652e-001 8.940186e+001 -1.751969e-001 -1.747059e-002 -6.579599e-002 -6.120324e-002 8.652072e-002 -6.479993e-003 8.936679e-005 1.443504e-002 4.219803e-003 2.231600e-003 2.203631e-002 1.013340e-002 4.307183e-001 2.296666e-001 -9.673871e-002 -4.173670e-002 -8.965551e-002 -7.436113e-002 -4.623044e-002 -3.472226e-002 -3.118100e-002 -2.427243e-002 -9.716285e-003 -1.942393e-002 -5.564139e-003 5.645667e-001];
vr(:,5) = [3.188791e+001 8.206201e+000 4.536571e+000 4.255475e+000 2.764527e+000 1.853348e+000 1.402862e+000 1.214313e+000 9.666463e-001 8.940721e-001 7.845169e-001 6.380580e-001 1.713976e+002 4.418462e+000 6.682598e-001 3.245983e-001 4.629432e-001 3.485309e-001 1.991201e-001 1.743232e-001 1.410920e-001 1.139222e-001 9.160137e-002 9.185158e-002 7.063425e-002 1.538414e+001 5.061538e-001 1.195446e-001 6.700704e-002 6.336254e-002 4.179326e-002 3.278161e-002 2.870406e-002 2.408507e-002 2.016679e-002 1.808972e-002 1.666469e-002 1.409546e-002 2.838624e+000];
cv(:,:,5) = diag(vr(:,5));

mu(:,6) = [ -6.545323e+000 -3.892320e+000 -1.636953e+000 -2.222779e+000 -1.014096e+000 -9.023929e-001 1.103083e-003 -2.985399e-001 1.321728e-001 -2.284619e-001 5.709594e-002 -2.461104e-001 8.240534e+001 -1.893980e-002 -3.273061e-001 -1.775057e-001 -6.073304e-002 4.163495e-002 -4.962487e-002 3.911957e-002 3.734658e-002 3.037159e-002 -1.502426e-002 9.716090e-003 1.767162e-002 1.140246e+000 1.125629e-001 -1.703891e-002 -9.927938e-002 -1.330571e-001 -5.118115e-002 -7.256605e-002 -4.440103e-002 -3.517200e-002 -2.585248e-002 -2.186541e-002 -1.990273e-002 -1.605375e-002 8.911036e-001];
vr(:,6) = [1.270238e+001 1.381968e+001 5.881086e+000 5.416528e+000 2.727337e+000 2.071733e+000 1.482240e+000 1.328801e+000 1.007770e+000 7.911476e-001 6.821217e-001 5.817094e-001 1.626822e+002 1.510991e+000 1.742701e+000 5.902455e-001 7.072293e-001 3.172706e-001 2.990301e-001 1.996341e-001 1.575205e-001 1.155321e-001 9.620684e-002 8.330090e-002 7.047909e-002 2.746779e+001 2.696688e-001 3.676652e-001 9.530207e-002 9.549393e-002 5.675465e-002 4.484101e-002 3.335138e-002 2.812197e-002 2.218434e-002 1.852601e-002 1.630673e-002 1.398936e-002 4.171160e+000];
cv(:,:,6) = diag(vr(:,6));

mu(:,7) = [-4.481637e+000 -8.077502e+000 -4.016692e+000 -3.959216e+000 -1.943099e+000 -1.942464e+000 -5.334513e-002 -1.779836e+000 -9.108570e-001 -8.647965e-001 -1.575557e-001 -3.417000e-001 1.108569e+002 1.572965e-001 1.522747e-001 3.694922e-002 6.427733e-002 -1.576646e-002 2.460924e-003 -4.861242e-002 -8.977579e-003 -2.786925e-002 -1.844933e-003 -7.239067e-003 -6.211887e-003 -7.288184e-001 -5.171122e-002 2.275510e-001 9.396244e-002 3.644866e-002 2.680325e-002 8.704010e-003 -1.878852e-002 3.775245e-002 2.848845e-002 1.600545e-002 3.224804e-003 4.063338e-004 -1.001832e+000];
vr(:,7) = [1.090561e+001 9.380291e+000 4.552089e+000 4.492853e+000 3.058557e+000 2.528795e+000 2.069547e+000 2.826441e+000 1.757432e+000 1.402670e+000 1.125660e+000 1.032359e+000 2.222020e+002 7.745796e-001 1.412038e+000 4.231963e-001 3.036933e-001 1.844625e-001 1.880224e-001 1.470821e-001 1.549169e-001 1.284645e-001 1.011812e-001 8.199082e-002 6.975959e-002 2.564225e+001 1.581634e-001 1.436237e-001 5.938128e-002 5.372630e-002 3.572884e-002 3.539442e-002 2.820129e-002 2.596946e-002 2.251661e-002 1.860138e-002 1.593943e-002 1.345356e-002 2.273973e+000];
cv(:,:,7) = diag(vr(:,7));

mu(:,8) = [ -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(:,8) = [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(:,:,8) = diag(vr(:,8));
gconst(8) = -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 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 0.000000e+000 8.789026e-001 1.210974e-001 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 8.323556e-001 1.676444e-001 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 9.190401e-001 8.095991e-002 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 8.789026e-001 1.210974e-001 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 8.722202e-001 1.277798e-001 0.000000e+000;
    0.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 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 0.000000e+000;
    0.000000e+000 8.789026e-001 1.210974e-001 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 8.323556e-001 1.676444e-001 0.000000e+000 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 9.190401e-001 8.095991e-002 0.000000e+000 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 8.722202e-001 1.277798e-001 0.000000e+000;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 8.722202e-001 1.277798e-001;
    0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000 1.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)^param)*det(cv(:,:,j)))^(1/2)))*exp(-0.5*(o-mu(:,j))'*inv(cv(:,:,j))*(o-mu(:,j)));
    end
end

pr = [0;1;0;0;0;0;0;0;0];
pr2 = [1;0;0;0;0;0;0];

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

[ALPHA, BETA] = f_b(trans, B);
% [ALPHA, BETA, GAMMA, XI, LOGLIK] = forwards_backwards(pr, 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))
% trans(2,:)*(B(:,1).*BETA(:,1))
% trans2(1,:)*(B2(:,1).*BETA2(:,1))
    
%BETA2(1: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)