Mercurial > octave
changeset 26209:33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
* conv.m: All compuations of conv are already treated by Octaves C++ Function
Fconv2, when using vectorized input. Only the shape of the orginal operands
has to be restored properly. This simplification results in a performance
gain by factor 7-8 for inputs with more than 10^4 elemets.
author | Kai T. Ohlhus <k.ohlhus@gmail.com> |
---|---|
date | Wed, 12 Dec 2018 14:33:35 +0100 |
parents | fc7b842daca1 |
children | da75dfccf14b |
files | scripts/polynomial/conv.m |
diffstat | 1 files changed, 10 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/polynomial/conv.m Wed Dec 12 00:50:34 2018 -0500 +++ b/scripts/polynomial/conv.m Wed Dec 12 14:33:35 2018 +0100 @@ -61,36 +61,17 @@ error ('conv: SHAPE argument must be "full", "same", or "valid"'); endif - la = la_orig = length (a); - lb = lb_orig = length (b); - - ly = la + lb - 1; - - if (ly == 0) - y = zeros (1, 0); - return; - endif + y = conv2 (a(:), b(:), shape); - ## Use shortest vector as the coefficent vector to filter. - if (la > lb) - [a, b] = deal (b, a); # Swap vectors - lb = la; - endif - x = b; - - ## Pad longer vector to convolution length. - if (ly > lb) - x(end+1:end+ly-lb) = 0; - endif - - y = filter (a, 1, x); - - if (strcmpi (shape, "same")) - idx = ceil ((ly - la) / 2); - y = y(idx+1:idx+la); - elseif (strcmpi (shape, "valid")) - len = la_orig - lb_orig; - y = y(lb_orig:lb_orig+len); + if (strcmpi (shape, "full")) + ## Adapt the shape to the longest input argument, if necessary. + if ((length (a) > length (b) && isrow (a)) ... + || (length (a) <= length (b) && isrow (b))) + y = y.'; + endif + elseif (isrow (a)) + ## Adapt the shape to the first input argument, if necessary. + y = y.'; endif endfunction