IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
39 namespace itpp
40 {
41 
42 // ---------------------------------------------------------------------------
43 // LDPC_Parity
44 // ---------------------------------------------------------------------------
45 
71 {
72  friend class LDPC_Code;
73 public:
75  LDPC_Parity(): init_flag(false) {}
76 
78  LDPC_Parity(int ncheck, int nvar);
79 
91  LDPC_Parity(const std::string& filename, const std::string& format);
92 
94  LDPC_Parity(const GF2mat_sparse_alist& alist);
95 
97  virtual ~LDPC_Parity() {}
98 
100  void initialize(int ncheck, int nvar);
101 
103  GF2mat_sparse get_H(bool transpose = false) const {
104  return (transpose ? Ht : H);
105  }
106 
108  Sparse_Vec<bin> get_col(int c) const { return H.get_col(c); }
109 
111  Sparse_Vec<bin> get_row(int r) const { return Ht.get_col(r); }
112 
114  int get_nvar() const {
115  it_assert_debug(H.cols() == nvar,
116  "LDPC_Parity::get_nvar(): Internal error");
118  "LDPC_Parity::get_nvar(): Internal error");
119  return nvar;
120  }
121 
123  int get_ncheck() const {
125  "LDPC_Parity::get_ncheck(): Internal error");
127  "LDPC_Parity::get_ncheck(): Internal error");
128  return ncheck;
129  }
130 
132  void set(int i, int j, bin value);
133 
135  bin get(int i, int j) const {
136  it_assert_debug(H(i, j) == Ht(j, i), "LDPC_Parity::get(): Internal error");
137  return H(i, j);
138  }
139 
141  bin operator()(int i, int j) const {
142  it_assert_debug(H(i, j) == Ht(j, i),
143  "LDPC_Parity::operator(): Internal error");
144  return H(i, j);
145  }
146 
148  virtual void display_stats() const;
149 
151  double get_rate() const {
152  return (1.0 - static_cast<double>(ncheck) / nvar);
153  }
154 
156  void import_alist(const GF2mat_sparse_alist& H_alist);
157 
160 
162  void load_alist(const std::string& alist_file);
163 
165  void save_alist(const std::string& alist_file) const;
166 
167 protected:
169  bool init_flag;
171  static const int Nmax = 200;
177  int nvar;
179  int ncheck;
181  ivec sumX1;
183  ivec sumX2;
184 
200  int check_for_cycles(int L) const;
201 
245  int check_connectivity(int from_m, int from_n, int to_m, int to_n,
246  int g, int L) const;
247 
248  // inline int get_cmax() const { return (max(sumX1)); }
249  // inline int get_vmax() const { return (max(sumX2)); }
250  // ivec get_coldegree() const;
251  // ivec get_rowdegree() const;
252 };
253 
254 
255 // ----------------------------------------------------------------------
256 // LDPC_Parity_Unstructured
257 // ----------------------------------------------------------------------
258 
275 {
276 public:
278  virtual void display_stats() const = 0;
279 
298  int cycle_removal_MGW(int L);
299 
300 protected:
302  void generate_random_H(const ivec& C, const ivec& R, const ivec& cycopt);
303 
317  void compute_CR(const vec& var_deg, const vec& chk_deg, const int Nvar,
318  ivec &C, ivec &R);
319 
320 };
321 
322 
323 // ----------------------------------------------------------------------
324 // LDPC_Parity_Irregular
325 // ----------------------------------------------------------------------
326 
332 {
333 public:
337  LDPC_Parity_Irregular(int Nvar, const vec& var_deg, const vec& chk_deg,
338  const std::string& method = "rand",
339  const ivec& options = "200 6");
340 
379  void generate(int Nvar, const vec& var_deg, const vec& chk_deg,
380  const std::string& method = "rand",
381  const ivec& options = "200 6");
382 
385 };
386 
387 
388 // ----------------------------------------------------------------------
389 // LDPC_Parity_Regular
390 // ----------------------------------------------------------------------
391 
397 {
398 public:
402  LDPC_Parity_Regular(int Nvar, int k, int l,
403  const std::string& method = "rand",
404  const ivec& options = "200 6");
405 
424  void generate(int Nvar, int k, int l,
425  const std::string& method = "rand",
426  const ivec& options = "200 6");
427 
430 };
431 
432 // ----------------------------------------------------------------------
433 // BLDPC_Parity
434 // ----------------------------------------------------------------------
435 
460 class BLDPC_Parity : public LDPC_Parity
461 {
462 public:
464  BLDPC_Parity(): LDPC_Parity(), Z(0), H_b(), H_b_valid(false) {}
465 
467  BLDPC_Parity(const imat &base_matrix, int exp_factor);
468 
470  BLDPC_Parity(const std::string &filename, int exp_factor);
471 
473  void expand_base(const imat &base_matrix, int exp_factor);
474 
476  int get_exp_factor() const;
477 
479  imat get_base_matrix() const;
480 
482  bool is_valid() const { return H_b_valid && init_flag; }
483 
485  void set_exp_factor(int exp_factor);
486 
488  void load_base_matrix(const std::string &filename);
489 
491  void save_base_matrix(const std::string &filename) const;
492 
493 private:
494  int Z;
495  imat H_b;
496  bool H_b_valid;
497 
499  void calculate_base_matrix();
500 };
501 
502 
503 // ----------------------------------------------------------------------
504 // LDPC_Generator
505 // ----------------------------------------------------------------------
506 
523 {
524  friend class LDPC_Code;
525 public:
527  LDPC_Generator(const std::string& type_in = ""): init_flag(false),
528  type(type_in) {}
530  virtual ~LDPC_Generator() {}
531 
533  virtual void encode(const bvec &input, bvec &output) = 0;
534 
536  std::string get_type() const { return type; }
537 
538 protected:
539  bool init_flag;
540  std::string type;
541 
543  virtual void save(const std::string& filename) const = 0;
545  virtual void load(const std::string& filename) = 0;
546 };
547 
548 
549 // ----------------------------------------------------------------------
550 // LDPC_Generator_Systematic
551 // ----------------------------------------------------------------------
552 
565 {
566 public:
568  LDPC_Generator_Systematic(): LDPC_Generator("systematic"), G() {}
571  bool natural_ordering = false,
572  const ivec& ind = "");
573 
576 
578  virtual void encode(const bvec &input, bvec &output);
579 
613  ivec construct(LDPC_Parity* const H, bool natural_ordering = false,
614  const ivec& ind = "");
615 
616 protected:
618  virtual void save(const std::string& filename) const;
620  virtual void load(const std::string& filename);
621 
622 private:
623  GF2mat G; // the matrix is stored in transposed form
624 };
625 
626 
627 // ----------------------------------------------------------------------
628 // BLDPC_Generator
629 // ----------------------------------------------------------------------
630 
639 {
640 public:
642  BLDPC_Generator(const std::string type = "BLDPC"):
643  LDPC_Generator(type), H_enc(), N(0), M(0), K(0), Z(0) {}
645  BLDPC_Generator(const BLDPC_Parity* const H,
646  const std::string type = "BLDPC");
647 
649  int get_exp_factor() const { return Z; }
650 
652  void encode(const bvec &input, bvec &output);
653 
655  void construct(const BLDPC_Parity* const H);
656 
657 protected:
659  void save(const std::string &filename) const;
661  void load(const std::string &filename);
662 
664  int N;
665  int M;
666  int K;
667  int Z;
668 };
669 
670 
671 // ----------------------------------------------------------------------
672 // LDPC_Code
673 // ----------------------------------------------------------------------
674 
725 class LDPC_Code : public Channel_Code
726 {
727 public:
729  LDPC_Code();
730 
737  LDPC_Code(const LDPC_Parity* const H, LDPC_Generator* const G = 0);
738 
744  LDPC_Code(const std::string& filename, LDPC_Generator* const G = 0);
745 
747  virtual ~LDPC_Code() {}
748 
749 
757  void set_code(const LDPC_Parity* const H, LDPC_Generator* const G = 0);
758 
772  void load_code(const std::string& filename, LDPC_Generator* const G = 0);
773 
782  void save_code(const std::string& filename) const;
783 
784 
793  void set_decoding_method(const std::string& method);
794 
809  bool syndr_check_each_iter = true,
810  bool syndr_check_at_start = false);
811 
813  void set_llrcalc(const LLR_calc_unit& llrcalc);
814 
815 
816  // ------------ Encoding ---------------------
817 
827  virtual void encode(const bvec &input, bvec &output);
829  virtual bvec encode(const bvec &input);
830 
831 
832  // ------------ Decoding ---------------------
833 
835  virtual void decode(const bvec &, bvec &) {
836  it_error("LDPC_Code::decode(): Hard input decoding not implemented");
837  }
839  virtual bvec decode(const bvec &) {
840  it_error("LDPC_Code::decode(): Hard input decoding not implemented");
841  return bvec();
842  }
843 
845  virtual void decode(const vec &llr_in, bvec &syst_bits);
847  virtual bvec decode(const vec &llr_in);
848 
850  void decode_soft_out(const vec &llr_in, vec &llr_out);
852  vec decode_soft_out(const vec &llr_in);
853 
876  int bp_decode(const QLLRvec &LLRin, QLLRvec &LLRout);
877 
886  bool syndrome_check(const QLLRvec &LLR) const;
887 
889  bool syndrome_check(const bvec &b) const;
890 
891  // ------------ Basic information gathering functions ------
892 
894  double get_rate() const {
895  return (1.0 - static_cast<double>(ncheck) / nvar);
896  }
897 
899  int get_nvar() const { return nvar; }
900 
902  int get_ncheck() const { return ncheck; }
903 
905  int get_ninfo() const { return nvar - ncheck; }
906 
908  std::string get_decoding_method() const { return dec_method; }
909 
911  int get_nrof_iterations() const { return max_iters; }
912 
914  LLR_calc_unit get_llrcalc() const { return llrcalc; }
915 
917  friend std::ostream &operator<<(std::ostream &os, const LDPC_Code &C);
918 
919 protected:
920  bool H_defined;
921  bool G_defined;
922  int nvar;
923  int ncheck;
925 
926  // decoder parameters
927  std::string dec_method;
928  int max_iters;
929  bool psc;
930  bool pisc;
932 
934  void decoder_parameterization(const LDPC_Parity* const H);
935 
937  void integrity_check();
938 
940  void setup_decoder();
941 
942 private:
943  // Parity check matrix parameterization
944  ivec C, V, sumX1, sumX2, iind, jind;
945 
946  // temporary storage for decoder (memory allocated when codec defined)
947  QLLRvec mvc, mcv;
948 
950  static const int max_cnd = 200;
951 };
952 
953 
958 std::ostream &operator<<(std::ostream &os, const LDPC_Code &C);
959 }
960 
961 #endif
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:13 for IT++ by Doxygen 1.8.1.2