]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/@galois/conv.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / @galois / conv.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} {} conv (@var{a}, @var{b})
18 ## Convolve two Galois vectors.
19 ##
20 ## @code{y = conv (a, b)} returns a vector of length equal to
21 ## @code{length (a) + length (b) - 1}.
22 ## If @var{a} and @var{b} are polynomial coefficient vectors, @code{conv}
23 ## returns the coefficients of the product polynomial.
24 ## @end deftypefn
25 ## @seealso{deconv}
26
27 function y = conv (a, b)
28
29   if (nargin != 2)
30     usage ("conv(a, b)");
31   endif
32
33   if (!isgalois (a) && !isgalois (b))
34     error("conv: at least one argument must be a galois variable");
35   elseif (!isgalois (a))
36     a = gf(a, b.m, b.prim_poly);
37   elseif (!isgalois (b))
38     b = gf(b, a.m, a.prim_poly);
39   elseif (a.m != b.m && a.prim_poly != b.prim_poly)
40     error("conv: both vectors must be in the same galois field");
41   endif
42   
43   if (min(size(a)) > 1 || min(size(b)) > 1)
44     error("conv: both arguments must be vectors");
45   endif
46
47   la = length (a);
48   lb = length (b);
49
50   ly = la + lb - 1;
51
52   ## Ensure that both vectors are row vectors.
53   if (rows (a) > 1)
54     a = reshape (a, 1, la);
55   endif
56   if (rows (b) > 1)
57     b = reshape (b, 1, lb);
58   endif
59
60   ## Use the shortest vector as the coefficent vector to filter.
61   if (la < lb)
62     if (ly > lb)
63       ## Can't concatenate galois variables like this yet
64       ## x = [b, (zeros (1, ly - lb))];
65       x = gf([b, (zeros (1, ly - lb))], b.m, b.prim_poly);
66     else
67       x = b;
68     endif
69     y = filter (a, 1, x);
70   else
71     if(ly > la)
72       ## Can't concatenate galois variables like this yet
73       ## x = [a, (zeros (1, ly - la))];
74       x = gf([a, (zeros (1, ly - la))], a.m, a.prim_poly);
75     else
76       x = a;
77     endif
78     y = filter (b, 1, x);
79   endif
80
81 endfunction