[07d29c]: conv2.cc Maximize Restore History

Download this file

conv2.cc    379 lines (327 with data), 9.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* Copyright (C) 1999-2005 Andy Adler
* conv2: 2D convolution for octave
*
* $Id$
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
## 2000-05-17: Paul Kienzle
## * change argument to vector conversion to work for 2.1 series octave
## as well as 2.0 series
## 2001-02-05: Paul Kienzle
## * accept complex arguments
*/
#include <octave/oct.h>
using namespace std;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
enum Shape { SHAPE_FULL, SHAPE_SAME, SHAPE_VALID };
#if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL)
extern MArray2<double>
conv2 (MArray<double>&, MArray<double>&, MArray2<double>&, Shape);
extern MArray2<Complex>
conv2 (MArray<Complex>&, MArray<Complex>&, MArray2<Complex>&, Shape);
#endif
template <class T>
MArray2<T>
conv2 (MArray<T>& R, MArray<T>& C, MArray2<T>& A, Shape ishape)
{
int Rn= R.length();
int Cm= C.length();
int Am = A.rows();
int An = A.columns();
/*
* Calculate the size of the output matrix:
* in order to stay Matlab compatible, it is based
* on the third parameter if it's separable, and the
* first if it's not
*/
int outM=0,
outN=0,
edgM=0,
edgN=0;
switch (ishape)
{
case SHAPE_FULL:
outM= Am + Cm - 1;
outN= An + Rn - 1;
edgM= Cm - 1;
edgN= Rn - 1;
break;
case SHAPE_SAME:
outM= Am;
outN= An;
// Follow the Matlab convention (ie + instead of -)
edgM= ( Cm - 1) /2;
edgN= ( Rn - 1) /2;
break;
case SHAPE_VALID:
outM= Am - Cm + 1;
outN= An - Rn + 1;
if (outM < 0) outM = 0;
if (outN < 0) outN = 0;
edgM= edgN= 0;
break;
default:
error("conv2: invalid value of parameter ishape");
}
MArray2<T> O(outM,outN);
/*
* X accumulates the 1-D conv for each row, before calculating
* the convolution in the other direction
* There is no efficiency advantage to doing it in either direction
* first
*/
MArray<T> X( An );
for( int oi=0; oi < outM; oi++ )
{
for( int oj=0; oj < An; oj++ )
{
T sum=0;
int ci= Cm - 1 - MAX(0, edgM-oi);
int ai= MAX(0, oi-edgM) ;
const T* Ad= A.data() + ai + Am*oj;
const T* Cd= C.data() + ci;
for( ; ci >= 0 && ai < Am;
ci--,
Cd--,
ai++,
Ad++)
{
sum+= (*Ad) * (*Cd);
}
X(oj)= sum;
}
for( int oj=0; oj < outN; oj++ )
{
T sum=0;
int rj= Rn - 1 - MAX(0, edgN-oj);
int aj= MAX(0, oj-edgN) ;
const T* Xd= X.data() + aj;
const T* Rd= R.data() + rj;
for( ; rj >= 0 && aj < An;
rj--,
Rd--,
aj++,
Xd++)
{
sum+= (*Xd) * (*Rd);
}
O(oi,oj)= sum;
}
}
return O;
}
#if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL)
extern MArray2<double>
conv2 (MArray2<double>&, MArray2<double>&, Shape);
extern MArray2<Complex>
conv2 (MArray2<Complex>&, MArray2<Complex>&, Shape);
#endif
template <class T>
MArray2<T>
conv2 (MArray2<T>&A, MArray2<T>&B, Shape ishape)
{
/* Convolution works fastest if we choose the A matrix to be
* the largest.
*
* Here we calculate the size of the output matrix,
* in order to stay Matlab compatible, it is based
* on the third parameter if it's separable, and the
* first if it's not
*
* NOTE in order to be Matlab compatible, we give argueably
* wrong sizes for 'valid' if the smallest matrix is first
*/
int Am = A.rows();
int An = A.columns();
int Bm = B.rows();
int Bn = B.columns();
int outM=0,
outN=0,
edgM=0,
edgN=0;
switch (ishape)
{
case SHAPE_FULL:
outM= Am + Bm - 1;
outN= An + Bn - 1;
edgM= Bm - 1;
edgN= Bn - 1;
break;
case SHAPE_SAME:
outM= Am;
outN= An;
edgM= ( Bm - 1) /2;
edgN= ( Bn - 1) /2;
break;
case SHAPE_VALID:
outM= Am - Bm + 1;
outN= An - Bn + 1;
if (outM < 0) outM = 0;
if (outN < 0) outN = 0;
edgM= edgN= 0;
break;
}
MArray2<T> O(outM,outN);
for( int oi=0; oi < outM; oi++ )
{
for( int oj=0; oj < outN; oj++ )
{
T sum=0;
for( int bj= Bn - 1 - MAX(0, edgN-oj),
aj= MAX(0, oj-edgN);
bj >= 0 && aj < An;
bj--,
aj++)
{
int bi= Bm - 1 - MAX(0, edgM-oi);
int ai= MAX(0, oi-edgM);
const T* Ad= A.data() + ai + Am*aj;
const T* Bd= B.data() + bi + Bm*bj;
for( ; bi >= 0 && ai < Am;
bi--,
Bd--,
ai++,
Ad++)
{
sum+= (*Ad) * (*Bd);
/* Comment: it seems to be 2.5 x faster than this:
* sum+= A(ai,aj) * B(bi,bj);
*/
}
}
O(oi,oj)= sum;
}
}
return O;
}
/*
%!test
%! b = [0,1,2,3;1,8,12,12;4,20,24,21;7,22,25,18];
%! assert(conv2([0,1;1,2],[1,2,3;4,5,6;7,8,9]),b);
*/
DEFUN_DLD (conv2, args, ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {y =} conv2 (@var{a}, @var{b}, @var{shape})\n\
@deftypefnx {Loadable Function} {y =} conv2 (@var{v1}, @var{v2}, @var{M}, @var{shape})\n\
\n\
Returns 2D convolution of @var{a} and @var{b} where the size\n\
of @var{c} is given by\n\
\n\
@table @asis\n\
@item @var{shape}= 'full'\n\
returns full 2-D convolution\n\
@item @var{shape}= 'same'\n\
same size as a. 'central' part of convolution\n\
@item @var{shape}= 'valid'\n\
only parts which do not include zero-padded edges\n\
@end table\n\
\n\
By default @var{shape} is 'full'. When the third argument is a matrix\n\
returns the convolution of the matrix @var{M} by the vector @var{v1}\n\
in the column direction and by vector @var{v2} in the row direction\n\
@end deftypefn")
{
octave_value_list retval;
octave_value tmp;
int nargin = args.length ();
string shape= "full"; //default
bool separable= false;
Shape ishape;
if (nargin < 2 )
{
print_usage ("conv2");
return retval;
}
else if (nargin == 3)
{
if ( args(2).is_string() )
shape= args(2).string_value();
else
separable= true;
}
else if (nargin >= 4)
{
separable= true;
shape= args(3).string_value();
}
if ( shape == "full" )
ishape = SHAPE_FULL;
else if ( shape == "same" )
ishape = SHAPE_SAME;
else if ( shape == "valid" )
ishape = SHAPE_VALID;
else
{
error("Shape type not valid");
print_usage ("conv2");
return retval;
}
if (separable)
{
/*
* If user requests separable, check first two params are vectors
*/
if (
!( 1== args(0).rows() || 1== args(0).columns() )
||
!( 1== args(1).rows() || 1== args(1).columns() ) )
{
print_usage ("conv2");
return retval;
}
if ( args(0).is_complex_type() ||
args(1).is_complex_type() ||
args(2).is_complex_type() )
{
ComplexColumnVector v1 (args(0).complex_vector_value());
ComplexColumnVector v2 (args(1).complex_vector_value());
ComplexMatrix a (args(2).complex_matrix_value());
ComplexMatrix c(conv2(v1, v2, a, ishape));
retval(0) = c;
}
else
{
ColumnVector v1 (args(0).vector_value());
ColumnVector v2 (args(1).vector_value());
Matrix a (args(2).matrix_value());
Matrix c(conv2(v1, v2, a, ishape));
retval(0) = c;
}
} // if (separable)
else
{
if ( args(0).is_complex_type() ||
args(1).is_complex_type())
{
ComplexMatrix a (args(0).complex_matrix_value());
ComplexMatrix b (args(1).complex_matrix_value());
ComplexMatrix c(conv2(a, b, ishape));
retval(0) = c;
}
else
{
Matrix a (args(0).matrix_value());
Matrix b (args(1).matrix_value());
Matrix c(conv2(a, b, ishape));
retval(0) = c;
}
} // if (separable)
return retval;
}
template MArray2<double>
conv2 (MArray<double>&, MArray<double>&, MArray2<double>&, Shape);
template MArray2<double>
conv2 (MArray2<double>&, MArray2<double>&, Shape);
template MArray2<Complex>
conv2 (MArray<Complex>&, MArray<Complex>&, MArray2<Complex>&, Shape);
template MArray2<Complex>
conv2 (MArray2<Complex>&, MArray2<Complex>&, Shape);