%BCH decoder using RiBM and Chien search
close all; clear all;
%parameter
m=5;
n=2^m-1;
t=1;
k=2^m-1-m*t;
%the constants used in the algorithm
alpha = gf(2, m);
zero = gf(0, m);
one = gf(1, m);
%since all zero is always a codeword, only have to specify the error
code=gf(zeros(1, n), 1);
%error injection, location
code(2)=gf(1, 1);
codev = code.x;
reccode=gf(codev, m);
%creating alpha array
%note that syndrome should be in the order [s3, s2, s1, s0]
alpha_tb=gf(zeros(1, 2*t), m);
for i=1:2*t,
alpha_tb(i)=alpha^(2*t-i+1);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%syndrome generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%
syndrome=gf(zeros(1, 2*t), m);
for i=1:n,
syndrome=syndrome.*alpha_tb+reccode(i);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%RiBM algorithm
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%init
max = 3*t+1;
k = 0;
gamma = one;
theta = gf(zeros(1, max), m); %3*t+1
delta = gf(zeros(1, max+1), m); %3*t+2
theta(1:2*t) = syndrome; %2*t
delta(1:2*t) = syndrome;
theta(max) = one;
delta(max) = one;
for r=1:2*t,
delta_o = delta;
%step1
delta(1:max) = gamma*delta(2:max+1)-delta(1)*theta;
%step2
if((delta_o(1) ~= zero) && (k>=0))
theta = delta_o(2:max+1);
gamma = delta_o(1);
k=-k-1;
else
theta = theta;
gamma = gamma;
k=k+1;
end
end
%lambda
lambda = delta(t+1:2*t+1);
%inverstable
inverse_tb = gf(zeros(1, t+1), m);
for i=1:t+1,
inverse_tb(i) = alpha^(-i+1);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%chien's search
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%note: no fail detection
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
found = find(error(1,:)~=0)