]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/hammgen.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / hammgen.m
1 ## Copyright (C) 2003 David Bateman
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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{...})
21 ##
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.
26 ##
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}).
31 ##
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}.
35 ##
36 ## @end deftypefn
37 ## @seealso{gen2par}
38
39 function [h, g, n, k] = hammgen(m, p)
40
41   if ((nargin < 1) || (nargin > 2))
42     usage ("[h [, g [, n, k]]] = hammgen (m [, p])");
43   endif
44
45   if (!isscalar(m) || (floor(m) != m) || (m < 3) || (m > 16))
46     error ("hammgen: m must be an integer between 3 and 16");
47   endif
48
49   if (nargin > 1)
50     if (!isscalar(p))
51       p = bi2de(p);
52     endif
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)");
55     endif
56   else
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;
63   endif
64
65   n = 2^m -1;
66   k = n - m;
67   if (nargout > 1)
68     [h, g] = cyclgen(n, p);
69   else
70     h = cyclgen(n,p);
71   endif
72   
73 endfunction