function [priorE, transmatE, obsmatE] = treningDS(data, broj_trening_podataka, States)
%diskretni sa smjerom + broj prstiju

O = 18*6; %velicina codebook-a
podatci=data;

for i = 1:broj_trening_podataka
    for j=2:36
        y_dist = podatci{i}(j,2)-podatci{i}(j-1,2);
        x_dist = podatci{i}(j,1)-podatci{i}(j-1,1);
        if x_dist == 0
           x_dist = 0.00000001;
        end
        a = atand(y_dist/x_dist)+0.01;
        if y_dist < 0 && x_dist > 0
            a = a + 360;
        elseif (y_dist < 0 && x_dist < 0) || (y_dist > 0 && x_dist < 0)
            a = a + 180;
        end
        
        testdata = round(a/20);
        if testdata == 0
            testdata = 1;
        elseif testdata == -4
            testdata = 14;
        end
        if podatci{i}(j,3)>5
                podatci{i}(j,3)=5;
        elseif podatci{i}(j,3)==0
                podatci{i}(j,3)=6;
        end
        
%         if i == 21
%            podataktocan(j-1)=(testdata-1)*6 + (podatci{i}(j,3)+1);
%         else
           podatak((i-1)*30+j-1)=(testdata-1)*6 + (podatci{i}(j,3));
%         end
        
    end
end

Q = States; %broj stanja

    % initial guess of parameters
    priorG = normalise(rand(Q,1));
    transmatG = mk_stochastic(rand(Q,Q));
    obsmatG = mk_stochastic(rand(Q,O));
    
    % improve guess of parameters using EM
    [LL, priorE, transmatE, obsmatE] = dhmm_em(podatak, priorG, transmatG, obsmatG, 'max_iter', 7);
    %DSnex20(Q)=LL(end);

%osiguranje da niti jedan clan matrice nije 0
for i = 1:(length(obsmatE)*size(obsmatE,1))
    if obsmatE(i) == 0
        obsmatE(i) = 2e-200;
    end
end
for i = 1:(length(transmatE)*size(transmatE,1))
    if transmatE(i) == 0
        transmatE(i) = 2e-200;
    end
end

%loglik = dhmm_logprob(podatatocan, priorE, transmatE, obsmatE)
%loglik = dhmm_logprob(podatakrivi, priorE, transmatE, obsmatE)
end