function vystupdekoderu = bmdeksim(n, k, m, pp,  prijateslovo)
t=(n-k)/2; 

prijatepom = gf(prijateslovo,m,pp);           % prevedeni prijateho slova do prvků GF
polynom_chyb = gf(zeros(1,n), m, pp);

prijate_chybne = prijatepom;

%vytvoreni alpha podle galoisova pole
alpha = gf(2, m, pp);
zero = gf(0, m, pp);
one = gf(1, m, pp);

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

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

for i=1:2*t,
    syndrome(i)=syndrome_o(2*t+1-i);
end;

% % % % % % % % % % % % % % % %  zacatek BM algoritmu

lamda = gf([1, zeros(1, t)], m, pp);
lamda0 = gf([1, zeros(1, t)], m, pp);
b = gf([0, 1, zeros(1, t)], m, pp);
kkk = 0;
gamma = one;
delta = zero;
syndrome_array1 = gf(zeros(1, t+1), m, pp);

for r = 1:2*t,
   
    r1 = 2*t-r+1;
    r2 = min(r1+t, 2*t);
    num = r2-r1+1;
    syndrome_array1(1:num) = syndrome(r1:r2);
    delta = syndrome_array1*lamda';
    
    
    lamda0 = lamda;
    lamda = gamma*lamda-delta*b(1:t+1);
    
  
    if((delta ~= zero) && (k>=0))
        b(2:2+t)=lamda0;
        gamma = delta;
        kkk=-kkk-1;
    else
        b(2:2+t) = b(1:t+1);
        gamma = gamma;
        kkk=kkk+1;
    end
    
   mini=1;
end
 
lamda2=double(lamda.x);

omega = gf(zeros(1, t), m, pp);
syndrome_array2 = gf(zeros(1, t), m, pp);
for i= 1:t,
    i1 = 2*t-i+1;
    syndrome_array2(1:i) = syndrome(i1: 2*t);
    omega(i) = syndrome_array2*lamda2(1:t)';   
end;

%chien search a forneyův algoritmus

even=floor(t/2)*2;
if(even==t)
    odd=t-1;
else
    odd=t;
end

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


%postupné počítání hodnoty chyby
lamda_v=gf(0, m, pp);
lamda_ov=gf(0, m, pp);
omega_v=gf(0, m, pp);
accu_tb=gf(ones(1, t+1), m,pp);
accu_tb1=gf(ones(1, t), m, pp);
for i=1:n,
    root=alpha^(1-i);
    lamda_v=lamda*accu_tb';
    lamda_ov=lamda(2:2:odd+1)*accu_tb(2:2:odd+1)';
    omega_v=omega*accu_tb1';
    
    accu_tb=accu_tb.*inverse_tb;
    accu_tb1=accu_tb1.*inverse_tb(1:t);
    
    if(lamda_v==zero)
        error(1,n-i+1)=1;
         if lamda_ov ~= 0
        ev=(omega_v/lamda_ov)*root;
     
        evv=ev.x;
        error(2, n-i+1)=double(evv);
         end
    else
        error(1, n-i+1)=0;
        error(2, n-i+1)=0;
    end
    kk=1;
end


hledej = find(error(1,:));    %na prvnim radku hledej nenulove hodnoty
error(2, hledej);             % na techto pozicich jsou hodnoty chyb

count = 1;
pozice = 0;
hodnotachyb = 0;
for i=1:n,
   if (error(1,i) == 1)
       polynom_chyb(i) = error(2,i);
       pozice(count) = i;
       hodnotachyb(count) = error(2,i);
       count = count + 1;
   end;
end

 opraveno = polynom_chyb + prijate_chybne;
 double(opraveno.x);
vystupdekoderu=opraveno(1:k);