File: bch_test.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (115 lines) | stat: -rw-r--r-- 4,049 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*!
 * \file
 * \brief BCH encoder/decoder class test program
 * \author Pal Frenger, Steve Peters and Adam Piatyszek
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/itcomm.h>

using namespace itpp;
using namespace std;


bvec set_errors(const bvec &input, const ivec errpos)
{
  bvec output = input;
  for (int i = 0; i < errpos.length(); i++)
    output(errpos(i)) ^= 1;
  return output ;
}


int main()
{
  cout << "===================================" << endl;
  cout << "    Test of BCH encoder/decoder    " << endl;
  cout << "===================================" << endl;

  {
    BCH bch(31, 21, 2, "3 5 5 1");

    bvec input = randb(21);
    bvec encoded = bch.encode(input);
    bvec err = set_errors(encoded, (ivec) "1 2"); // error positions
    bvec decoded = bch.decode(err);

    cout << "A two error case (should be corrected)" << endl;
    cout << "input =   " << input << endl ;
    cout << "encoded = " << encoded << endl ;
    cout << "err =     " << err <<  endl ;
    cout << "decoded = " << decoded << endl ;

    input = randb(21);
    encoded = bch.encode(input);
    err = set_errors(encoded, (ivec) "1 2 27"); // error positions
    decoded = bch.decode(err);

    cout << "A three error case (will cause decoding errors)" << endl;
    cout << "input =   " << input << endl;
    cout << "encoded = " << encoded << endl;
    cout << "err =     " << err <<  endl;
    cout << "decoded = " << decoded << endl << endl;
  }

  cout << "========================================" << endl;
  cout << "   Systematic vs. non-systematic test   " << endl;
  cout << "========================================" << endl;

  {
    bmat u = "0 0 0 0; 0 0 0 1; 0 0 1 0; 0 0 1 1; 0 1 0 0; 0 1 0 1; 0 1 1 0";
    bmat c(u.rows(), 7);
    bmat y(u.rows(), 7);
    bmat decoded(u.rows(), u.cols());
    BCH bch_nsys(7, 4, 1, "1 3");
    BCH bch_sys(7, 4, 1, "1 3", true);

    bmat f = "1 0 0 0 0 0 0; 0 1 0 0 0 0 0; 0 0 1 0 0 0 0; 0 0 0 1 0 0 0; 0 0 0 0 1 0 0; 0 0 0 0 0 1 0; 0 0 0 0 0 0 1";

    cout << "Non-systematic case" << endl;
    cout << "-------------------" << endl;
    for (int i = 0; i < u.rows(); i++) {
      c.set_row(i, bch_nsys.encode(u.get_row(i)));
      cout << "Encoded " << u.get_row(i) << " to " << c.get_row(i) << endl;
      y.set_row(i, f.get_row(i) + c.get_row(i));
      cout << "One error added: " << y.get_row(i) << endl;
      decoded.set_row(i, bch_nsys.decode(y.get_row(i)));
      cout << "Decoded to:" << decoded.get_row(i) << endl << endl;
    }

    cout << "Systematic case" << endl;
    cout << "---------------" << endl;
    for (int i = 0; i < u.rows(); i++) {
      c.set_row(i, bch_sys.encode(u.get_row(i)));
      cout << "Encoded " << u.get_row(i) << " to " << c.get_row(i) << endl;
      y.set_row(i, f.get_row(i) + c.get_row(i));
      cout << "One error added: " << y.get_row(i) << endl;
      decoded.set_row(i, bch_sys.decode(y.get_row(i)));
      cout << "Decoded to:" << decoded.get_row(i) << endl << endl;
    }
  }

  return 0;
}