6 #include <itpp/itbase.h>
16 for (
int i=0; i<u.size(); i++){
37 void init(itpp::GF2mat H_temp);
47 int decode(itpp::bvec syndrome,
const itpp::vec & LLRin, itpp::vec &LLRout);
48 int bp_syndrome_llr(itpp::bvec syndrome,
const itpp::vec & LLRin, itpp::vec & LLRout);
49 int bp_schedule(itpp::bvec syndrome,
const itpp::vec & LLRin, itpp::vec & LLRout);
51 int bp_flexible( itpp::bvec syndrome,
const itpp::vec & LLRin, itpp::vec & LLRout);
65 debug = debug_mode_temp;
72 for (
int i =0; i<H_temp.rows();i++){
73 if (
summation(H_temp.get_row(i)) < 2 )
return false;
76 for (
int j = 0; j< H_temp.cols(); j++){
77 if (
summation(H_temp.get_col(j)) < 1 )
return false;
87 if ( !
is_H_valid(
H) )
throw std::invalid_argument(
"BP_Decoder: invalid parity check matrix H" );
90 for (
int i =0; i<
ncheck; i++){
91 for (
int j = 0; j<
nvar;j++){
100 std::cout<<
"--- BP_Decoder --- nvar = "<<
nvar
108 std::cout<<
"decoder is not initialized"<<std::endl;
125 switch (decode_mode_temp){
137 throw std::invalid_argument(
"BP_Decoder: illegal decode mode" );
146 if (decode_mode_str_temp ==
"standard")
decode_mode = 1;
147 else if (decode_mode_str_temp ==
"min sum")
decode_mode = 2;
148 else if (decode_mode_str_temp ==
"normalization")
decode_mode = 3;
149 else throw std::invalid_argument(
"BP_Decoder: illegal decode mode string" );
175 for (
int i =0; i<
ncheck; i++ ){
176 for (
int j = 0; j<
nvar; j ++){
193 schedule_mode_str =
"edge by edge, switch var and check order. using bp_flexible()";
197 for (
int j = 0; j<
nvar; j ++){
198 for (
int i =0; i<
ncheck; i++ ){
218 int permutation=2*
nedge;
219 itpp::RNG_randomize();
220 itpp::ivec source=itpp::randi(permutation,0,
nedge*2-1);
221 itpp::ivec target=itpp::randi(permutation,0,
nedge*2-1);
222 for (
int i =0; i< permutation; i++){
223 schedule.swap_rows(source(i),target(i));
233 std::cout<<
"start updating schedule"<<std::endl;
234 for (
int j = 0; j<
nvar; j ++){
235 for (
int i =0; i<
ncheck; i++ ){
243 for (
int i =0; i<
ncheck; i++ ){
252 std::cout<<
"finish updating schedule"<<std::endl;
257 throw std::invalid_argument(
"BP_Decoder: illegal schedule" );
276 throw std::invalid_argument(
"BP_Decoder: illegal schedule" );
282 itpp::bvec error = LLR < 0;
283 return itpp::GF2mat(
H*error - syndrome).is_zero();
296 if ( itpp::GF2mat(syndrome).is_zero() ){
304 if (
debug) std::cout<<
"nvar = "<<
nvar <<
", ncheck = "<<
ncheck<<std::endl;
309 for (
int i = 0; i<
ncheck ; i++){
310 for (
int j=0; j<
nvar; j++){
312 llrs.set(i,j,LLRin(j));
330 for (
int i = 0; i<
ncheck ; i++){
331 for (
int j=0; j<
nvar; j++){
334 str =
"prod list: i,j,k,prod,llr:";
335 for (
int k=0; k<
nvar; k++){
338 prod = prod * tanh( llrs(i,k)/2 );
339 str += std::to_string(i)+
",";
340 str += std::to_string(j)+
",";
341 str += std::to_string(k)+
",";
342 str += std::to_string(prod)+
",";
343 str += std::to_string(llrs(i,k))+
",";
355 if (
debug)
if ( std::abs(LLR) > 1000000.0) std::cout<<
"LLRs: LLR = "<<LLR<<
", prod = "<<prod<<
"\n"<<str<<std::endl<<
"H.get_row(i)="<<
H.get_row(i)<<std::endl <<
"llrs.get_row(i)="<<llrs.get_row(i)<<std::endl;
360 for (
int i = 0; i<
ncheck ; i++){
361 for (
int j=0; j<
nvar; j++){
366 for (
int k=0; k<
nvar; k++){
375 prod = (llr < prod) ? llr: prod;
394 if (
debug)
if ( std::abs(LLR) >
INF_BP ) std::cout<<
"LLRs: LLR = "<<LLR<<
", prod = "<<prod<<
"\n"<<str<<std::endl<<
"H.get_row(i)="<<
H.get_row(i)<<std::endl <<
"llrs.get_row(i)="<<llrs.get_row(i)<<std::endl;
405 for (
int i = 0; i<
ncheck ; i++){
406 for (
int j=0; j<
nvar; j++){
410 for (
int t=0; t<
ncheck; t++){
429 for (
int j=0; j<
nvar; j++){
432 for (
int t=0; t<
ncheck; t++){
441 if (
debug) std::cout<<
"update_count = "<<update_count<<
", LLRout = "<<floor(LLRout)<<std::endl ;
450 if (
debug) std::cout<<
"LLRout = "<<LLRout<<std::endl;
457 update_count = - update_count;
460 return update_count ;
473 if ( itpp::GF2mat(syndrome).is_zero() ){
481 if (
debug) std::cout<<
"nvar = "<<
nvar <<
", ncheck = "<<
ncheck<<std::endl;
486 for (
int i = 0; i<
ncheck ; i++){
487 for (
int j=0; j<
nvar; j++){
489 llrs.set(i,j,LLRin(j));
507 for (
int i = 0; i<
ncheck ; i++){
508 for (
int j=0; j<
nvar; j++){
512 for (
int k=0; k<
nvar; k++){
515 prod = prod * tanh( llrs(i,k)/2 );
532 if (
debug)
if ( std::abs(LLR) > 1000000.0) std::cout<<
"LLRs: LLR = "<<LLR<<
", prod = "<<prod<<
"\n"<<str<<std::endl<<
"H.get_row(i)="<<
H.get_row(i)<<std::endl <<
"llrs.get_row(i)="<<llrs.get_row(i)<<std::endl;
542 for (
int t=0; t<
ncheck; t++){
561 for (
int j=0; j<
nvar; j++){
564 for (
int t=0; t<
ncheck; t++){
573 if (
debug) std::cout<<
"update_count = "<<update_count<<
", LLRout = "<<floor(LLRout)<<std::endl ;
582 if (
debug) std::cout<<
"LLRout = "<<LLRout<<std::endl;
589 update_count = - update_count;
592 return update_count ;
607 if ( itpp::GF2mat(syndrome).is_zero() )
return 1;
611 if (
debug) std::cout<<
"nvar = "<<
nvar <<
", ncheck = "<<
ncheck<<std::endl;
615 for (
int i = 0; i<
ncheck ; i++){
616 for (
int j=0; j<
nvar; j++){
618 llrs.set(i,j,LLRin(j));
633 for (
int is = 0; is <
nedge*2; is ++){
639 switch ( direction ){
646 for (
int k=0; k<
nvar; k++){
648 if ( k != j ) prod = prod * tanh( llrs(i,k)/2 );
653 if ( syndrome(i) ) LLR = -LLR;
656 if (
debug)
if ( std::abs(LLR) > 1000000.0) std::cout<<
"LLRs: LLR = "<<LLR<<
", prod = "<<prod<<
"\n"<<str<<std::endl<<
"H.get_row(i)="<<
H.get_row(i)<<std::endl <<
"llrs.get_row(i)="<<llrs.get_row(i)<<std::endl;
668 for (
int k=0; k<
nvar; k++){
677 prod = (llr<prod) ? llr:prod;
682 if ( syndrome(i) ) LLR = -LLR;
685 if (
debug)
if ( std::abs(LLR) >
INF_BP ) std::cout<<
"LLRs: LLR = "<<LLR<<
", prod = "<<prod<<
"\n"<<str<<std::endl<<
"H.get_row(i)="<<
H.get_row(i)<<std::endl <<
"llrs.get_row(i)="<<llrs.get_row(i)<<std::endl;
696 for (
int t=0; t<
ncheck; t++){
710 for (
int j=0; j<
nvar; j++){
713 for (
int t=0; t<
ncheck; t++){
722 if (
debug) std::cout<<
"update_count = "<<update_count<<
", LLRout = "<<floor(LLRout)<<std::endl ;
731 if (
debug) std::cout<<
"LLRout = "<<LLRout<<std::endl;
738 update_count = - update_count;
741 return update_count ;