-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering.hpp
169 lines (150 loc) · 5.18 KB
/
filtering.hpp
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
/**
* @file filtering.hpp
* @brief Convolution and digital filtering functions.
* @author seonho.oh@gmail.com
* @date 2013-10-07
* @version 1.0
*
* @section LICENSE
*
* Copyright (c) 2013-2015, Seonho Oh
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <ORGANIZATION> nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace arma_ext
{
using namespace arma;
//! @addtogroup foufilt
//! @{
/**
* Convolution types
*/
#ifdef ARMA_EXT_USE_CPP11
enum convolution_type : uword
#else
enum convolution_type
#endif
{
full, //! The full two-dimensional convolution (default).
same, //! The central part of the convolution of the same size as @c A.
valid //! Only those parts of the convolution that are computed without the zero-padded edges.<br>Using this option, #size (C) = max([ma - max(0, mb - 1), na - max(0, nb - 1)], 0).
};
/**
* @brief An arma style intermediate interface class implementation of 2D convolution operation
* @see conv2
*/
class glue_conv2
{
public:
/**
* @brief A rudimentary implementation of the 2D convolution operation.
* @param out The convolution result.
* @param X The second parameter of convolution.
*/
template <typename T1, typename T2>
inline static void apply(Mat<typename T1::elem_type>&out, const Glue<T1, T2, glue_conv2>& X)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type elem_type;
const Mat<elem_type>& a = X.A;
const Mat<elem_type>& b = X.B;
uword ma = a.n_rows, na = a.n_cols;
uword mb = b.n_rows, nb = b.n_cols;
uword mc = ma + mb - 1, nc = na + nb - 1;
out.set_size(mc, nc);
#if defined(USE_PPL)
concurrency::parallel_for(uword(0), nc, [&](uword c) {
#elif defined(USE_OPENMP)
#pragma omp parallel for
for (int sc = 0 ; sc < (int)nc ; sc++) {
uword c = (uword)sc;
#else
for (uword c = 0 ; c < nc ; c++) {
#endif
elem_type* outptr = out.colptr(c);
for (uword r = 0 ; r < mc ; r++) {
elem_type value = 0;
const uword minu = (r + 1 > mb) ? r - mb + 1 : 0;
const uword maxu = std::min(ma - 1, r);
const uword minv = (c + 1 > nb) ? c - nb + 1 : 0;
const uword maxv = std::min(na - 1, c);
for (uword v = minv ; v <= maxv ; v++) {
const elem_type* aptr = a.colptr(v);
const elem_type* bptr = b.colptr(c - v);
for (uword u = minu ; u <= maxu ; u++) {
//value += a.at(u, v) * b.at(r - u, c - v);
value += aptr[u] * bptr[r - u];
}
}
//out.at(r, c) = value;
outptr[r] = value;
}
#ifdef USE_PPL
});
#else
}
#endif
switch (X.aux_uword) {
case full:
// do nothing
break;
case valid:
out = out(span(mb - 1,ma - 1), span(nb - 1,na - 1));
break;
case same:
{
uword r1 = (uword)std::floor(mb / 2.0); // zero-begin index corrected
uword r2 = r1 + ma - 1;
uword c1 = (uword)std::floor(nb / 2.0); // zero-begin index corrected
uword c2 = c1 + na - 1;
out = out(span(r1, r2), span(c1, c2));
}
break;
default:
break;
}
}
};
/**
* @brief 2-D convolution of matrices A and B.
* @param A The input matrix.
* @param B The convolution kernel matrix.
* @param conv_type The convolution type
* @return convolution The result matrix.
* @see http://www.mathworks.co.kr/kr/help/matlab/ref/conv2.html
* @see convolution_type
* @note This function is preliminary; it is not yet fully optimized.
*/
template <typename T1, typename T2>
inline const Glue<T1, T2, glue_conv2> conv2(const Base<typename T1::elem_type, T1>& A, const Base<typename T1::elem_type, T2>& B, const uword conv_type = full)
{
arma_extra_debug_sigprint();
return Glue<T1, T2, glue_conv2>(A.get_ref(), B.get_ref(), conv_type);
}
//! @}
}