home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / polynom / conv.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  77 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: conv (a, b)
  21. ##
  22. ## Convolve two vectors.
  23. ##
  24. ## y = conv (a, b) returns a vector of length equal to length (a) +
  25. ## length (b) -1.
  26. ##
  27. ## If a and b are polynom coefficient vectors, conv returns the
  28. ## coefficients of the product polynom.
  29. ##
  30. ## SEE ALSO: deconv, poly, roots, residue, polyval, polyderv, polyintg
  31.  
  32. ## Author: Tony Richardson <arichard@stark.cc.oh.us>
  33. ## Created: June 1994
  34. ## Adapted-By: jwe
  35.  
  36. function y = conv (a, b)
  37.  
  38.   if (nargin != 2)
  39.     usage ("conv(a, b)");
  40.   endif
  41.  
  42.   if (! (is_vec (a) && is_vec (b)))
  43.     error("conv: both arguments must be vectors");
  44.   endif
  45.  
  46.   la = length (a);
  47.   lb = length (b);
  48.  
  49.   ly = la + lb - 1;
  50.  
  51.   ## Ensure that both vectors are row vectors.
  52.   if (rows (a) > 1)
  53.     a = reshape (a, 1, la);
  54.   endif
  55.   if (rows (b) > 1)
  56.     b = reshape (b, 1, lb);
  57.   endif
  58.  
  59.   ## Use the shortest vector as the coefficent vector to filter.
  60.   if (la < lb)
  61.     if (ly > lb)
  62.       x = [b, (zeros (1, ly - lb))];
  63.     else
  64.       x = b;
  65.     endif
  66.     y = filter (a, 1, x);
  67.   else
  68.     if(ly > la)
  69.       x = [a, (zeros (1, ly - la))];
  70.     else
  71.       x = a;
  72.     endif
  73.     y = filter (b, 1, x);
  74.   endif
  75.  
  76. endfunction
  77.