Commit 619d2fec authored by Stefan Westerfeld's avatar Stefan Westerfeld

Split decoding functions into soft and hard decoding.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 33852ca9
...@@ -61,8 +61,9 @@ conv_encode (const vector<int>& in_bits) ...@@ -61,8 +61,9 @@ conv_encode (const vector<int>& in_bits)
return out_vec; return out_vec;
} }
/* decode using viterbi algorithm */
vector<int> vector<int>
conv_decode (const vector<int>& coded_bits) conv_decode_soft (const vector<float>& coded_bits)
{ {
vector<int> decoded_bits; vector<int> decoded_bits;
...@@ -70,9 +71,9 @@ conv_decode (const vector<int>& coded_bits) ...@@ -70,9 +71,9 @@ conv_decode (const vector<int>& coded_bits)
struct StateEntry struct StateEntry
{ {
int last_state; int last_state;
int delta; float delta;
int bit; int bit;
}; };
vector<vector<StateEntry>> error_count; vector<vector<StateEntry>> error_count;
for (size_t i = 0; i < coded_bits.size() + rate; i += rate) /* 1 extra element */ for (size_t i = 0; i < coded_bits.size() + rate; i += rate) /* 1 extra element */
...@@ -108,9 +109,14 @@ conv_decode (const vector<int>& coded_bits) ...@@ -108,9 +109,14 @@ conv_decode (const vector<int>& coded_bits)
int delta = old_table[state].delta; int delta = old_table[state].delta;
int sbit_pos = new_state * rate; int sbit_pos = new_state * rate;
/* hamming distance between produced bits and coded bits */
for (size_t p = 0; p < generators.size(); p++) for (size_t p = 0; p < generators.size(); p++)
delta += state2bits[sbit_pos + p] ^ coded_bits[i + p]; {
const float cbit = coded_bits[i + p];
const float sbit = state2bits[sbit_pos + p];
/* decoding error weight for this bit; if input is only 0.0 and 1.0, this is the hamming distance */
delta += (cbit - sbit) * (cbit - sbit);
}
if (delta < new_table[new_state].delta || new_table[new_state].delta < 0) /* better match with this link? replace entry */ if (delta < new_table[new_state].delta || new_table[new_state].delta < 0) /* better match with this link? replace entry */
{ {
...@@ -138,3 +144,17 @@ conv_decode (const vector<int>& coded_bits) ...@@ -138,3 +144,17 @@ conv_decode (const vector<int>& coded_bits)
return decoded_bits; return decoded_bits;
} }
vector<int>
conv_decode_hard (const vector<int>& coded_bits)
{
/* for the final application, we always want soft decoding, so we don't
* special case hard decoding here, so this will be a little slower than
* necessary
*/
vector<float> soft_bits;
for (auto b : coded_bits)
soft_bits.push_back (b ? 1.0f : 0.0f);
return conv_decode_soft (soft_bits);
}
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <string> #include <string>
std::vector<int> conv_encode (const std::vector<int>& in_bits); std::vector<int> conv_encode (const std::vector<int>& in_bits);
std::vector<int> conv_decode (const std::vector<int>& coded_bits); std::vector<int> conv_decode_hard (const std::vector<int>& coded_bits);
std::vector<int> conv_decode_soft (const std::vector<float>& coded_bits);
#endif /* AUDIOWMARK_CONV_CODE_HH */ #endif /* AUDIOWMARK_CONV_CODE_HH */
...@@ -41,7 +41,7 @@ main (int argc, char **argv) ...@@ -41,7 +41,7 @@ main (int argc, char **argv)
printf ("\n"); printf ("\n");
printf ("coded hex: %s\n", bit_vec_to_str (coded_bits).c_str()); printf ("coded hex: %s\n", bit_vec_to_str (coded_bits).c_str());
vector<int> decoded_bits = conv_decode (coded_bits); vector<int> decoded_bits = conv_decode_hard (coded_bits);
printf ("output vector (k=%zd): ", decoded_bits.size()); printf ("output vector (k=%zd): ", decoded_bits.size());
for (auto b : decoded_bits) for (auto b : decoded_bits)
printf ("%d", b); printf ("%d", b);
...@@ -75,7 +75,7 @@ main (int argc, char **argv) ...@@ -75,7 +75,7 @@ main (int argc, char **argv)
for (size_t pos = 0; pos < coded_bits.size(); pos++) for (size_t pos = 0; pos < coded_bits.size(); pos++)
coded_bits[pos] ^= error_bits[pos]; coded_bits[pos] ^= error_bits[pos];
vector<int> decoded_bits = conv_decode (coded_bits); vector<int> decoded_bits = conv_decode_hard (coded_bits);
assert (decoded_bits.size() == 128); assert (decoded_bits.size() == 128);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment