Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[b92126]: src / syndtable.cc Maximize Restore History

Download this file

syndtable.cc    166 lines (147 with data), 5.0 kB

  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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//Copyright (C) 2003 David Bateman
//
// 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 3 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, see
// <http://www.gnu.org/licenses/>.
//
// In addition to the terms of the GPL, you are permitted to link this
// program with any Open Source program, as defined by the Open Source
// Initiative (www.opensource.org)
#include <iostream>
#include <iomanip>
#include <sstream>
#include <octave/oct.h>
#include <octave/pager.h>
#define COL_MAJ(N) (N / (SIZEOF_INT << 3))
#define COL_MIN(N) (N % (SIZEOF_INT << 3))
Array<int>
get_errs (const int& nmin, const int& nmax, const int &nerrs)
{
Array<int> pos;
int cols = COL_MAJ (nmax)+1;
OCTAVE_QUIT;
if (nerrs == 1)
{
pos.resize (dim_vector (nmax-nmin, cols), 0);
for (int i = nmin; i < nmax; i++)
{
pos(i-nmin, COL_MAJ (i)) = (1<<COL_MIN (i));
}
}
else
{
for (int i = nmin; i < nmax - nerrs + 1; i++)
{
Array<int> new_pos = get_errs (i+1, nmax, nerrs-1);
int l = pos.rows ();
pos.resize (dim_vector (l+new_pos.rows (), cols), 0);
for (int j=0; j<new_pos.rows (); j++)
{
for (int k = 0; k < cols; k++)
pos(l+j, k) = new_pos(j, k);
pos(l+j, COL_MAJ (i)) += (1<<COL_MIN (i));
}
}
}
return pos;
}
DEFUN_DLD (syndtable, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{t} =} syndtable (@var{h})\n\
Create the syndrome decoding table from the parity check matrix @var{h}.\n\
Each row of the returned matrix @var{t} represents the error vector in\n\
a received symbol for a certain syndrome. The row selected is determined\n\
by a conversion of the syndrome to an integer representation, and using\n\
this to reference each row of @var{t}.\n\
@seealso{hammgen, cyclgen}\n\
@end deftypefn")
{
octave_value retval;
int nargin = args.length ();
if (nargin != 1)
{
print_usage ();
return retval;
}
if (!args(0).is_real_matrix ())
{
error ("syndtable: parity check matrix must be a real matrix");
return retval;
}
Matrix h = args(0).matrix_value ();
int m = h.rows ();
int n = h.columns ();
unsigned int nrows = ((unsigned int)1 << m);
// Could convert this to unsigned long long, but there isn't much point.
// the syndrome table can already have 2^32 rows with n columns, which
// is already unrealistically large. The result is DON'T use this with
// large BCH codes!!!!
if (m > (int)(sizeof (int) << 3))
{
error ("syndtable: codeword minus message length must be less than %d",
(sizeof (int) << 3));
return retval;
}
// Check that the data in h is valid in GF(2)
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (((h(i, j) != 0) && (h(i, j) != 1)) ||
((h(i, j) - (double)((int)h(i, j))) != 0))
{
error ("syndtable: parity check matrix contains invalid data");
return retval;
}
RowVector filled (nrows, 0);
Matrix table (nrows, n, 0);
unsigned int nfilled = nrows;
int nerrs = 1;
// The first row of the table is for no errors
nfilled--;
filled(0) = 1;
while (nfilled != 0)
{
// Get all possible combinations of nerrs bit errors in n bits
Array<int> errpos = get_errs (0, n, nerrs);
// Calculate the syndrome with the error vectors just calculated
for (int j = 0; j < errpos.rows (); j++)
{
int syndrome = 0;
for (int i = 0; i < m; i++)
{
for (int k = 0; k < n; k++)
syndrome ^= (errpos(j, COL_MAJ (k)) &
((unsigned int)h(i, k) << COL_MIN (k)) ?
((unsigned int)1<<(m-i-1)) : 0);
}
// Now use the syndrome as the rows indices to put the error vectors
// in place
if (((unsigned int)syndrome < nrows) && !filled(syndrome))
{
filled(syndrome) = 1;
nfilled--;
for (int i = 0; i < n; i++)
table(syndrome, i) = ((errpos(j, COL_MAJ (i)) &
((unsigned int)1 << COL_MIN (i))) != 0);
}
}
nerrs++;
}
retval = octave_value (table);
return retval;
}
/*
%% Test input validation
%!error syndtable ()
%!error syndtable (1, 2)
%!error syndtable ([1 2])
*/