From octave-sources-request at bevo dot che dot wisc dot edu Wed Dec 20 15:34:20 2000 Subject: improved conv.m From: "Johan Kullstam" To: octave-sources at bevo dot che dot wisc dot edu Date: 20 Dec 2000 16:36:28 -0500 conv.m can do convolution of two vectors x = conv(a,b) since the convolution operation is associative, we can allow more vectors y = conv(a,b,c) = conv(conv(a,b),c) = conv(a,conv(b,c)) this new version uses varargs to process one or more vectors. it is fully backwards compatible with the old version. i'd like to have this version go into the standard library of octave m-files. enjoy. johan ## Copyright (C) 1996, 1997 John W. Eaton ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## -*- texinfo -*- ## at deftypefn {Function File} {} conv (@var{a}, @var{b}, ...) ## Convolve one or more vectors. ## ## at code{y = conv (a, b)} returns a vector of length equal to ## at code{length (a) + length (b) - 1}. ## If at var{a} and @var{b} are polynomial coefficient vectors, @code{conv} ## returns the coefficients of the product polynomial. ## ## at code{y = conv (a, ...)} returns the convolution of one or more vectors. ## at end deftypefn ## at seealso{deconv, poly, roots, residue, polyval, polyderiv, and polyinteg} ## Author: Tony Richardson ## Created: June 1994 ## Adapted-By: jwe ## ## Dec 2000 Johan Kullstam ## do multiple convolution function a = conv(a,...) ## Ensure that all inputs are vectors. if (!is_vector(a)) error("conv: all arguments must be vectors"); endif ## Ensure that all vectors are row vectors. ## and get the length in the meantime. [r,c] = size(a); if r > 1, la = r; a = reshape (a, 1, la); else la = c; endif va_start(); ## loop over all inputs while (--nargin) b = va_arg(); ## Ensure that all inputs are vectors. if (!is_vector(b)) error("conv: all arguments must be vectors"); endif ## Ensure that all vectors are row vectors. ## and get the length in the meantime. [r,c] = size(b); if r > 1, lb = r; b = reshape(b, 1, lb); else lb = c; endif ## Use the shortest vector as the coefficent vector to filter. ly = la + lb - 1; if (la < lb) if (ly > lb) x = [b, (zeros(1, ly-lb))]; else x = b; endif a = filter(a, 1, x); else if (ly > la) x = [a, (zeros(1, ly-la))]; else x = a; endif a = filter(b, 1, x); endif la = ly; endwhile endfunction -- J o h a n K u l l s t a m [kullstam at ne dot mediaone dot net] Don't Fear the Penguin! ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------