1 ## Copyright (C) 2003 David Bateman
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{code} =} encode (@var{msg},@var{n},@var{k})
18 ## @deftypefnx {Function File} {@var{code} =} encode (@var{msg},@var{n},@var{k},@var{typ})
19 ## @deftypefnx {Function File} {@var{code} =} encode (@var{msg},@var{n},@var{k},@var{typ},@var{opt})
20 ## @deftypefnx {Function File} {[@var{code}, @var{added}] =} encode (@var{...})
22 ## Top level block encoder. This function makes use of the lower level
23 ## functions such as @dfn{cyclpoly}, @dfn{cyclgen}, @dfn{hammgen}, and
24 ## @dfn{bchenco}. The message to code is pass in @var{msg}, the
25 ## codeword length is @var{n} and the message length is @var{k}. This
26 ## function is used to encode messages using either:
29 ## @item A [n,k] linear block code defined by a generator matrix
30 ## @item A [n,k] cyclic code defined by a generator polynomial
31 ## @item A [n,k] Hamming code defined by a primitive polynomial
32 ## @item A [n,k] BCH code code defined by a generator polynomial
35 ## The type of coding to use is defined by the variable @var{typ}. This
36 ## variable is a string taking one of the values
39 ## @item 'linear' or 'linear/binary'
40 ## A linear block code is assumed with the coded message @var{code} being in
41 ## a binary format. In this case the argument @var{opt} is the generator
42 ## matrix, and is required.
43 ## @item 'cyclic' or 'cyclic/binary'
44 ## A cyclic code is assumed with the coded message @var{code} being in a
45 ## binary format. The generator polynomial to use can be defined in @var{opt}.
46 ## The default generator polynomial to use will be
47 ## @dfn{cyclpoly(@var{n},@var{k})}
48 ## @item 'hamming' or 'hamming/binary'
49 ## A Hamming code is assumed with the coded message @var{code} being in a
50 ## binary format. In this case @var{n} must be of an integer of the form
51 ## @code{2^@var{m}-1}, where @var{m} is an integer. In addition @var{k}
52 ## must be @code{@var{n}-@var{m}}. The primitive polynomial to use can
53 ## be defined in @var{opt}. The default primitive polynomial to use is
54 ## the same as defined by @dfn{hammgen}.
55 ## @item 'bch' or 'bch/binary'
56 ## A BCH code is assumed with the coded message @var{code} being in a binary
57 ## format. The generator polynomial to use can be defined in @var{opt}.
58 ## The default generator polynomial to use will be
59 ## @dfn{bchpoly(@var{n},@var{k})}
62 ## In addition the argument 'binary' above can be replaced with 'decimal',
63 ## in which case the message is assumed to be a decimal vector, with each
64 ## value representing a symbol to be coded. The binary format can be in two
68 ## @item An @var{x}-by-@var{k} matrix
69 ## Each row of this matrix represents a symbol to be coded
71 ## The symbols are created from groups of @var{k} elements of this vector.
72 ## If the vector length is not divisble by @var{k}, then zeros are added
73 ## and the number of zeros added is returned in @var{added}.
76 ## It should be noted that all internal calculations are performed in the
77 ## binary format. Therefore for large values of @var{n}, it is preferable
78 ## to use the binary format to pass the messages to avoid possible rounding
79 ## errors. Additionally, if repeated calls to @dfn{encode} will be performed,
80 ## it is often faster to create a generator matrix externally with the
81 ## functions @dfn{hammgen} or @dfn{cyclgen}, rather than let @dfn{encode}
82 ## recalculate this matrix at each iteration. In this case @var{typ} should
83 ## be 'linear'. The exception to this case is BCH codes, whose encoder
84 ## is implemented directly from the polynomial and is significantly faster.
87 ## @seealso{decode,cyclgen,cyclpoly,hammgen,bchenco,bchpoly}
89 function [code, added] = encode(msg, n, k, typ, opt)
91 if ((nargin < 3) || (nargin > 5))
92 usage ("[code, added] = encode (msg, n, k [, typ [, opt]])");
95 if (!isscalar(n) || (n != floor(n)) || (n < 3))
96 error ("encode: codeword length must be an integer greater than 3");
99 if (!isscalar(k) || (k != floor(k)) || (k > n))
100 error ("encode: message length must be an integer less than codeword length");
105 error ("encode: type argument must be a string");
107 ## Why the hell did matlab decide on such an ugly way of passing 2 args!
108 if (strcmp(typ,"linear") || strcmp(typ,"linear/binary"))
111 elseif (strcmp(typ,"linear/decimal"))
114 elseif (strcmp(typ,"cyclic") || strcmp(typ,"cyclic/binary"))
117 elseif (strcmp(typ,"cyclic/decimal"))
120 elseif (strcmp(typ,"bch") || strcmp(typ,"bch/binary"))
123 elseif (strcmp(typ,"bch/decimal"))
126 elseif (strcmp(typ,"hamming") || strcmp(typ,"hamming/binary"))
129 elseif (strcmp(typ,"hamming/decimal"))
133 error ("encode: unrecognized coding and/or message type");
142 if (strcmp(msgtyp,"binary"))
144 if ((max(msg(:)) > 1) || (min(msg(:)) < 0))
145 error ("encode: illegal value in message");
147 [ncodewords, k2] = size(msg);
149 if (min(k2,ncodewords) == 1)
151 msg = vec2mat(msg,k);
152 ncodewords = size(msg,1);
154 error ("encode: message matrix must be k columns wide");
158 error ("encode: decimally encoded message must be a vector");
160 if ((max(msg) > 2^k-1) || (min(msg) < 0))
161 error ("encode: illegal value in message");
163 ncodewords = length(msg);
164 msg = de2bi(msg(:),k);
167 if (strcmp(coding,"bch"))
169 code = bchenco(msg,n,k,opt);
171 code = bchenco(msg,n,k);
174 if (strcmp(coding,"linear"))
177 if ((size(gen,1) != k) || (size(gen,2) != n))
178 error ("encode: generator matrix is in incorrect form");
181 error ("encode: linear coding must supply the generator matrix");
183 elseif (strcmp(coding,"cyclic"))
185 [par, gen] = cyclgen(n,opt);
187 [par, gen] = cyclgen(n,cyclpoly(n,k));
191 if ((m != floor(m)) || (m < 3) || (m > 16))
192 error ("encode: codeword length must be of the form '2^m-1' with integer m");
195 error ("encode: illegal message length for hamming code");
198 [par, gen] = hammgen(m, opt);
200 [par, gen] = hammgen(m);
203 code = mod(msg * gen, 2);
206 if (strcmp(msgtyp,"binary") && (vecttyp == 1))
209 elseif (strcmp(msgtyp,"decimal"))