1## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004,
2## 2005, 2006, 2007, 2008, 2009 John W. Eaton
4## This file is part of Octave.
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.
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.
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/>.
21## @deftypefn {Function File} {} conv (@var{a}, @var{b})
22## Convolve two vectors.
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}
31## Author: Tony Richardson <arichard@stark.cc.oh.us>
35function y = conv (a, b)
41 if (! (isvector (a) && isvector (b)))
42 error ("conv: both arguments must be vectors");
50 ## Use the shortest vector as the coefficent vector to filter.
51 ## Preserve the row/column orientation of the longer input.
54 if (size (b, 1) <= size (b, 2))
55 x = [b, (zeros (1, ly - lb))];
57 x = [b; (zeros (ly - lb, 1))];
65 if (size (a, 1) <= size (a, 2))
66 x = [a, (zeros (1, ly - la))];
68 x = [a; (zeros (ly - la, 1))];
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);
98%! assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
99%! assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])
104%! assert (size(conv(a,b)), [numel(a)+numel(b)-1, 1])
105%! assert (size(conv(b,a)), [numel(a)+numel(b)-1, 1])
110%! assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
111%! assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])