changelog shortlog tags changeset files revisions annotate raw

scripts/polynomial/conv.m

changeset 10289: 4b124317dc38
parent:38a0f9dc0ab4
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (44 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004,
2## 2005, 2006, 2007, 2008, 2009 John W. Eaton
3##
4## This file is part of Octave.
5##
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {} conv (@var{a}, @var{b})
22## Convolve two vectors.
23##
24## @code{y = conv (a, b)} returns a vector of length equal to
25## @code{length (a) + length (b) - 1}.
26## If @var{a} and @var{b} are polynomial coefficient vectors, @code{conv}
27## returns the coefficients of the product polynomial.
28## @seealso{deconv, poly, roots, residue, polyval, polyderiv, polyint}
29## @end deftypefn
30
31## Author: Tony Richardson <arichard@stark.cc.oh.us>
32## Created: June 1994
33## Adapted-By: jwe
34
35function y = conv (a, b)
36
37 if (nargin != 2)
38 print_usage ();
39 endif
40
41 if (! (isvector (a) && isvector (b)))
42 error ("conv: both arguments must be vectors");
43 endif
44
45 la = length (a);
46 lb = length (b);
47
48 ly = la + lb - 1;
49
50 ## Use the shortest vector as the coefficent vector to filter.
51 ## Preserve the row/column orientation of the longer input.
52 if (la <= lb)
53 if (ly > lb)
54 if (size (b, 1) <= size (b, 2))
55 x = [b, (zeros (1, ly - lb))];
56 else
57 x = [b; (zeros (ly - lb, 1))];
58 endif
59 else
60 x = b;
61 endif
62 y = filter (a, 1, x);
63 else
64 if (ly > la)
65 if (size (a, 1) <= size (a, 2))
66 x = [a, (zeros (1, ly - la))];
67 else
68 x = [a; (zeros (ly - la, 1))];
69 endif
70 else
71 x = a;
72 endif
73 y = filter (b, 1, x);
74 endif
75
76endfunction
77
78%!test
79%! x = ones(3,1);
80%! y = ones(1,3);
81%! b = 2;
82%! c = 3;
83%! assert (conv (x, x), [1; 2; 3; 2; 1]);
84%! assert (conv (y, y), [1, 2, 3, 2, 1]);
85%! assert (conv (x, y), [1, 2, 3, 2, 1]);
86%! assert (conv (y, x), [1; 2; 3; 2; 1]);
87%! assert (conv (c, x), [3; 3; 3]);
88%! assert (conv (c, y), [3, 3, 3]);
89%! assert (conv (x, c), [3; 3; 3]);
90%! assert (conv (y, c), [3, 3, 3]);
91%! assert (conv (b, c), 6);
92%!error conv ([1, 2; 3, 4], 3);
93%!error conv (2, []);
94
95%!test
96%! a = 1:10;
97%! b = 1:3;
98%! assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
99%! assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])
100
101%!test
102%! a = (1:10).';
103%! b = 1:3;
104%! assert (size(conv(a,b)), [numel(a)+numel(b)-1, 1])
105%! assert (size(conv(b,a)), [numel(a)+numel(b)-1, 1])
106
107%!test
108%! a = 1:10;
109%! b = (1:3).';
110%! assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
111%! assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])