1 ## Copyright (C) 1994-2012 John W. Eaton
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} deconv (@var{y}, @var{a})
21 ## Deconvolve two vectors.
23 ## @code{[b, r] = deconv (y, a)} solves for @var{b} and @var{r} such that
24 ## @code{y = conv (a, b) + r}.
26 ## If @var{y} and @var{a} are polynomial coefficient vectors, @var{b} will
27 ## contain the coefficients of the polynomial quotient and @var{r} will be
28 ## a remainder polynomial of lowest order.
29 ## @seealso{conv, residue}
32 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
36 function [b, r] = deconv (y, a)
42 if (! (isvector (y) && isvector (a)))
43 error("deconv: both arguments must be vectors");
51 ## Ensure A is oriented as Y.
52 if (diff (size (y)(1:2)) * diff (size (a)(1:2)) < 0)
53 a = permute (a, [2, 1]);
57 x = zeros (size (y) - size (a) + 1);
66 lc = la + length (b) - 1;
70 ## Respect the orientation of Y"
71 if (size (y, 1) <= size (y, 2))
72 r = [(zeros (1, lc - ly)), y] - conv (a, b);
74 r = [(zeros (lc - ly, 1)); y] - conv (a, b);
77 ## Trim the remainder is equal to the length of Y.
78 r = r(end-(length(y)-1):end);
85 %! [b, r] = deconv ([3, 6, 9, 9], [1, 2, 3]);
86 %! assert(all (all (b == [3, 0])) && all (all (r == [0, 0, 0, 9])));
89 %! [b, r] = deconv ([3, 6], [1, 2, 3]);
90 %! assert(b == 0 && all (all (r == [3, 6])));
93 %! [b, r] = deconv ([3, 6], [1; 2; 3]);
94 %! assert(b == 0 && all (all (r == [3, 6])));
97 %! [b,r] = deconv ([3; 6], [1; 2; 3]);
98 %! assert (b == 0 && all (all (r == [3; 6])))
101 %! [b, r] = deconv ([3; 6], [1, 2, 3]);
102 %! assert (b == 0 && all (all (r == [3; 6])))
105 %! assert (deconv ((1:3)',[1, 1]), [1; 1])
107 %!error [b, r] = deconv ([3, 6], [1, 2; 3, 4]);
109 %!error [b, r] = deconv ([3, 6; 1, 2], [1, 2, 3]);