added metric() helper

This commit is contained in:
Ahmet Inan 2020-06-09 13:33:42 +02:00
commit a33c2082d9

View file

@ -26,6 +26,14 @@ class ShortBCHCodeDecoder
} }
return inp; return inp;
} }
static int metric(const int8_t *soft, int hard)
{
int lol = 8 * sizeof(hard) - 1;
int sum = 0;
for (int i = 0; i < N; ++i)
sum += ((hard << (lol-i)) >> lol) * soft[i];
return sum;
}
public: public:
ShortBCHCodeDecoder(int generator, int T) ShortBCHCodeDecoder(int generator, int T)
{ {
@ -56,13 +64,10 @@ public:
if (0) { if (0) {
int word = 0, best = 0; int word = 0, best = 0;
for (int msg = 0; msg < W; ++msg) { for (int msg = 0; msg < W; ++msg) {
int sum = 0;
int enc = (msg << P) | par[msg]; int enc = (msg << P) | par[msg];
int lol = 8 * sizeof(enc) - 1; int met = metric(code, enc);
for (int i = 0; i < N; ++i) if (met > best) {
sum += ((enc << (lol-i)) >> lol) * code[i]; best = met;
if (sum > best) {
best = sum;
word = enc; word = enc;
} }
} }
@ -75,26 +80,16 @@ public:
// hard decision // hard decision
if (0) if (0)
return word; return word;
int best = 0;
// metric of hard decision // metric of hard decision
if (1) { int best = metric(code, word);
int lol = 8 * sizeof(word) - 1;
int sum = 0;
for (int i = 0; i < N; ++i)
sum += ((word << (lol-i)) >> lol) * code[i];
best = sum;
}
// flip each bit and see .. // flip each bit and see ..
if (0) { if (0) {
for (int j = 0; j < N; ++j) { for (int j = 0; j < N; ++j) {
int tmp = 1 << j; int tmp = 1 << j;
int dec = (*this)(cw ^ tmp); int dec = (*this)(cw ^ tmp);
int lol = 8 * sizeof(dec) - 1; int met = metric(code, dec);
int sum = 0; if (met > best) {
for (int i = 0; i < N; ++i) best = met;
sum += ((dec << (lol-i)) >> lol) * code[i];
if (sum > best) {
best = sum;
word = dec; word = dec;
} }
} }
@ -125,12 +120,9 @@ public:
for (int i = 0; i < num; ++i) for (int i = 0; i < num; ++i)
tmp |= ((j>>i)&1) << worst[i]; tmp |= ((j>>i)&1) << worst[i];
int dec = (*this)(cw ^ tmp); int dec = (*this)(cw ^ tmp);
int lol = 8 * sizeof(dec) - 1; int met = metric(code, dec);
int sum = 0; if (met > best) {
for (int i = 0; i < N; ++i) best = met;
sum += ((dec << (lol-i)) >> lol) * code[i];
if (sum > best) {
best = sum;
word = dec; word = dec;
} }
} }