convcode.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "utils.hh"
  2. #include "convcode.hh"
  3. #include <array>
  4. #include <algorithm>
  5. #include <assert.h>
  6. using std::vector;
  7. using std::string;
  8. using std::min;
  9. int
  10. parity (unsigned int v)
  11. {
  12. int p = 0;
  13. while (v)
  14. {
  15. p ^= (v & 1);
  16. v >>= 1;
  17. }
  18. return p;
  19. }
  20. constexpr auto ab_generators = std::array<unsigned,12>
  21. {
  22. 066561, 075211, 071545, 054435, 063635, 052475,
  23. 063543, 075307, 052547, 045627, 067657, 051757
  24. };
  25. constexpr unsigned int ab_rate = ab_generators.size();
  26. constexpr unsigned int order = 15;
  27. /*
  28. constexpr unsigned int order = 9;
  29. constexpr auto generators = std::array<unsigned,3> { 0557, 0663, 0711 };
  30. constexpr unsigned int order = 14;
  31. constexpr auto generators = std::array<unsigned,3> { 021645, 035661, 037133 };
  32. constexpr unsigned int order = 18;
  33. constexpr auto generators = std::array<unsigned,3> { 0552137, 0614671, 0772233 };
  34. */
  35. constexpr unsigned int state_count = (1 << order);
  36. constexpr unsigned int state_mask = (1 << order) - 1;
  37. size_t
  38. conv_code_size (ConvBlockType block_type, size_t msg_size)
  39. {
  40. switch (block_type)
  41. {
  42. case ConvBlockType::a:
  43. case ConvBlockType::b: return (msg_size + order) * ab_rate / 2;
  44. case ConvBlockType::ab: return (msg_size + order) * ab_rate;
  45. default: assert (false);
  46. }
  47. }
  48. vector<unsigned>
  49. get_block_type_generators (ConvBlockType block_type)
  50. {
  51. vector<unsigned> generators;
  52. if (block_type == ConvBlockType::a)
  53. {
  54. for (unsigned int i = 0; i < ab_rate / 2; i++)
  55. generators.push_back (ab_generators[i * 2]);
  56. }
  57. else if (block_type == ConvBlockType::b)
  58. {
  59. for (unsigned int i = 0; i < ab_rate / 2; i++)
  60. generators.push_back (ab_generators[i * 2 + 1]);
  61. }
  62. else
  63. {
  64. assert (block_type == ConvBlockType::ab);
  65. generators.assign (ab_generators.begin(), ab_generators.end());
  66. }
  67. return generators;
  68. }
  69. vector<int>
  70. conv_encode (ConvBlockType block_type, const vector<int>& in_bits)
  71. {
  72. auto generators = get_block_type_generators (block_type);
  73. vector<int> out_vec;
  74. vector<int> vec = in_bits;
  75. /* termination: bring encoder into all-zero state */
  76. for (unsigned int i = 0; i < order; i++)
  77. vec.push_back (0);
  78. unsigned int reg = 0;
  79. for (auto b : vec)
  80. {
  81. reg = (reg << 1) | b;
  82. for (auto poly : generators)
  83. {
  84. int out_bit = parity (reg & poly);
  85. out_vec.push_back (out_bit);
  86. }
  87. }
  88. return out_vec;
  89. }
  90. /* decode using viterbi algorithm */
  91. vector<int>
  92. conv_decode_soft (ConvBlockType block_type, const vector<float>& coded_bits, float *error_out)
  93. {
  94. auto generators = get_block_type_generators (block_type);
  95. unsigned int rate = generators.size();
  96. vector<int> decoded_bits;
  97. assert (coded_bits.size() % rate == 0);
  98. struct StateEntry
  99. {
  100. int last_state;
  101. float delta;
  102. int bit;
  103. };
  104. vector<vector<StateEntry>> error_count;
  105. for (size_t i = 0; i < coded_bits.size() + rate; i += rate) /* 1 extra element */
  106. error_count.emplace_back (state_count, StateEntry {0, -1, 0});
  107. error_count[0][0].delta = 0; /* start state */
  108. /* precompute state -> output bits table */
  109. vector<float> state2bits;
  110. for (unsigned int state = 0; state < state_count; state++)
  111. {
  112. for (size_t p = 0; p < generators.size(); p++)
  113. {
  114. int out_bit = parity (state & generators[p]);
  115. state2bits.push_back (out_bit);
  116. }
  117. }
  118. for (size_t i = 0; i < coded_bits.size(); i += rate)
  119. {
  120. vector<StateEntry>& old_table = error_count[i / rate];
  121. vector<StateEntry>& new_table = error_count[i / rate + 1];
  122. for (unsigned int state = 0; state < state_count; state++)
  123. {
  124. /* this check enforces that we only consider states reachable from state=0 at time=0*/
  125. if (old_table[state].delta >= 0)
  126. {
  127. for (int bit = 0; bit < 2; bit++)
  128. {
  129. int new_state = ((state << 1) | bit) & state_mask;
  130. float delta = old_table[state].delta;
  131. int sbit_pos = new_state * rate;
  132. for (size_t p = 0; p < rate; p++)
  133. {
  134. const float cbit = coded_bits[i + p];
  135. const float sbit = state2bits[sbit_pos + p];
  136. /* decoding error weight for this bit; if input is only 0.0 and 1.0, this is the hamming distance */
  137. delta += (cbit - sbit) * (cbit - sbit);
  138. }
  139. if (delta < new_table[new_state].delta || new_table[new_state].delta < 0) /* better match with this link? replace entry */
  140. {
  141. new_table[new_state].delta = delta;
  142. new_table[new_state].last_state = state;
  143. new_table[new_state].bit = bit;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. unsigned int state = 0;
  150. if (error_out)
  151. *error_out = error_count.back()[state].delta / coded_bits.size();
  152. for (size_t idx = error_count.size() - 1; idx > 0; idx--)
  153. {
  154. decoded_bits.push_back (error_count[idx][state].bit);
  155. state = error_count[idx][state].last_state;
  156. }
  157. std::reverse (decoded_bits.begin(), decoded_bits.end());
  158. /* remove termination */
  159. assert (decoded_bits.size() >= order);
  160. decoded_bits.resize (decoded_bits.size() - order);
  161. return decoded_bits;
  162. }
  163. vector<int>
  164. conv_decode_hard (ConvBlockType block_type, const vector<int>& coded_bits)
  165. {
  166. /* for the final application, we always want soft decoding, so we don't
  167. * special case hard decoding here, so this will be a little slower than
  168. * necessary
  169. */
  170. vector<float> soft_bits;
  171. for (auto b : coded_bits)
  172. soft_bits.push_back (b ? 1.0f : 0.0f);
  173. return conv_decode_soft (block_type, soft_bits);
  174. }
  175. void
  176. conv_print_table (ConvBlockType block_type)
  177. {
  178. vector<int> bits (100);
  179. bits[0] = 1;
  180. vector<int> out_bits = conv_encode (block_type, bits);
  181. auto generators = get_block_type_generators (block_type);
  182. unsigned int rate = generators.size();
  183. for (unsigned int r = 0; r < rate; r++)
  184. {
  185. for (unsigned int i = 0; i < order; i++)
  186. printf ("%s%d", i == 0 ? "" : " ", out_bits[i * rate + r]);
  187. printf ("\n");
  188. }
  189. }