IT++ Logo
hammcode.cpp
Go to the documentation of this file.
1 
29 #include <itpp/comm/hammcode.h>
30 #include <itpp/base/math/log_exp.h>
31 #include <itpp/base/converters.h>
32 
33 
34 namespace itpp
35 {
36 
38 {
39  n = pow2i(m) - 1;
40  k = pow2i(m) - m - 1;
41  H.set_size(n - k, n);
42  G.set_size(k, n);
43  generate_H(); // generate_H must be run before generate_G
44  generate_G();
45 }
46 
47 void Hamming_Code::generate_H(void)
48 {
49  int i, j, NextPos;
50  char NotUsed;
51  bvec temp;
52  ivec indexes(n);
53  indexes.zeros();
54 
55  for (i = 1; i <= n - k; i++) { indexes(i - 1) = pow2i(n - k - i); }
56  NextPos = n - k;
57  for (i = 1; i <= n; i++) {
58  NotUsed = 1;
59  for (j = 0; j < n; j++)
60  if (i == indexes(j)) { NotUsed = 0; }
61  if (NotUsed) { indexes(NextPos) = i; NextPos = NextPos + 1; }
62  }
63 
64  for (i = 0; i < n; i++) {
65  temp = dec2bin(n - k, indexes(i)); //<-CHECK THIS OUT!!!!
66  for (j = 0; j < (n - k); j++) {
67  H(j, i) = temp(j);
68  }
69  }
70 }
71 
72 void Hamming_Code::generate_G(void)
73 {
74  int i, j;
75  for (i = 0; i < k; i++) {
76  for (j = 0; j < n - k; j++)
77  G(i, j) = H(j, i + n - k);
78  }
79 
80  for (i = 0; i < k; i++) {
81  for (j = n - k; j < n; j++)
82  G(i, j) = 0;
83  }
84 
85  for (i = 0; i < k; i++)
86  G(i, i + n - k) = 1;
87 }
88 
89 void Hamming_Code::encode(const bvec &uncoded_bits, bvec &coded_bits)
90 {
91  int length = uncoded_bits.length();
92  int Itterations = floor_i(static_cast<double>(length) / k);
93  bmat Gt = G.T();
94  int i;
95 
96  coded_bits.set_size(Itterations * n, false);
97  //Code all codewords
98  for (i = 0; i < Itterations; i++)
99  coded_bits.replace_mid(n*i, Gt * uncoded_bits.mid(i*k, k));
100 }
101 
102 bvec Hamming_Code::encode(const bvec &uncoded_bits)
103 {
104  bvec coded_bits;
105  encode(uncoded_bits, coded_bits);
106  return coded_bits;
107 }
108 
109 void Hamming_Code::decode(const bvec &coded_bits, bvec &decoded_bits)
110 {
111  int length = coded_bits.length();
112  int Itterations = floor_i(static_cast<double>(length) / n);
113  ivec Hindexes(n);
114  bvec temp(n - k);
115  bvec coded(n), syndrome(n - k);
116  int isynd, errorpos = 0;
117  int i, j;
118 
119  decoded_bits.set_size(Itterations*k, false);
120 
121  for (i = 0; i < n; i++) {
122  for (j = 0; j < n - k; j++)
123  temp(j) = H(j, i);
124  Hindexes(i) = bin2dec(temp);
125  }
126 
127  //Decode all codewords
128  for (i = 0; i < Itterations; i++) {
129  coded = coded_bits.mid(i * n, n);
130  syndrome = H * coded;
131  isynd = bin2dec(syndrome);
132  if (isynd != 0) {
133  for (j = 0; j < n; j++)
134  if (Hindexes(j) == isynd) { errorpos = j; };
135  coded(errorpos) += 1;
136  }
137  decoded_bits.replace_mid(k*i, coded.right(k));
138  }
139 }
140 
141 bvec Hamming_Code::decode(const bvec &coded_bits)
142 {
143  bvec decoded_bits;
144  decode(coded_bits, decoded_bits);
145  return decoded_bits;
146 }
147 
148 
149 // -------------- Soft-decision decoding is not implemented ----------------
150 void Hamming_Code::decode(const vec &, bvec &)
151 {
152  it_error("Hamming_Code::decode(vec, bvec); soft-decision decoding is not implemented");
153 }
154 
155 bvec Hamming_Code::decode(const vec &)
156 {
157  it_error("Hamming_Code::decode(vec, bvec); soft-decision decoding is not implemented");
158  return bvec();
159 }
160 
161 
162 } // namespace itpp
SourceForge Logo

Generated on Sat May 25 2013 16:32:20 for IT++ by Doxygen 1.8.2