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>
37 #include <itpp/comm/channel_code.h>
38 #include <itpp/itexports.h>
39 
40 namespace itpp
41 {
42 
43 // ---------------------------------------------------------------------------
44 // LDPC_Parity
45 // ---------------------------------------------------------------------------
46 
71 class ITPP_EXPORT LDPC_Parity
72 {
73  friend class LDPC_Code;
74 public:
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 
168 protected:
170  bool init_flag;
172  static const int Nmax = 200;
178  int nvar;
180  int ncheck;
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 
275 class ITPP_EXPORT LDPC_Parity_Unstructured : public LDPC_Parity
276 {
277 public:
279  virtual void display_stats() const = 0;
280 
299  int cycle_removal_MGW(int L);
300 
301 protected:
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 {
334 public:
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 {
399 public:
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 
461 class ITPP_EXPORT BLDPC_Parity : public LDPC_Parity
462 {
463 public:
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 
494 private:
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 
523 class ITPP_EXPORT LDPC_Generator
524 {
525  friend class LDPC_Code;
526 public:
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;};
544 private:
545  bool init_flag;
546  std::string* type;
547 protected:
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 
570 class ITPP_EXPORT LDPC_Generator_Systematic : public LDPC_Generator
571 {
572 public:
574  LDPC_Generator_Systematic(): LDPC_Generator("systematic"), G() {}
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 
622 protected:
624  virtual void save(const std::string& filename) const;
626  virtual void load(const std::string& filename);
627 
628 private:
629  GF2mat G; // the matrix is stored in transposed form
630 };
631 
632 
633 // ----------------------------------------------------------------------
634 // BLDPC_Generator
635 // ----------------------------------------------------------------------
636 
644 class ITPP_EXPORT BLDPC_Generator : public LDPC_Generator
645 {
646 public:
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 
663 protected:
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 
731 class ITPP_EXPORT LDPC_Code : public Channel_Code
732 {
733 public:
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 
942 protected:
944  void decoder_parameterization(const LDPC_Parity* const H);
945 
947  void integrity_check();
948 
950  void setup_decoder();
951 
952 private:
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 
980 ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const LDPC_Code &C);
981 }
982 
983 #endif
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:22 for IT++ by Doxygen 1.8.2