-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconvolution.cpp
More file actions
358 lines (283 loc) · 10.4 KB
/
convolution.cpp
File metadata and controls
358 lines (283 loc) · 10.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
/*
RealLib, a library for efficient exact real computation
Copyright (C) 2006 Branimir Lambov
This library is licensed under the Apache License, Version 2.0 (the "License");
you may not use this library except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "defs.h"
#include "convolution.h"
#include <math.h>
#include <assert.h>
//using namespace std;
#define realindex(i) ((i)*2)
#define imagindex(i) ((i)*2+1)
// forward real-to-complex in-place fft
template <class TYPE>
void fft_fwd_ip_rc(int size, TYPE *weights, int *br, TYPE *data, int wstride);
// inverse complex-to-real in-place fft
template <class TYPE>
void fft_inv_ip_cr(int size, TYPE *weights, int *br, TYPE *data, int wstride);
// complex multiplication of vector
template <class TYPE>
void mul_complex(int size, TYPE *a, TYPE *b, double scale);
// returns the reversed bits of z for size = 2 ^ bits
static int bitreverse(int z, int bits, int size)
{
int r = 0;
for (int i=0; i<bits; ++i) {
size >>= 1;
if (z & 1) r += size;
z >>= 1;
}
return r;
}
// constructor
template <class TYPE>
Convolution<TYPE>::Convolution(int size, TYPE PI2)
: m_Size(size)
{
// verify size is correct. should be a power of 2
assert ((size & (size - 1)) == 0);
int bits = int(log(double(size)) / log(2.0) + 0.5);
// allocate memory
m_pWeights = new TYPE [size*2];
m_pBR = new int [size/2];
assert(m_pWeights && m_pBR);
// fill in weights and bit reverse vector
int i;
for (i=0; i<size/2; ++i) {
m_pWeights[realindex(i)] = cos(PI2 * i / size);
m_pWeights[imagindex(i)] = -sin(PI2 * i / size);
m_pBR[i] = bitreverse(i, bits-1, size/2);
}
}
// destructor
template <class TYPE>
Convolution<TYPE>::~Convolution()
{
delete [] m_pWeights;
delete [] m_pBR;
}
#ifndef __RESTRICT
#define restrict
#endif
// convolve performs the operation.
// output goes to a. b is destroyed
template <class TYPE>
void Convolution<TYPE>::Convolve(TYPE * restrict a, TYPE * restrict b, int size)
{
if (size==0) size = m_Size;
// forward ffts. remember rc multiplies both by additional factor of 2
int wstride = m_Size / size;
fft_fwd_ip_rc(size, m_pWeights, m_pBR, a, wstride);
fft_fwd_ip_rc(size, m_pWeights, m_pBR, b, wstride);
// DC and Nyguest share one complex value
// should be multiplied separately
a[0] *= b[0] / (size * 4);
a[1] *= b[1] / (size * 4);
mul_complex(size/2-1, a+2, b+2, 1.0 / (size * 4));
// inverse fft
fft_inv_ip_cr(size, m_pWeights, m_pBR, a, wstride);
}
// multiply two complex vectors, in-place
template <class TYPE>
void mul_complex(int size, TYPE * restrict a, TYPE * restrict b, double scale)
{
for (int i=0; i<size; ++i) {
TYPE re(a[realindex(i)]*b[realindex(i)] - a[imagindex(i)]*b[imagindex(i)]);
TYPE im(a[realindex(i)]*b[imagindex(i)] + a[imagindex(i)]*b[realindex(i)]);
a[realindex(i)] = scale * re;
a[imagindex(i)] = scale * im;
}
}
// gentleman-sande decimation-in-frequency forward in-place fft
template <class TYPE>
void fft_fwd_ip(int size, TYPE * restrict weights, TYPE * restrict a, int wstride)
{
for (int L=size; L>1; L >>= 1) {
int r = size / L;
int L2 = L >> 1;
for (int j=0; j<L2; ++j) {
TYPE wr = weights[realindex(j * r * wstride)];
TYPE wi = weights[imagindex(j * r * wstride)];
for (int k=0; k<r; ++k) {
TYPE cr = a[realindex(k * L + j)];
TYPE ci = a[imagindex(k * L + j)];
TYPE dr = a[realindex(k * L + L2 + j)];
TYPE di = a[imagindex(k * L + L2 + j)];
a[realindex(k * L + j)] = cr + dr;
a[imagindex(k * L + j)] = ci + di;
cr -= dr;
ci -= di;
a[realindex(k * L + L2 + j)] = wr * cr - wi * ci;
a[imagindex(k * L + L2 + j)] = wr * ci + wi * cr;
}
}
}
// permutation is skipped
}
// cooley-tukey decimation-in-time inverse in-place fft
template <class TYPE>
void fft_inv_ip(int size, TYPE * restrict weights, TYPE *restrict a, int wstride)
{
// permutation is skipped
for (int L = 2; L <= size; L <<= 1) {
int r = size / L;
int L2 = L / 2;
for (int j=0; j<L2; ++j) {
TYPE wr = weights[realindex(j * r * wstride)];
TYPE wi = -weights[imagindex(j * r * wstride)]; // inverse
for (int k=0; k<r; ++k) {
TYPE cr = a[realindex(k * L + j)];
TYPE ci = a[imagindex(k * L + j)];
TYPE dr = a[realindex(k * L + L2 + j)];
TYPE di = a[imagindex(k * L + L2 + j)];
TYPE tr = wr * dr - wi * di;
TYPE ti = wr * di + wi * dr;
a[realindex(k * L + j)] = cr + tr;
a[imagindex(k * L + j)] = ci + ti;
a[realindex(k * L + L2 + j)] = cr - tr;
a[imagindex(k * L + L2 + j)] = ci - ti;
}
}
}
}
// real-to-complex step after fft_fwd
// the result is multiplied by 2
template <class TYPE>
void fft_realtocomplex(int size, TYPE * restrict weights, int * restrict br, TYPE * restrict a, int wstride)
{
int size2 = size/2;
TYPE pr, pi, mr, mi;
int i, j;
// calculate DC and Nyguest (the value at the center frequency)
// both are real numbers, to avoid needing extra space they share one
// complex point
pr = a[realindex(0)]; pi = a[imagindex(0)];
a[realindex(0)] = (pr + pi) * 2.0;
a[imagindex(0)] = (pr - pi) * 2.0;
// this is in the middle, bitreverse(size/2) == 1
mr = a[realindex(1)]; mi = a[imagindex(1)];
a[realindex(1)] = mr * 2.0;
a[imagindex(1)] = mi * -2.0;
// from here on, indexes are retrieved bitreversed
// br(i*wstride) is the proper br(i) when the size is divided by wstride
for (i=wstride, j=(size-1)*wstride; i<size2*wstride; (i+=wstride), (j-=wstride)) {
pr = a[realindex(br[i])] + a[realindex(br[j])];
pi = a[imagindex(br[i])] + a[imagindex(br[j])];
mr = a[realindex(br[i])] - a[realindex(br[j])];
mi = a[imagindex(br[i])] - a[imagindex(br[j])];
a[realindex(br[i])] = pr + weights[realindex(i)] * pi + weights[imagindex(i)] * mr;
a[imagindex(br[i])] = mi - weights[realindex(i)] * mr + weights[imagindex(i)] * pi;
a[realindex(br[j])] = pr - weights[realindex(i)] * pi - weights[imagindex(i)] * mr;
a[imagindex(br[j])] = -mi - weights[realindex(i)] * mr + weights[imagindex(i)] * pi;
}
}
// complex-to-real step before fft_inv
template <class TYPE>
void fft_complextoreal(int size, TYPE * restrict weights, int * restrict br, TYPE * restrict a, int wstride)
{
int size2 = size/2;
TYPE pr, pi, mr, mi, zr, zi;
int i, j;
// DC and Nyquest were calculated using a different formula
pr = a[realindex(0)]; pi = a[imagindex(0)];
a[realindex(0)] = (pr + pi);
a[imagindex(0)] = (pr - pi);
// this is in the middle, bitreverse(size/2) == 1
mr = a[realindex(1)];
mi = a[imagindex(1)];
a[realindex(1)] = mr * 2.0;
a[imagindex(1)] = mi * -2.0;
// from here on, indexes are retrieved bitreversed
for (i=wstride, j=(size-1)*wstride; i<size2*wstride; (i+=wstride), (j-=wstride)) {
pr = a[realindex(br[i])] + a[realindex(br[j])];
pi = a[imagindex(br[i])] - a[imagindex(br[j])];
mi = a[realindex(br[i])] - a[realindex(br[j])];
mr = a[imagindex(br[i])] + a[imagindex(br[j])];
zr = mr * weights[realindex(i)] - mi * weights[imagindex(i)];
zi = mi * weights[realindex(i)] + mr * weights[imagindex(i)];
a[realindex(br[i])] = pr - zr;
a[imagindex(br[i])] = pi + zi;
a[realindex(br[j])] = pr + zr;
a[imagindex(br[j])] = zi - pi;
}
}
template <class TYPE>
void fft_fwd_ip_rc(int size, TYPE * restrict weights, int * restrict br, TYPE * restrict a, int wstride)
{
// perform a complex-to-complex fft on the data
fft_fwd_ip(size / 2, weights, a, 2*wstride);
// then use an additional step to get the actual result
fft_realtocomplex(size / 2, weights, br, a, wstride);
}
template <class TYPE>
void fft_inv_ip_cr(int size, TYPE * restrict weights, int * restrict br, TYPE * restrict a, int wstride)
{
// revert the operation of fft_realtocomplex
fft_complextoreal(size / 2, weights, br, a, wstride);
// perform a complex-to-complex fft
fft_inv_ip(size / 2, weights, a, 2*wstride);
}
// instantiate
template
class Convolution<double>;
/*
test code
#include <stdio.h>
void main()
{
#define size 128
#define br(x) conv.m_pBR[x]
Convolution<double> conv(size*2);
double a[size*2];
int i;
for (i=0; i<size; ++i) {
a[realindex(i)] = i + 1;
a[imagindex(i)] = -i;
}
for (i=0; i<size; ++i) {
printf("%3x: %lf+i%lf\n", i, a[realindex(i)], a[imagindex(i)]);
}
getchar();
for (i=0; i<size; ++i) {
printf("weights %3x: %lf+i%lf\n", i, conv.m_pWeights[realindex(i)], conv.m_pWeights[imagindex(i)]);
}
getchar();
fft_fwd_ip(size, conv.m_pWeights, a, 2);
for (i=0; i<size; ++i) {
printf("%3x, %3x: %lf+i%lf\n", i, br(i), a[realindex(br(i))], a[imagindex(br(i))]);
}
getchar();
fft_inv_ip(size, conv.m_pWeights, a, 2);
for (i=0; i<size; ++i) {
printf("%3x: %lf+i%lf\n", i, a[realindex(i)]/size, a[imagindex(i)]/size);
}
getchar();
int sizer = size*2;
for (i=0; i<sizer; ++i) a[i] = i;
for (i=0; i<sizer; ++i) printf("%3x: %lf\n", i, a[i]);
getchar();
fft_fwd_ip_rc(sizer, conv.m_pWeights, conv.m_pBR, a);
for (i=0; i<size; ++i) {
printf("%3x, %3x: %lf+i%lf\n", i, br(i), a[realindex(br(i))], a[imagindex(br(i))]);
}
getchar();
fft_inv_ip_cr(sizer, conv.m_pWeights, conv.m_pBR, a);
for (i=0; i<sizer; ++i) printf("%3x: %lf\n", i, a[i]/(sizer*2));
getchar();
double b[size*2];
b[0] = 1; b[1] = 1;
for (i=2;i<sizer;++i) b[i] = 0;
conv.Convolve(a, b);
for (i=0; i<sizer; ++i) printf("%3x: %lf\n", i, a[i]/(sizer*2));
getchar();
}
*/