IT++ Logo
ldpc.h
Go to the documentation of this file.
1
29#ifndef LDPC_H
30#define LDPC_H
31
32#include <iostream>
33#include <itpp/base/gf2mat.h>
34#include <itpp/base/random.h>
35#include <itpp/base/sort.h>
36#include <itpp/comm/llr.h>
38#include <itpp/itexports.h>
39
40namespace itpp
41{
42
43// ---------------------------------------------------------------------------
44// LDPC_Parity
45// ---------------------------------------------------------------------------
46
71class ITPP_EXPORT LDPC_Parity
72{
73 friend class LDPC_Code;
74public:
76 LDPC_Parity(): init_flag(false) {}
77
79 LDPC_Parity(int ncheck, int nvar);
80
92 LDPC_Parity(const std::string& filename, const std::string& format);
93
95 LDPC_Parity(const GF2mat_sparse_alist& alist);
96
98 virtual ~LDPC_Parity() {}
99
101 void initialize(int ncheck, int nvar);
102
104 GF2mat_sparse get_H(bool transpose = false) const {
105 return (transpose ? Ht : H);
106 }
107
109 Sparse_Vec<bin> get_col(int c) const { return H.get_col(c); }
110
112 Sparse_Vec<bin> get_row(int r) const { return Ht.get_col(r); }
113
115 int get_nvar() const {
116 it_assert_debug(H.cols() == nvar,
117 "LDPC_Parity::get_nvar(): Internal error");
118 it_assert_debug(Ht.rows() == nvar,
119 "LDPC_Parity::get_nvar(): Internal error");
120 return nvar;
121 }
122
124 int get_ncheck() const {
125 it_assert_debug(H.rows() == ncheck,
126 "LDPC_Parity::get_ncheck(): Internal error");
127 it_assert_debug(Ht.cols() == ncheck,
128 "LDPC_Parity::get_ncheck(): Internal error");
129 return ncheck;
130 }
131
133 void set(int i, int j, bin value);
134
136 bin get(int i, int j) const {
137 it_assert_debug(H(i, j) == Ht(j, i), "LDPC_Parity::get(): Internal error");
138 return H(i, j);
139 }
140
142 bin operator()(int i, int j) const {
143 it_assert_debug(H(i, j) == Ht(j, i),
144 "LDPC_Parity::operator(): Internal error");
145 return H(i, j);
146 }
147
149 virtual void display_stats() const;
150
152 double get_rate() const {
153 return (1.0 - static_cast<double>(ncheck) / nvar);
154 }
155
157 void import_alist(const GF2mat_sparse_alist& H_alist);
158
160 GF2mat_sparse_alist export_alist() const;
161
163 void load_alist(const std::string& alist_file);
164
166 void save_alist(const std::string& alist_file) const;
167
168protected:
172 static const int Nmax = 200;
178 int nvar;
182 ivec sumX1;
184 ivec sumX2;
185
201 int check_for_cycles(int L) const;
202
246 int check_connectivity(int from_m, int from_n, int to_m, int to_n,
247 int g, int L) const;
248
249 // inline int get_cmax() const { return (max(sumX1)); }
250 // inline int get_vmax() const { return (max(sumX2)); }
251 // ivec get_coldegree() const;
252 // ivec get_rowdegree() const;
253};
254
255
256// ----------------------------------------------------------------------
257// LDPC_Parity_Unstructured
258// ----------------------------------------------------------------------
259
275class ITPP_EXPORT LDPC_Parity_Unstructured : public LDPC_Parity
276{
277public:
279 virtual void display_stats() const = 0;
280
299 int cycle_removal_MGW(int L);
300
301protected:
303 void generate_random_H(const ivec& C, const ivec& R, const ivec& cycopt);
304
318 void compute_CR(const vec& var_deg, const vec& chk_deg, const int Nvar,
319 ivec &C, ivec &R);
320
321};
322
323
324// ----------------------------------------------------------------------
325// LDPC_Parity_Irregular
326// ----------------------------------------------------------------------
327
333{
334public:
338 LDPC_Parity_Irregular(int Nvar, const vec& var_deg, const vec& chk_deg,
339 const std::string& method = "rand",
340 const ivec& options = "200 6");
341
380 void generate(int Nvar, const vec& var_deg, const vec& chk_deg,
381 const std::string& method = "rand",
382 const ivec& options = "200 6");
383
386};
387
388
389// ----------------------------------------------------------------------
390// LDPC_Parity_Regular
391// ----------------------------------------------------------------------
392
398{
399public:
403 LDPC_Parity_Regular(int Nvar, int k, int l,
404 const std::string& method = "rand",
405 const ivec& options = "200 6");
406
425 void generate(int Nvar, int k, int l,
426 const std::string& method = "rand",
427 const ivec& options = "200 6");
428
431};
432
433// ----------------------------------------------------------------------
434// BLDPC_Parity
435// ----------------------------------------------------------------------
436
461class ITPP_EXPORT BLDPC_Parity : public LDPC_Parity
462{
463public:
465 BLDPC_Parity(): LDPC_Parity(), Z(0), H_b(), H_b_valid(false) {}
466
468 BLDPC_Parity(const imat &base_matrix, int exp_factor);
469
471 BLDPC_Parity(const std::string &filename, int exp_factor);
472
474 void expand_base(const imat &base_matrix, int exp_factor);
475
477 int get_exp_factor() const;
478
480 imat get_base_matrix() const;
481
483 bool is_valid() const { return H_b_valid && init_flag; }
484
486 void set_exp_factor(int exp_factor);
487
489 void load_base_matrix(const std::string &filename);
490
492 void save_base_matrix(const std::string &filename) const;
493
494private:
495 int Z;
496 imat H_b;
497 bool H_b_valid;
498
500 void calculate_base_matrix();
501};
502
503
504// ----------------------------------------------------------------------
505// LDPC_Generator
506// ----------------------------------------------------------------------
507
523class ITPP_EXPORT LDPC_Generator
524{
525 friend class LDPC_Code;
526public:
528 LDPC_Generator(const std::string& type_in = ""): init_flag(false),
529 type(new std::string(type_in)) {}
531 virtual ~LDPC_Generator() {delete type;}
532
534 virtual void encode(const bvec &input, bvec &output) = 0;
535
537 std::string get_type() const { return *type; }
538
540 void mark_initialized() {init_flag = true;};
541
543 bool is_initialized() const {return init_flag;};
544private:
545 bool init_flag;
546 std::string* type;
547protected:
549 virtual void save(const std::string& filename) const = 0;
551 virtual void load(const std::string& filename) = 0;
552};
553
554
555// ----------------------------------------------------------------------
556// LDPC_Generator_Systematic
557// ----------------------------------------------------------------------
558
571{
572public:
577 bool natural_ordering = false,
578 const ivec& ind = "");
579
582
584 virtual void encode(const bvec &input, bvec &output);
585
619 ivec construct(LDPC_Parity* const H, bool natural_ordering = false,
620 const ivec& ind = "");
621
622protected:
624 virtual void save(const std::string& filename) const;
626 virtual void load(const std::string& filename);
627
628private:
629 GF2mat G; // the matrix is stored in transposed form
630};
631
632
633// ----------------------------------------------------------------------
634// BLDPC_Generator
635// ----------------------------------------------------------------------
636
644class ITPP_EXPORT BLDPC_Generator : public LDPC_Generator
645{
646public:
648 BLDPC_Generator(const std::string type = "BLDPC"):
649 LDPC_Generator(type), H_enc(), N(0), M(0), K(0), Z(0) {}
651 BLDPC_Generator(const BLDPC_Parity* const H,
652 const std::string type = "BLDPC");
653
655 int get_exp_factor() const { return Z; }
656
658 void encode(const bvec &input, bvec &output);
659
661 void construct(const BLDPC_Parity* const H);
662
663protected:
665 void save(const std::string &filename) const;
667 void load(const std::string &filename);
668
670 int N;
671 int M;
672 int K;
673 int Z;
674};
675
676
677// ----------------------------------------------------------------------
678// LDPC_Code
679// ----------------------------------------------------------------------
680
731class ITPP_EXPORT LDPC_Code : public Channel_Code
732{
733public:
735 LDPC_Code();
736
747 LDPC_Code(const LDPC_Parity* const H, LDPC_Generator* const G = 0,
748 bool perform_integrity_check = true);
749
755 LDPC_Code(const std::string& filename, LDPC_Generator* const G = 0);
756
758 virtual ~LDPC_Code() {delete dec_method;}
759
760
769 void set_code(const LDPC_Parity* const H, LDPC_Generator* const G = 0,
770 bool perform_integrity_check = true);
771
785 void load_code(const std::string& filename, LDPC_Generator* const G = 0);
786
795 void save_code(const std::string& filename) const;
796
797
806 void set_decoding_method(const std::string& method);
807
821 void set_exit_conditions(int max_iters,
822 bool syndr_check_each_iter = true,
823 bool syndr_check_at_start = false);
824
826 void set_llrcalc(const LLR_calc_unit& llrcalc);
827
828
829 // ------------ Encoding ---------------------
830
840 virtual void encode(const bvec &input, bvec &output);
842 virtual bvec encode(const bvec &input);
843
844
845 // ------------ Decoding ---------------------
846
848 virtual void decode(const bvec &, bvec &) {
849 it_error("LDPC_Code::decode(): Hard input decoding not implemented");
850 }
852 virtual bvec decode(const bvec &) {
853 it_error("LDPC_Code::decode(): Hard input decoding not implemented");
854 return bvec();
855 }
856
858 virtual void decode(const vec &llr_in, bvec &syst_bits);
860 virtual bvec decode(const vec &llr_in);
861
863 void decode_soft_out(const vec &llr_in, vec &llr_out);
865 vec decode_soft_out(const vec &llr_in);
866
889 int bp_decode(const QLLRvec &LLRin, QLLRvec &LLRout);
890
899 bool syndrome_check(const QLLRvec &LLR) const;
900
902 bool syndrome_check(const bvec &b) const;
903
912 QLLRvec soft_syndrome_check(const QLLRvec &LLR) const;
913
914 // ------------ Basic information gathering functions ------
915
917 double get_rate() const {
918 return (1.0 - static_cast<double>(ncheck) / nvar);
919 }
920
922 int get_nvar() const { return nvar; }
923
925 int get_ncheck() const { return ncheck; }
926
928 int get_ninfo() const { return nvar - ncheck; }
929
931 std::string get_decoding_method() const { return *dec_method; }
932
934 int get_nrof_iterations() const { return max_iters; }
935
937 LLR_calc_unit get_llrcalc() const { return llrcalc; }
938
940 friend ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const LDPC_Code &C);
941
942protected:
944 void decoder_parameterization(const LDPC_Parity* const H);
945
947 void integrity_check();
948
950 void setup_decoder();
951
952private:
953 bool H_defined;
954 bool G_defined;
955 int nvar;
956 int ncheck;
957 LDPC_Generator *G;
958
959 // decoder parameters
960 std::string* dec_method;
961 int max_iters;
962 bool psc;
963 bool pisc;
964 LLR_calc_unit llrcalc;
965 // Parity check matrix parameterization
966 ivec C, V, sumX1, sumX2, iind, jind;
967
968 // temporary storage for decoder (memory allocated when codec defined)
969 QLLRvec mvc, mcv;
970
972 static const int max_cnd = 200;
973};
974
975
980ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const LDPC_Code &C);
981}
982
983#endif
Channel Code class virtual interface.
Block LDPC Generator class.
Definition: ldpc.h:645
int get_exp_factor() const
Get expansion factor.
Definition: ldpc.h:655
GF2mat H_enc
Preprocessed parity check matrix.
Definition: ldpc.h:669
int Z
Expansion factor.
Definition: ldpc.h:673
int M
Number of parity check bits = H_enc.rows()
Definition: ldpc.h:671
BLDPC_Generator(const std::string type="BLDPC")
Default constructor.
Definition: ldpc.h:648
int N
Codeword length = H_enc.cols()
Definition: ldpc.h:670
int K
Number of information bits = N-M.
Definition: ldpc.h:672
Block LDPC code parity-check matrix.
Definition: ldpc.h:462
bool is_valid() const
Verify initialisation.
Definition: ldpc.h:483
BLDPC_Parity()
Default constructor.
Definition: ldpc.h:465
Generic Channel Code class.
Definition: channel_code.h:51
Parameterized "alist" representation of sparse GF(2) matrix.
Definition: gf2mat.h:99
Class for dense GF(2) matrices.
Definition: gf2mat.h:172
Low-density parity check (LDPC) codec.
Definition: ldpc.h:732
virtual bvec decode(const bvec &)
Inherited from the base class - not implemented here.
Definition: ldpc.h:852
virtual ~LDPC_Code()
Destructor.
Definition: ldpc.h:758
int get_nrof_iterations() const
Get the maximum number of iterations of the decoder.
Definition: ldpc.h:934
virtual void decode(const bvec &, bvec &)
Inherited from the base class - not implemented here.
Definition: ldpc.h:848
LLR_calc_unit get_llrcalc() const
Get LLR calculation unit used in decoder.
Definition: ldpc.h:937
std::string get_decoding_method() const
Return the decoding method.
Definition: ldpc.h:931
int get_ninfo() const
Get the number of information bits per codeword.
Definition: ldpc.h:928
int get_ncheck() const
Get the number of check nodes.
Definition: ldpc.h:925
double get_rate() const
Get the coderate.
Definition: ldpc.h:917
int get_nvar() const
Get the number of variable nodes.
Definition: ldpc.h:922
Systematic LDPC Generator class.
Definition: ldpc.h:571
LDPC_Generator_Systematic()
Default constructor.
Definition: ldpc.h:574
virtual ~LDPC_Generator_Systematic()
Virtual destructor.
Definition: ldpc.h:581
LDPC Generator pure virtual base class.
Definition: ldpc.h:524
bool is_initialized() const
Check if generator is initialized.
Definition: ldpc.h:543
void mark_initialized()
Mark generator as initialized.
Definition: ldpc.h:540
virtual ~LDPC_Generator()
Virtual destructor.
Definition: ldpc.h:531
std::string get_type() const
Return generator type.
Definition: ldpc.h:537
virtual void save(const std::string &filename) const =0
Save generator data to a file.
virtual void load(const std::string &filename)=0
Read generator data from a file.
virtual void encode(const bvec &input, bvec &output)=0
Generator specific encode function.
LDPC_Generator(const std::string &type_in="")
Default constructor.
Definition: ldpc.h:528
Irregular LDPC code generator class.
Definition: ldpc.h:333
LDPC_Parity_Irregular()
Default constructor.
Definition: ldpc.h:336
void display_stats() const
Display some information about the matrix.
Definition: ldpc.h:385
Regular LDPC code generator class.
Definition: ldpc.h:398
void display_stats() const
Display some information about the matrix.
Definition: ldpc.h:430
LDPC_Parity_Regular()
Default constructor.
Definition: ldpc.h:401
Pure abstract class for unstructured LDPC matrices.
Definition: ldpc.h:276
virtual void display_stats() const =0
Display some information about the matrix.
LDPC parity check matrix generic class.
Definition: ldpc.h:72
GF2mat_sparse Ht
The transposed parity check matrix.
Definition: ldpc.h:176
virtual void display_stats() const
Display some information about the matrix.
Definition: ldpc.cpp:109
Sparse_Vec< bin > get_col(int c) const
Get a specific column from the matrix.
Definition: ldpc.h:109
bin operator()(int i, int j) const
Get element (i,j) of the parity check matrix.
Definition: ldpc.h:142
GF2mat_sparse get_H(bool transpose=false) const
Get the parity check matrix, optionally its transposed form.
Definition: ldpc.h:104
LDPC_Parity()
Default constructor.
Definition: ldpc.h:76
int get_nvar() const
Get the number of variable nodes (number of columns)
Definition: ldpc.h:115
ivec sumX2
Actual number of ones in each row.
Definition: ldpc.h:184
bool init_flag
Flag that indicates proper initialization.
Definition: ldpc.h:170
int nvar
Number of variable nodes.
Definition: ldpc.h:178
int ncheck
Number of check nodes.
Definition: ldpc.h:180
double get_rate() const
Get the code rate.
Definition: ldpc.h:152
bin get(int i, int j) const
Get element (i,j) of the parity check matrix.
Definition: ldpc.h:136
int get_ncheck() const
Get the number of check nodes (number of rows)
Definition: ldpc.h:124
Sparse_Vec< bin > get_row(int r) const
Get a specific row from the matrix.
Definition: ldpc.h:112
virtual ~LDPC_Parity()
Virtual destructor.
Definition: ldpc.h:98
ivec sumX1
Actual number of ones in each column.
Definition: ldpc.h:182
GF2mat_sparse H
The parity check matrix.
Definition: ldpc.h:174
Log-likelihood algebra calculation unit.
Definition: llr.h:125
Binary arithmetic (boolean) class.
Definition: binary.h:57
Definition of a class for algebra on GF(2) (binary) matrices.
#define it_error(s)
Abort unconditionally.
Definition: itassert.h:126
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
Definition: matfunc.h:308
Class for numerically efficient log-likelihood algebra.
itpp namespace
Definition: itmex.h:37
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition: binary.cpp:36
STL namespace.
Definition of classes for random number generators.
Sorting functions.
SourceForge Logo

Generated on Sun Jun 5 2022 21:26:42 for IT++ by Doxygen 1.9.3