📄 bchcore.m
字号:
function [msg, err, ccode] = bchcore(code, pow_dim, dim, k, t, tp)
%BCHCORE The core part of the BCH decode.
% [MSG, ERR, CCODE] = BCHCORE(CODE, POW_DIM, DIM, K, T, TP) decodes
% a BCH code, in which CODE is a code word row vector, with its column
% size being POW_DIM. POW_DIM equals 2^DIM -1. K is the message length,
% T is the error correction capability. TP is a complete list of the
% elements in GF(2^DIM).
%
% This function is can share the information between a SIMULINK file and
% MATLAB functions. It is not designed to be called directly. There is
% no error check in order to eliminate overhead.
%
% Wes Wang 8/5/94, 9/30/95
% Copyright (c) 1995-96 by The MathWorks, Inc.
% $Revision: 1.1 $ $Date: 1996/04/01 17:51:57 $
code = code(:)';
err = 0;
tp_num = tp * 2.^[0:dim-1]';
tp_inv(tp_num+1) = 0:pow_dim;
% **(1)** syndrome computation.
% initialization, find all non-zeros to do the calculation
non_zero_itm = find(code > 0) - 1;
len_non_z_itm = length(non_zero_itm);
syndrome = -ones(1, 2*t);
% syndrome number is 2*t where t is error correction capability
if len_non_z_itm > 0
% for n_j = 1 : 2*t
% syndrome(n_j) = non_zero_itm(1) * n_j;
% if len_non_z_itm > 1
% for n_k = 2 : len_non_z_itm
% syndrome(n_j) = gfplus(syndrome(n_j), non_zero_itm(n_k)*n_j, tp_num, tp_inv);
% end;
% end;
% end;
tmp = 1:2*t;
syndrome(tmp) = non_zero_itm(1) * tmp;
if len_non_z_itm > 1
for n_k = 2 : len_non_z_itm
syndrome(tmp) = gfplus(syndrome(tmp), non_zero_itm(n_k) * tmp, tp_num, tp_inv);
end;
end;
end;
% complete syndrome computation
% **(2)** determine the error-location polynomial.
% This step is the most complicated part in the BCH decode.
% reference to p158 of Shu Lin's Error Control Coding,
% the simplified algorithm for finding sigma_x.
% the maximum degree of the error-location polynomial is t
% if the degree is larger than t, there are more than t errors and
% the algorithm cannot find a solution to correct the errors.
% for BCH code, use simplified method. Note that when you call
% errlocp, the parameter should be exact.
[sigma, err] = errlocp(syndrome, t, tp, pow_dim, err, 0);
% ***(3)*** computation of error-location numbers.
loc_err = zeros(1, pow_dim);
% in case of failed or no error, skip.
num_err = length(sigma) - 1;
if (~err) & (num_err > 0)
cnt_err = 0;
pos_err = [];
er_i = 0;
while (cnt_err < num_err) & (er_i < pow_dim * dim)
test_flag = sigma(1);
for er_j = 1 : num_err
if sigma(er_j + 1) >= 0
% The following 6 lines is equivelent to
% tmp = gfmul(er_i * er_j, sigma(er_j+1), tp);
tmp = er_i * er_j;
if (tmp < 0) | (sigma(er_j+1) < 0)
tmp = -1;
else
tmp = rem(tmp + sigma(er_j + 1), pow_dim);
end;
test_flag = gfplus(test_flag, tmp, tp_num, tp_inv);
end;
end;
if test_flag < 0
cnt_err = cnt_err + 1;
pos_err = [pos_err, rem(pow_dim-er_i, pow_dim)];
end;
er_i = er_i + 1;
end;
pos_err = rem(pow_dim+pos_err, pow_dim);
pos_err = pos_err + 1; % shift one location because power zero is one.
loc_err(pos_err) = ones(1, cnt_err);
err = num_err;
else
if err
err = -1;
end;
end;
% completed error location detection
% correct the error
ccode = rem(code + loc_err, 2);
msg = ccode(pow_dim-k+1 : pow_dim);
%-- end of bchcore---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -