]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/minpol.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / minpol.m
1 ## Copyright (C) 2002 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} {} minpol (@var{v})
18 ##
19 ## Finds the minimum polynomial for elements of a Galois Field. For  a 
20 ## vector @var{v} with @math{N} components, representing @math{N} values 
21 ## in a Galois Field GF(2^@var{m}), return the minimum polynomial in GF(2)
22 ## representing thos values.
23 ## @end deftypefn
24
25 function r = minpol (v)
26
27   if (nargin != 1)
28     error("usage: r = minpol(v)");
29   endif
30
31   if (!isgalois(v))
32     error("minpol: argument must be a galois variable");
33   endif
34
35   if (min (size (v)) > 1 || nargin != 1)
36     usage ("minpol (v), where v is a galois vector");
37   endif
38
39   n = length (v);
40   m = v.m;
41   prim_poly = v.prim_poly;
42   r = zeros(n,m+1);
43
44   ## Find cosets of GF(2^m) and convert from cell array to matrix
45   cyclocoset = cosets(m, prim_poly);
46   cyclomat = zeros(max(size(cyclocoset)),m);
47   for j=1:max(size(cyclocoset))
48     cyclomat(j,1:length(cyclocoset{j})) = cyclocoset{j};
49   end
50   
51   for j =1:n
52     if (v(j) == 0)
53       ## Special case
54       r(j,m-1) = 1;
55     else
56       ## Find the coset within which the current element falls
57       [rc, ignored] = find(cyclomat == v(j));
58
59       rv = cyclomat(rc,:);
60
61       ## Create the minimum polynomial from its roots 
62       ptmp = gf([1,rv(1)], m, prim_poly);
63       for i=2:length(rv)
64         ptmp = conv(ptmp, [1,rv(i)]);
65       end
66
67       ## Need to left-shift polynomial to divide by x while can
68       i = 0;
69       while (!ptmp(m+1-i))
70         i = i + 1;
71       end
72       ptmp = [zeros(1,i), ptmp(1:m+1-i)];
73       r(j,:) = ptmp;
74     endif
75   end
76
77   ## Ok, now put the return value into GF(2)
78   r = gf(r,1);
79   
80 endfunction