comparison scripts/polynomial/conv.m @ 9433:38a0f9dc0ab4

conv.m: fix Matlab incompatibility; new tests
author Robert T. Short <octave@phaselockedsystems.com>
date Wed, 15 Jul 2009 14:10:14 -0400
parents eb63fbe60fab
children f6e0404421f4
comparison
equal deleted inserted replaced
9432:8cc2d087f3c1 9433:38a0f9dc0ab4
1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004, 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004,
2 ## 2005, 2006, 2007, 2008 John W. Eaton 2 ## 2005, 2006, 2007, 2008, 2009 John W. Eaton
3 ## 3 ##
4 ## This file is part of Octave. 4 ## This file is part of Octave.
5 ## 5 ##
6 ## Octave is free software; you can redistribute it and/or modify it 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 7 ## under the terms of the GNU General Public License as published by
37 if (nargin != 2) 37 if (nargin != 2)
38 print_usage (); 38 print_usage ();
39 endif 39 endif
40 40
41 if (! (isvector (a) && isvector (b))) 41 if (! (isvector (a) && isvector (b)))
42 error("conv: both arguments must be vectors"); 42 error ("conv: both arguments must be vectors");
43 endif 43 endif
44 44
45 la = length (a); 45 la = length (a);
46 lb = length (b); 46 lb = length (b);
47 47
48 ly = la + lb - 1; 48 ly = la + lb - 1;
49 49
50 ## Use the shortest vector as the coefficent vector to filter. 50 ## Use the shortest vector as the coefficent vector to filter.
51 ## Preserve the row/column orientation of the longer input. 51 ## Preserve the row/column orientation of the longer input.
52 if (la < lb) 52 if (la <= lb)
53 if (ly > lb) 53 if (ly > lb)
54 if (size (b, 1) <= size (b, 2)) 54 if (size (b, 1) <= size (b, 2))
55 x = [b, (zeros (1, ly - lb))]; 55 x = [b, (zeros (1, ly - lb))];
56 else 56 else
57 x = [b; (zeros (ly - lb, 1))]; 57 x = [b; (zeros (ly - lb, 1))];
59 else 59 else
60 x = b; 60 x = b;
61 endif 61 endif
62 y = filter (a, 1, x); 62 y = filter (a, 1, x);
63 else 63 else
64 if(ly > la) 64 if (ly > la)
65 if (size (a, 1) <= size (a, 2)) 65 if (size (a, 1) <= size (a, 2))
66 x = [a, (zeros (1, ly - la))]; 66 x = [a, (zeros (1, ly - la))];
67 else 67 else
68 x = [a; (zeros (ly - la, 1))]; 68 x = [a; (zeros (ly - la, 1))];
69 endif 69 endif
73 y = filter (b, 1, x); 73 y = filter (b, 1, x);
74 endif 74 endif
75 75
76 endfunction 76 endfunction
77 77
78 %!assert(all (all (conv (ones (3, 1), ones (3, 1)) == [1; 2; 3; 2; 1]))); 78 %!test
79 79 %! x = ones(3,1);
80 %!assert(all (all (conv (ones (1, 3), ones (3, 1)) == [1, 2, 3, 2, 1]))); 80 %! y = ones(1,3);
81 81 %! b = 2;
82 %!assert(all (all (conv (3, [1, 2, 3]) == [3, 6, 9]))); 82 %! c = 3;
83 83 %! assert (conv (x, x), [1; 2; 3; 2; 1]);
84 %! assert (conv (y, y), [1, 2, 3, 2, 1]);
85 %! assert (conv (x, y), [1, 2, 3, 2, 1]);
86 %! assert (conv (y, x), [1; 2; 3; 2; 1]);
87 %! assert (conv (c, x), [3; 3; 3]);
88 %! assert (conv (c, y), [3, 3, 3]);
89 %! assert (conv (x, c), [3; 3; 3]);
90 %! assert (conv (y, c), [3, 3, 3]);
91 %! assert (conv (b, c), 6);
84 %!error conv ([1, 2; 3, 4], 3); 92 %!error conv ([1, 2; 3, 4], 3);
85
86 %!assert(conv (2, 3),6);
87
88 %!error conv (2, []); 93 %!error conv (2, []);
89 94
90 %!test 95 %!test
91 %! a = 1:10; 96 %! a = 1:10;
92 %! b = 1:3; 97 %! b = 1:3;
93 %! c = conv (a, b); 98 %! assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
94 %! assert (size(c), [1, numel(a)+numel(b)-1]) 99 %! assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])
95 %!test
96 %! a = (1:10).';
97 %! b = 1:3;
98 %! c = conv (a, b);
99 %! assert (size(c), [numel(a)+numel(b)-1, 1])
100 %!test
101 %! a = 1:10;
102 %! b = (1:3).';
103 %! c = conv (a, b);
104 %! assert (size(c), [1, numel(a)+numel(b)-1])
105 100
106 %!test 101 %!test
107 %! b = 1:10; 102 %! a = (1:10).';
108 %! a = 1:3; 103 %! b = 1:3;
109 %! c = conv (a, b); 104 %! assert (size(conv(a,b)), [numel(a)+numel(b)-1, 1])
110 %! assert (size(c), [1, numel(a)+numel(b)-1]) 105 %! assert (size(conv(b,a)), [numel(a)+numel(b)-1, 1])
106
111 %!test 107 %!test
112 %! b = (1:10).'; 108 %! a = 1:10;
113 %! a = 1:3; 109 %! b = (1:3).';
114 %! c = conv (a, b); 110 %! assert (size(conv(a,b)), [1, numel(a)+numel(b)-1])
115 %! assert (size(c), [numel(a)+numel(b)-1, 1]) 111 %! assert (size(conv(b,a)), [1, numel(a)+numel(b)-1])
116 %!test
117 %! b = 1:10;
118 %! a = (1:3).';
119 %! c = conv (a, b);
120 %! assert (size(c), [1, numel(a)+numel(b)-1])
121