File: egolay.cpp

package info (click to toggle)
libitpp 4.3.1-14
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (143 lines) | stat: -rw-r--r-- 4,021 bytes parent folder | download | duplicates (7)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*!
 * \file
 * \brief Implementation of the Extended Golay Code (24, 12, 8)
 * \author Tony Ottosson
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ 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 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ 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 IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/comm/egolay.h>
#include <itpp/comm/commfunc.h>
#include <itpp/base/specmat.h>
#include <itpp/base/converters.h>

namespace itpp
{

Extended_Golay::Extended_Golay(void)
{
  B = "0 1 1 1 1 1 1 1 1 1 1 1;1 1 1 0 1 1 1 0 0 0 1 0;1 1 0 1 1 1 0 0 0 1 0 1;1 0 1 1 1 0 0 0 1 0 1 1;1 1 1 1 0 0 0 1 0 1 1 0;1 1 1 0 0 0 1 0 1 1 0 1;1 1 0 0 0 1 0 1 1 0 1 1;1 0 0 0 1 0 1 1 0 1 1 1;1 0 0 1 0 1 1 0 1 1 1 0;1 0 1 0 1 1 0 1 1 1 0 0;1 1 0 1 1 0 1 1 1 0 0 0;1 0 1 1 0 1 1 1 0 0 0 1";

  G = concat_horizontal(eye_b(12), B);
}

void Extended_Golay::encode(const bvec &uncoded_bits, bvec &coded_bits)
{
  int no_bits = uncoded_bits.length();
  int no_blocks = floor_i(no_bits / 12.0);

  coded_bits.set_size(24*no_blocks, false);
  bmat Gt = G.T();
  int i;

  for (i = 0; i < no_blocks; i++)
    coded_bits.replace_mid(24*i, Gt * uncoded_bits.mid(i*12, 12));
}

bvec Extended_Golay::encode(const bvec &uncoded_bits)
{
  bvec coded_bits;
  encode(uncoded_bits, coded_bits);
  return coded_bits;
}

void Extended_Golay::decode(const bvec &coded_bits, bvec &decoded_bits)
{
  int no_bits = coded_bits.length();
  int no_blocks = floor_i(no_bits / 24.0);

  decoded_bits.set_size(12*no_blocks, false);
  int i, j;
  bvec S(12), BS(12), r(12), temp(12), e(24), c(24);
  bmat eyetemp = eye_b(12);

  for (i = 0; i < no_blocks; i++) {
    r = coded_bits.mid(i * 24, 24);
    // Step 1. Compute S=G*r.
    S = G * r;
    // Step 2. w(S)<=3. e=(S,0). Goto 8.
    if (weight(S) <= 3) {
      e = concat(S, zeros_b(12));
      goto Step8;
    }

    // Step 3. w(S+Ii)<=2. e=(S+Ii,yi). Goto 8.
    for (j = 0; j < 12; j++) {

      temp = S + B.get_col(j);
      if (weight(temp) <= 2) {
        e = concat(temp, eyetemp.get_row(j));
        goto Step8;
      }
    }

    // STEP 4. Compute B*S
    BS = B * S;

    // Step 5. w(B*S)<=3. e=(0,BS). Goto8.
    if (weight(BS) <= 3) {
      e = concat(zeros_b(12), BS);
      goto Step8;
    }

    // Step 6. w(BS+Ri)<=2. e=(xi,BS+Ri). Goto 8.
    for (j = 0; j < 12; j++) {
      temp = BS + B.get_row(j);
      if (weight(temp) <= 2) {
        e = concat(eyetemp.get_row(j), temp);
        goto Step8;
      }
    }

    // Step 7. Uncorrectable erreor pattern. Choose the first 12 bits.
    e = zeros_b(24);
    goto Step8;

  Step8: // Step 8. c=r+e. STOP
    c = r + e;
    decoded_bits.replace_mid(i*12, c.left(12));
  }
}

bvec Extended_Golay::decode(const bvec &coded_bits)
{
  bvec decoded_bits;
  decode(coded_bits, decoded_bits);
  return decoded_bits;
}


// -------------- Soft-decision decoding is not implemented ------------------
void Extended_Golay::decode(const vec &, bvec &)
{
  it_error("Extended_Golay::decode(vec, bvec); soft-decision decoding is not implemented");
}

bvec Extended_Golay::decode(const vec &)
{
  it_error("Extended_Golay::decode(vec, bvec); soft-decision decoding is not implemented");
  return bvec();
}


} // namespace itpp