function vystupdekoderu = bchbmdeksim(n, k, m, pp, prijateslovo)

%     clc;     
t=bchnumerr(n,k);         %korekční schopnost


prijatepom = gf(zeros(1,n),m,pp);           % převedení slova do prvků GF
for i = 1:n                            % invertace prijateho slova
    prijatepom(i) = prijateslovo(i);
end

prijate_chybne = prijatepom;

alpha = gf(2, m);
zero = gf(0, m);
one = gf(1, m);


alpha_tb=gf(zeros(1, 2*t), m);
for i=1:2*t,
    alpha_tb(i)=alpha^(2*t-i+1);
end;

%Vypocet syndromu
syndrome=gf(zeros(1, 2*t), m);
syndromepom=gf(zeros(1, 2*t), m);
for i=1:n,
    syndrome=syndrome.*alpha_tb+prijate_chybne(i);
end;

% Zacatek BM algoritmu
lambda = gf([1, zeros(1, t)], m);
lambda0= lambda;
b=gf([0, 1, zeros(1, t)], m);
b2 = gf([0, 0, 1, zeros(1, t)], m);
kk=0;
gamma = one;
delta = zero;
syndrome_array = gf(zeros(1, t+1), m);

    for r=1:2*t,
        r1 = 2*t-r+1;
        r2 = min(r1+t, 2*t);
        num = r2-r1+1;
        syndrome_array(1:num) = syndrome(r1:r2);
        delta = syndrome_array*lambda';

        lambda0 = lambda;
        lambda = gamma*lambda-delta*b(1:t+1);

        if((delta ~= zero) && (k>=0))
            b(2:2+t)=lambda0;
            gamma = delta;
            kk=-kk-1;
        else
            b(2:2+t) = b(1:t+1);
            gamma = gamma;
            kk=kk+1;
        end   
    end

inverse_tb = gf(zeros(1, t+1), m);
for i=1:t+1,
    inverse_tb(i) = alpha^(-i+1);
end;

%chien search
lambda_v = zero;
accu_tb=gf(ones(1, t+1), m);
for i=1:n,
    lambda_v=lambda*accu_tb';
    accu_tb = accu_tb.*inverse_tb;
    if(lambda_v==zero)
        error(1,n-i+1)=1;    
    else
        error(1,n-i+1)=0;
    end
end

vektor_chyb=error;
opravenopom = prijate_chybne + vektor_chyb;
opraveno=opravenopom(1:k);
vystupdekoderu = double(opraveno.x);