Mercurial > hg > octave-lojdl
comparison scripts/polynomial/convn.m @ 7640:3398ce778b4b
Added support for N-dimensional convolution
author | sh@sh-laptop |
---|---|
date | Tue, 25 Mar 2008 21:28:02 -0400 |
parents | |
children | e7b999840056 |
comparison
equal
deleted
inserted
replaced
7639:b2fbb393a072 | 7640:3398ce778b4b |
---|---|
1 ## Copyright (C) 2008 SÃ?ren Hauberg | |
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" | |
28 ## The full convolution result is returned. The size out of the output is | |
29 ## @code{size (@var{a}) + size (@var{b})-1}. This is the default behaviour. | |
30 ## @item "same" | |
31 ## The central part of the convolution result is returned. The size out of the | |
32 ## output is the same as @var{a}. | |
33 ## @item "valid" | |
34 ## The valid part of the convolution is returned. The size of the result is | |
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 |