Mercurial > hg > octave-nkf
annotate scripts/polynomial/deconv.m @ 12118:973f585cfdf2 release-3-2-x
include PTHREAD_CFLAGS in LINK_DEPS for liboctave
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 22 Jan 2010 10:21:33 +0100 |
parents | eb63fbe60fab |
children | f6e0404421f4 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004, |
8920 | 2 ## 2005, 2006, 2007, 2008 John W. Eaton |
2313 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
1025 | 19 |
3368 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} deconv (@var{y}, @var{a}) | |
2311 | 22 ## Deconvolve two vectors. |
3426 | 23 ## |
3368 | 24 ## @code{[b, r] = deconv (y, a)} solves for @var{b} and @var{r} such that |
25 ## @code{y = conv (a, b) + r}. | |
3426 | 26 ## |
3368 | 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 | |
7001 | 29 ## a remainder polynomial of lowest order. |
5642 | 30 ## @seealso{conv, poly, roots, residue, polyval, polyderiv, polyinteg} |
3368 | 31 ## @end deftypefn |
787 | 32 |
3202 | 33 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 34 ## Created: June 1994 |
35 ## Adapted-By: jwe | |
787 | 36 |
2312 | 37 function [b, r] = deconv (y, a) |
787 | 38 |
39 if (nargin != 2) | |
6046 | 40 print_usage (); |
787 | 41 endif |
42 | |
4030 | 43 if (! (isvector (y) && isvector (a))) |
787 | 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 | |
8159
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
52 ## Ensure A is oriented as Y. |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
53 if (diff (size (y)(1:2)) * diff (size (a)(1:2)) < 0) |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
54 a = permute (a, [2, 1]); |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
55 endif |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
56 |
787 | 57 if (ly > la) |
1337 | 58 b = filter (y, a, [1, (zeros (1, ly - la))]); |
787 | 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 | |
8159
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
69 ## Respect the orientation of Y" |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
70 if (size (y, 1) <= size (y, 2)) |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
71 r = [(zeros (1, lc - ly)), y] - conv (a, b); |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
72 else |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
73 r = [(zeros (lc - ly, 1)); y] - conv (a, b); |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
74 endif |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
75 if (ly < la) |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
76 ## Trim the remainder is equal to the length of Y. |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
77 r = r(end-(length(y)-1):end); |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
78 endif |
787 | 79 endif |
80 | |
81 endfunction | |
7411 | 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]); | |
8159
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
89 %! assert(b == 0 && all (all (r == [3, 6]))); |
7411 | 90 |
91 %!test | |
92 %! [b, r] = deconv ([3, 6], [1; 2; 3]); | |
8159
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
93 %! assert(b == 0 && all (all (r == [3, 6]))); |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
94 |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
95 %!test |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
96 %! [b,r] = deconv ([3; 6], [1; 2; 3]); |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
97 %! assert (b == 0 && all (all (r == [3; 6]))) |
7411 | 98 |
8159
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
99 %!test |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
100 %! [b, r] = deconv ([3; 6], [1, 2, 3]); |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
101 %! assert (b == 0 && all (all (r == [3; 6]))) |
7411 | 102 |
8159
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
103 %!error [b, r] = deconv ([3, 6], [1, 2; 3, 4]); |
7411 | 104 |
8159
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
105 %!error [b, r] = deconv ([3, 6; 1, 2], [1, 2, 3]); |
ccf38fc1057f
deconv.m: Fix row/col orientation & length of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
106 |