changelog shortlog tags changeset files revisions annotate raw

scripts/polynomial/deconv.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (56 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004,
2## 2005, 2006, 2007, 2008 John W. Eaton
3##
4## This file is part of Octave.
5##
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.
10##
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.
15##
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/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {} deconv (@var{y}, @var{a})
22## Deconvolve two vectors.
23##
24## @code{[b, r] = deconv (y, a)} solves for @var{b} and @var{r} such that
25## @code{y = conv (a, b) + r}.
26##
27## If @var{y} and @var{a} are polynomial coefficient vectors, @var{b} will
28## contain the coefficients of the polynomial quotient and @var{r} will be
29## a remainder polynomial of lowest order.
30## @seealso{conv, poly, roots, residue, polyval, polyderiv, polyint}
31## @end deftypefn
32
33## Author: Tony Richardson <arichard@stark.cc.oh.us>
34## Created: June 1994
35## Adapted-By: jwe
36
37function [b, r] = deconv (y, a)
38
39 if (nargin != 2)
40 print_usage ();
41 endif
42
43 if (! (isvector (y) && isvector (a)))
44 error("conv: both arguments must be vectors");
45 endif
46
47 la = length (a);
48 ly = length (y);
49
50 lb = ly - la + 1;
51
52 ## Ensure A is oriented as Y.
53 if (diff (size (y)(1:2)) * diff (size (a)(1:2)) < 0)
54 a = permute (a, [2, 1]);
55 endif
56
57 if (ly > la)
58 b = filter (y, a, [1, (zeros (1, ly - la))]);
59 elseif (ly == la)
60 b = filter (y, a, 1);
61 else
62 b = 0;
63 endif
64
65 lc = la + length (b) - 1;
66 if (ly == lc)
67 r = y - conv (a, b);
68 else
69 ## Respect the orientation of Y"
70 if (size (y, 1) <= size (y, 2))
71 r = [(zeros (1, lc - ly)), y] - conv (a, b);
72 else
73 r = [(zeros (lc - ly, 1)); y] - conv (a, b);
74 endif
75 if (ly < la)
76 ## Trim the remainder is equal to the length of Y.
77 r = r(end-(length(y)-1):end);
78 endif
79 endif
80
81endfunction
82
83%!test
84%! [b, r] = deconv ([3, 6, 9, 9], [1, 2, 3]);
85%! assert(all (all (b == [3, 0])) && all (all (r == [0, 0, 0, 9])));
86
87%!test
88%! [b, r] = deconv ([3, 6], [1, 2, 3]);
89%! assert(b == 0 && all (all (r == [3, 6])));
90
91%!test
92%! [b, r] = deconv ([3, 6], [1; 2; 3]);
93%! assert(b == 0 && all (all (r == [3, 6])));
94
95%!test
96%! [b,r] = deconv ([3; 6], [1; 2; 3]);
97%! assert (b == 0 && all (all (r == [3; 6])))
98
99%!test
100%! [b, r] = deconv ([3; 6], [1, 2, 3]);
101%! assert (b == 0 && all (all (r == [3; 6])))
102
103%!error [b, r] = deconv ([3, 6], [1, 2; 3, 4]);
104
105%!error [b, r] = deconv ([3, 6; 1, 2], [1, 2, 3]);
106