GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-convn.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2010-2013 VZLU Prague
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <iostream>
28 #include <algorithm>
29 
30 #include "f77-fcn.h"
31 
32 #include "oct-convn.h"
33 #include "oct-locbuf.h"
34 
35 // 2d convolution with a matrix kernel.
36 template <class T, class R>
37 static void
38 convolve_2d (const T *a, octave_idx_type ma, octave_idx_type na,
39  const R *b, octave_idx_type mb, octave_idx_type nb,
40  T *c, bool inner);
41 
42 // Forward instances to our Fortran implementations.
43 #define FORWARD_IMPL(T,R,f,F) \
44 extern "C" \
45 F77_RET_T \
46 F77_FUNC (f##conv2o, F##CONV2O) (const octave_idx_type&, \
47  const octave_idx_type&, \
48  const T*, const octave_idx_type&, \
49  const octave_idx_type&, const R*, T *); \
50 \
51 extern "C" \
52 F77_RET_T \
53 F77_FUNC (f##conv2i, F##CONV2I) (const octave_idx_type&, \
54  const octave_idx_type&, \
55  const T*, const octave_idx_type&, \
56  const octave_idx_type&, const R*, T *); \
57 \
58 template <> void \
59 convolve_2d<T, R> (const T *a, octave_idx_type ma, octave_idx_type na, \
60  const R *b, octave_idx_type mb, octave_idx_type nb, \
61  T *c, bool inner) \
62 { \
63  if (inner) \
64  F77_XFCN (f##conv2i, F##CONV2I, (ma, na, a, mb, nb, b, c)); \
65  else \
66  F77_XFCN (f##conv2o, F##CONV2O, (ma, na, a, mb, nb, b, c)); \
67 }
68 
69 FORWARD_IMPL (double, double, d, D)
70 FORWARD_IMPL (float, float, s, S)
71 FORWARD_IMPL (Complex, Complex, z, Z)
72 FORWARD_IMPL (FloatComplex, FloatComplex, c, C)
73 FORWARD_IMPL (Complex, double, zd, ZD)
74 FORWARD_IMPL (FloatComplex, float, cs, CS)
75 
76 template <class T, class R>
77 void convolve_nd (const T *a, const dim_vector& ad, const dim_vector& acd,
78  const R *b, const dim_vector& bd, const dim_vector& bcd,
79  T *c, const dim_vector& ccd, int nd, bool inner)
80 {
81  if (nd == 2)
82  convolve_2d<T, R> (a, ad(0), ad(1), b, bd(0), bd(1), c, inner);
83  else
84  {
85  octave_idx_type ma = acd(nd-2);
86  octave_idx_type na = ad(nd-1);
87  octave_idx_type mb = bcd(nd-2);
88  octave_idx_type nb = bd(nd-1);
89  octave_idx_type ldc = ccd(nd-2);
90  if (inner)
91  {
92  for (octave_idx_type ja = 0; ja < na - nb + 1; ja++)
93  for (octave_idx_type jb = 0; jb < nb; jb++)
94  convolve_nd<T, R> (a + ma*(ja+jb), ad, acd,
95  b + mb*(nb-jb-1), bd, bcd,
96  c + ldc*ja, ccd, nd-1, inner);
97  }
98  else
99  {
100  for (octave_idx_type ja = 0; ja < na; ja++)
101  for (octave_idx_type jb = 0; jb < nb; jb++)
102  convolve_nd<T, R> (a + ma*ja, ad, acd, b + mb*jb, bd, bcd,
103  c + ldc*(ja+jb), ccd, nd-1, inner);
104  }
105  }
106 }
107 
108 // Arbitrary convolutor.
109 // The 2nd array is assumed to be the smaller one.
110 template <class T, class R>
111 static MArray<T>
112 convolve (const MArray<T>& a, const MArray<R>& b,
113  convn_type ct)
114 {
115  if (a.is_empty () || b.is_empty ())
116  return MArray<T> ();
117 
118  int nd = std::max (a.ndims (), b.ndims ());
119  const dim_vector adims = a.dims ().redim (nd), bdims = b.dims ().redim (nd);
120  dim_vector cdims = dim_vector::alloc (nd);
121 
122  for (int i = 0; i < nd; i++)
123  {
124  if (ct == convn_valid)
125  cdims(i) = std::max (adims(i) - bdims(i) + 1,
126  static_cast<octave_idx_type> (0));
127  else
128  cdims(i) = std::max (adims(i) + bdims(i) - 1,
129  static_cast<octave_idx_type> (0));
130  }
131 
132  MArray<T> c (cdims, T ());
133 
134  convolve_nd<T, R> (a.fortran_vec (), adims, adims.cumulative (),
135  b.fortran_vec (), bdims, bdims.cumulative (),
136  c.fortran_vec (), cdims.cumulative (),
137  nd, ct == convn_valid);
138 
139  if (ct == convn_same)
140  {
141  // Pick the relevant part.
142  Array<idx_vector> sidx (dim_vector (nd, 1));
143 
144  for (int i = 0; i < nd; i++)
145  sidx(i) = idx_vector::make_range (bdims(i)/2, 1, adims(i));
146  c = c.index (sidx);
147  }
148 
149  return c;
150 }
151 
152 #define CONV_DEFS(TPREF, RPREF) \
153 TPREF ## NDArray \
154 convn (const TPREF ## NDArray& a, const RPREF ## NDArray& b, convn_type ct) \
155 { \
156  return convolve (a, b, ct); \
157 } \
158 TPREF ## Matrix \
159 convn (const TPREF ## Matrix& a, const RPREF ## Matrix& b, convn_type ct) \
160 { \
161  return convolve (a, b, ct); \
162 } \
163 TPREF ## Matrix \
164 convn (const TPREF ## Matrix& a, const RPREF ## ColumnVector& c, \
165  const RPREF ## RowVector& r, convn_type ct) \
166 { \
167  return convolve (a, c * r, ct); \
168 }
169 
172 CONV_DEFS (Complex, Complex)
173 CONV_DEFS (Float, Float)
175 CONV_DEFS (FloatComplex, FloatComplex)