]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/gfweight.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / gfweight.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{w} = } gfweight (@var{gen})
18 ## @deftypefnx {Function File} {@var{w} = } gfweight (@var{gen},'gen')
19 ## @deftypefnx {Function File} {@var{w} = } gfweight (@var{par},'par')
20 ## @deftypefnx {Function File} {@var{w} = } gfweight (@var{p},n)
21 ##
22 ## Calculate the minimum weight or distance of a linear block code. The
23 ## code can be either defined by its generator or parity check matrix, or
24 ## its generator polynomial. By default if the first argument is a matrix,
25 ## it is assumed to be the generator matrix of the code. The type of the
26 ## matrix can be defined by a flag 'gen' for the generator matrix or
27 ## 'par' for the parity check matrix.
28 ##
29 ## If the first argument is a vector, it is assumed that it defines the
30 ## generator polynomial of the code. In this case a second argument is
31 ## required that defines the codeword length.
32 ##
33 ## @end deftypefn
34 ## @seealso{hammgen,cyclpoly,bchpoly}
35
36 function w = gfweight (arg1, arg2)
37
38   if ((nargin < 1) || (nargin > 2))
39     usage ("gfweight(mat,typ) or gfweight(poly,n)");
40   endif
41
42   if (isvector(arg1))
43     if (nargin != 2)
44       error ("gfweight: need the codeword length if passing generator polynomial");
45     endif
46     [ign, gen] = cyclgen(arg2, arg1);
47   elseif (ismatrix(arg1))
48     if (nargin == 2)
49       if (ischar(arg2))
50         if (strcmp(arg2,"gen"))
51           gen = arg1;
52         elseif (strcmp(arg2,"par"))
53           gen = gen2par(arg1);
54         else
55           error ("gfweight: unrecognized string argument");
56         endif
57       else
58         error ("gfweight: if first argument is a matrix, the second must be a string");
59       endif
60     else
61       gen = arg1;
62     endif
63   else
64     error ("gfweight: first argument must be a matrix or a vector");
65   endif
66
67   [k, n] = size(gen);
68   if (n < k)
69     error ("gfweight: generator matrix in an illegal form");
70   endif
71
72   ## We only need to test codewords 1:2^k-1 against the zero code word
73   ## We do the equivalent of 
74   ## w = min(sum((mod(de2bi([1:2^k-1]') * gen, 2))'));
75   ## But in a more memory efficient manner in an oct-file
76   w = __gfweight__(gen);
77
78 endfunction