Mercurial > hg > octave-lyh
annotate scripts/polynomial/convn.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | e9dc2ed2ec0f |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 Soren Hauberg |
7640 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
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. | |
9 ## | |
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. | |
14 ## | |
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/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {@var{c} =} convn (@var{a}, @var{b}, @var{shape}) | |
21 ## @math{N}-dimensional convolution of matrices @var{a} and @var{b}. | |
22 ## | |
23 ## The size of the output is determined by the @var{shape} argument. | |
24 ## This can be any of the following character strings: | |
25 ## | |
26 ## @table @asis | |
27 ## @item "full" | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
28 ## The full convolution result is returned. The size out of the output is |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @code{size (@var{a}) + size (@var{b})-1}. This is the default behaviour. |
7640 | 30 ## @item "same" |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
31 ## The central part of the convolution result is returned. The size out of the |
7640 | 32 ## output is the same as @var{a}. |
33 ## @item "valid" | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
34 ## The valid part of the convolution is returned. The size of the result is |
7640 | 35 ## @code{max (size (@var{a}) - size (@var{b})+1, 0)}. |
36 ## @end table | |
37 ## | |
38 ## @seealso{conv, conv2} | |
39 ## @end deftypefn | |
40 | |
41 function c = convn (a, b, shape = "full") | |
42 | |
43 if (nargin < 2) | |
44 error ("convn: not enough input arguments"); | |
45 endif | |
46 | |
47 if (!ismatrix (a) || !ismatrix (b) || ndims (a) != ndims (b)) | |
48 error ("convn: first and second arguments must be matrices of the same dimensionality"); | |
49 endif | |
50 | |
51 if (!ischar (shape)) | |
52 error ("convn: third input argument must be a string"); | |
53 endif | |
54 | |
55 if (!any (strcmpi (shape, {"full", "same", "valid"}))) | |
56 error ("convn: invalid shape argument: '%s'", shape); | |
57 endif | |
58 | |
59 ## Should we swap 'a' and 'b'? | |
60 ## FIXME -- should we also swap in any of the non-full cases? | |
61 if (numel (b) > numel (a) && strcmpi (shape, "full")) | |
62 tmp = a; | |
63 a = b; | |
64 b = tmp; | |
65 endif | |
66 | |
67 ## Pad A. | |
68 switch (lower (shape)) | |
69 case "full" | |
70 a = pad (a, size (b)-1, size (b)-1); | |
71 case "same" | |
72 a = pad (a, floor ((size (b)-1)/2), ceil ((size (b)-1)/2)); | |
73 endswitch | |
74 | |
75 ## Perform convolution. | |
76 c = __convn__ (a, b); | |
77 | |
78 endfunction | |
79 | |
80 ## Helper function that performs the padding. | |
81 function a = pad (a, left, right) | |
82 cl = class (a); | |
83 for dim = 1:ndims (a) | |
84 l = r = size (a); | |
85 l(dim) = left(dim); | |
86 r(dim) = right(dim); | |
87 a = cat (dim, zeros (l, cl), a, zeros (r, cl)); | |
88 endfor | |
89 endfunction | |
7648
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
90 |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
91 %!test |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
92 %! ## Compare to conv2 |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
93 %! a = rand (100); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
94 %! b = ones (3); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
95 %! c2 = conv2 (a, b, "full"); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
96 %! cn = convn (a, b, "full"); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
97 %! assert (max (abs (cn(:)-c2(:))), 0, 100*eps); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
98 |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
99 %!test |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
100 %! ## Compare to conv2 |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
101 %! a = rand (100); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
102 %! b = ones (3); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
103 %! c2 = conv2 (a, b, "same"); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
104 %! cn = convn (a, b, "same"); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
105 %! assert (max (abs (cn(:)-c2(:))), 0, 100*eps); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
106 |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
107 %!test |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
108 %! ## Compare to conv2 |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
109 %! a = rand (100); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
110 %! b = ones (3); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
111 %! c2 = conv2 (a, b, "valid"); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
112 %! cn = convn (a, b, "valid"); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
113 %! assert (max (abs (cn(:)-c2(:))), 0, 100*eps); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
114 |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
115 %!test |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
116 %! ## Real data |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
117 %! a = ones (10,10,10); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
118 %! b = ones (3,3,3); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
119 %! c = convn (a, b, "valid"); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
120 %! assert (all (c == numel (b))); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
121 |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
122 %!test |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
123 %! ## Complex data |
8507 | 124 %! a = complex(ones (10,10,10), ones(10,10,10)); |
125 %! b = complex(ones (3,3,3), ones(3,3,3)); | |
7648
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
126 %! c = convn (a, b, "valid"); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
127 %! assert (all (c == 2*i*numel (b))); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
128 |