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{h} = } hammgen (@var{m})
18 ## @deftypefnx {Function File} {@var{h} = } hammgen (@var{m},@var{p})
19 ## @deftypefnx {Function File} {[@var{h},@var{g}] = } hammgen (@var{...})
20 ## @deftypefnx {Function File} {[@var{h},@var{g},@var{n},@var{k}] = } hammgen (@var{...})
22 ## Produce the parity check and generator matrices of a Hamming code. The
23 ## variable @var{m} defines the [@var{n},@var{k}] Hamming code where
24 ## @code{@var{n} = 2 ^ @var{m} - 1} and @code{@var{k} = @var{n} - @var{m}}.
25 ## @var{m} must be between 3 and 16.
27 ## The parity check matrix is generated relative to the primitive polynomial
28 ## of GF(2^@var{m}). If @var{p} is specified the default primitive polynomial
29 ## of GF(2^@var{m}) is overridden. @var{p} must be a valid primitive
30 ## polynomial of the correct order for GF(2^@var{m}).
32 ## The parity check matrix is returned in the @var{m} by @var{n} matrix
33 ## @var{h}, and if requested the generator matrix is returned in the @var{k}
34 ## by @var{n} matrix @var{g}.
39 function [h, g, n, k] = hammgen(m, p)
41 if ((nargin < 1) || (nargin > 2))
42 usage ("[h [, g [, n, k]]] = hammgen (m [, p])");
45 if (!isscalar(m) || (floor(m) != m) || (m < 3) || (m > 16))
46 error ("hammgen: m must be an integer between 3 and 16");
53 if ((floor(p) != p) || (p < 2^m) || (p > 2^(m+1)) || !isprimitive(p))
54 error ("hammgen: p must be a primitive polynomial of GF(2^m)");
57 ## Get the primitive polynomial of GF(2^M). Note that the default
58 ## primitive polynomial is not necessarily primpoly(m,"min"), so
59 ## have to create a Galois variable to extract the default primitive.
60 ## The problem is, this limits m to be less than or equal to 16,
61 ## as the Galois type itself is limited to this value
62 p = gf(0,m).prim_poly;
68 [h, g] = cyclgen(n, p);