From f8b782033d0b07a78f380e37f55cef808ff5857b Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Wed, 10 Jun 2020 18:32:52 +0200 Subject: [PATCH] print histogram of distances and verify T --- tests/short_bch_code_encoder_test.cc | 50 +++++++++++++++++++--------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/tests/short_bch_code_encoder_test.cc b/tests/short_bch_code_encoder_test.cc index b6b417a..7a87a9a 100644 --- a/tests/short_bch_code_encoder_test.cc +++ b/tests/short_bch_code_encoder_test.cc @@ -8,31 +8,51 @@ Copyright 2020 Ahmet Inan #include #include "short_bch_code_encoder.hh" +template +int popcnt(TYPE x) +{ + int cnt = 0; + while (x) { + ++cnt; + x &= x-1; + } + return cnt; +} + +template +void bch_test(int message, int target) +{ + CODE::ShortBCHCodeEncoder encode(POLY); + int hist[N+1] = { 0 }; + for (int j = 0; j < (1 << K); ++j) + for (int i = j + 1; i < (1 << K); ++i) + ++hist[popcnt(encode(i)^encode(j))]; + std::cout << "BCH(" << N << ", " << K << ") T=" << T << " hist:"; + for (int i = 0; i < N + 1; ++i) + if (hist[i]) + std::cout << " " << i << ":" << hist[i]; + std::cout << std::endl; + int d = 0; + while (!hist[d]) + ++d; + assert((d-1)/2 == T); + int codeword = encode(message); + assert(target == codeword); +} + int main() { if (1) { // Perfect binary Golay code using x^11+x^9+x^7+x^6+x^5+x+1 - CODE::ShortBCHCodeEncoder<23, 12> encode(0b101011100011); - int message = 0b101011011011; - int target = 0b10101101101111011111001; - int codeword = encode(message); - assert(target == codeword); + bch_test<23, 12, 3, 0b101011100011>(0b101011011011, 0b10101101101111011111001); } if (1) { // Perfect binary Golay code using x^11+x^10+x^6+x^5+x^4+x^2+1 - CODE::ShortBCHCodeEncoder<23, 12> encode(0b110001110101); - int message = 0b101011011011; - int target = 0b10101101101100100010101; - int codeword = encode(message); - assert(target == codeword); + bch_test<23, 12, 3, 0b110001110101>(0b101011011011, 0b10101101101100100010101); } if (1) { // NASA INTRO BCH(15, 5) T=3 - CODE::ShortBCHCodeEncoder<15, 5> encode(0b10100110111); - int message = 0b11001; - int target = 0b110010001111010; - int codeword = encode(message); - assert(target == codeword); + bch_test<15, 5, 3, 0b10100110111>(0b11001, 0b110010001111010); } std::cerr << "Short BCH code encoder test passed!" << std::endl; return 0;