-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib_transforms.cpp
452 lines (410 loc) · 12.7 KB
/
lib_transforms.cpp
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright (c) 2011, Marc Lebrun <[email protected]>
* All rights reserved.
*
* This program is free software: you can use, modify and/or
* redistribute it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later
* version. You should have received a copy of this license along
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file lib_transforms.cpp
* @brief 1D and 2D wavelet transforms
*
* @author Marc Lebrun <[email protected]>
**/
#include "lib_transforms.h"
#include <math.h>
#include <numeric>
using namespace std;
/**
* @brief Compute a full 2D Bior 1.5 spline wavelet (normalized)
*
* @param input: vector on which the transform will be applied;
* @param output: will contain the result;
* @param N: size of the 2D patch (N x N) on which the 2D transform
* is applied. Must be a power of 2;
* @param d_i: for convenience. Shift for input to access to the patch;
* @param r_i: for convenience. input(i, j) = input[d_i + i * r_i + j];
* @param d_o: for convenience. Shift for output;
* @param lpd: low frequencies coefficients for the forward Bior 1.5;
* @param hpd: high frequencies coefficients for the forward Bior 1.5.
*
* @return none.
**/
void bior_2d_forward(
vector<float> const& input
, vector<float> &output
, const unsigned N
, const unsigned d_i
, const unsigned r_i
, const unsigned d_o
, vector<float> const& lpd
, vector<float> const& hpd
){
//! Initializing output
for (unsigned i = 0; i < N; i++)
for (unsigned j = 0; j < N; j++)
output[i * N + j + d_o] = input[i * r_i + j + d_i];
const unsigned iter_max = log2(N);
unsigned N_1 = N;
unsigned N_2 = N / 2;
const unsigned S_1 = lpd.size();
const unsigned S_2 = S_1 / 2 - 1;
for (unsigned iter = 0; iter < iter_max; iter++)
{
//! Periodic extension index initialization
vector<float> tmp(N_1 + 2 * S_2);
vector<unsigned> ind_per(N_1 + 2 * S_2);
per_ext_ind(ind_per, N_1, S_2);
//! Implementing row filtering
for (unsigned i = 0; i < N_1; i++)
{
//! Periodic extension of the signal in row
for (unsigned j = 0; j < tmp.size(); j++)
tmp[j] = output[d_o + i * N + ind_per[j]];
//! Low and High frequencies filtering
for (unsigned j = 0; j < N_2; j++)
{
float v_l = 0.0f, v_h = 0.0f;
for (unsigned k = 0; k < S_1; k++)
{
v_l += tmp[k + j * 2] * lpd[k];
v_h += tmp[k + j * 2] * hpd[k];
}
output[d_o + i * N + j] = v_l;
output[d_o + i * N + j + N_2] = v_h;
}
}
//! Implementing column filtering
for (unsigned j = 0; j < N_1; j++)
{
//! Periodic extension of the signal in column
for (unsigned i = 0; i < tmp.size(); i++)
tmp[i] = output[d_o + j + ind_per[i] * N];
//! Low and High frequencies filtering
for (unsigned i = 0; i < N_2; i++)
{
float v_l = 0.0f, v_h = 0.0f;
for (unsigned k = 0; k < S_1; k++)
{
v_l += tmp[k + i * 2] * lpd[k];
v_h += tmp[k + i * 2] * hpd[k];
}
output[d_o + j + i * N] = v_l;
output[d_o + j + (i + N_2) * N] = v_h;
}
}
//! Sizes update
N_1 /= 2;
N_2 /= 2;
}
}
void bior_2d_forward_test(
vector<float> const& input
, vector<float> &output
, const unsigned N
, const unsigned d_i
, const unsigned r_i
, const unsigned d_o
, vector<float> const& lpd
, vector<float> const& hpd
, vector<float> &tmp
, vector<unsigned> &ind_per
){
//! Initializing output
for (unsigned i = 0; i < N; i++)
for (unsigned j = 0; j < N; j++)
output[i * N + j + d_o] = input[i * r_i + j + d_i];
const unsigned iter_max = log2(N);
unsigned N_1 = N;
unsigned N_2 = N / 2;
const unsigned S_1 = lpd.size();
const unsigned S_2 = S_1 / 2 - 1;
for (unsigned iter = 0; iter < iter_max; iter++)
{
//! Periodic extension index initialization
// vector<float> tmp(N_1 + 2 * S_2);
// vector<unsigned> ind_per(N_1 + 2 * S_2);
per_ext_ind(ind_per, N_1, S_2);
//! Implementing row filtering
for (unsigned i = 0; i < N_1; i++)
{
//! Periodic extension of the signal in row
for (unsigned j = 0; j < tmp.size(); j++)
tmp[j] = output[d_o + i * N + ind_per[j]];
//! Low and High frequencies filtering
for (unsigned j = 0; j < N_2; j++)
{
float v_l = 0.0f, v_h = 0.0f;
for (unsigned k = 0; k < S_1; k++)
{
v_l += tmp[k + j * 2] * lpd[k];
v_h += tmp[k + j * 2] * hpd[k];
}
output[d_o + i * N + j] = v_l;
output[d_o + i * N + j + N_2] = v_h;
// output[d_o + i * N + j] = inner_product(tmp.begin() + j * 2, tmp.begin() + j * 2 + S_1, lpd.begin(), 0.f);
// output[d_o + i * N + j + N_2] = inner_product(tmp.begin() + j * 2, tmp.begin() + j * 2 + S_1, hpd.begin(), 0.f);
}
}
//! Implementing column filtering
for (unsigned j = 0; j < N_1; j++)
{
//! Periodic extension of the signal in column
for (unsigned i = 0; i < tmp.size(); i++)
tmp[i] = output[d_o + j + ind_per[i] * N];
//! Low and High frequencies filtering
for (unsigned i = 0; i < N_2; i++)
{
float v_l = 0.0f, v_h = 0.0f;
for (unsigned k = 0; k < S_1; k++)
{
v_l += tmp[k + i * 2] * lpd[k];
v_h += tmp[k + i * 2] * hpd[k];
}
output[d_o + j + i * N] = v_l;
output[d_o + j + (i + N_2) * N] = v_h;
// output[d_o + j + i * N] = inner_product(tmp.begin() + i * 2, tmp.begin() + i * 2 + S_1, lpd.begin(), 0.f);
// output[d_o + j + (i + N_2) * N] = inner_product(tmp.begin() + i * 2, tmp.begin() + i * 2 + S_1, hpd.begin(), 0.f);
}
}
//! Sizes update
N_1 /= 2;
N_2 /= 2;
}
}
/**
* @brief Compute a full 2D Bior 1.5 spline wavelet inverse (normalized)
*
* @param signal: vector on which the transform will be applied; It
* will contain the result at the end;
* @param N: size of the 2D patch (N x N) on which the 2D transform
* is applied. Must be a power of 2;
* @param d_s: for convenience. Shift for signal to access to the patch;
* @param lpr: low frequencies coefficients for the inverse Bior 1.5;
* @param hpr: high frequencies coefficients for the inverse Bior 1.5.
*
* @return none.
**/
void bior_2d_inverse(
vector<float> &signal
, const unsigned N
, const unsigned d_s
, vector<float> const& lpr
, vector<float> const& hpr
){
//! Initialization
const unsigned iter_max = log2(N);
unsigned N_1 = 2;
unsigned N_2 = 1;
const unsigned S_1 = lpr.size();
const unsigned S_2 = S_1 / 2 - 1;
for (unsigned iter = 0; iter < iter_max; iter++)
{
vector<float> tmp(N_1 + S_2 * N_1);
vector<unsigned> ind_per(N_1 + 2 * S_2 * N_2);
per_ext_ind(ind_per, N_1, S_2 * N_2);
//! Implementing column filtering
for (unsigned j = 0; j < N_1; j++)
{
//! Periodic extension of the signal in column
for (unsigned i = 0; i < tmp.size(); i++)
tmp[i] = signal[d_s + j + ind_per[i] * N];
//! Low and High frequencies filtering
for (unsigned i = 0; i < N_2; i++)
{
float v_l = 0.0f, v_h = 0.0f;
for (unsigned k = 0; k < S_1; k++)
{
v_l += lpr[k] * tmp[k * N_2 + i];
v_h += hpr[k] * tmp[k * N_2 + i];
}
signal[d_s + i * 2 * N + j] = v_h;
signal[d_s + (i * 2 + 1) * N + j] = v_l;
}
}
//! Implementing row filtering
for (unsigned i = 0; i < N_1; i++)
{
//! Periodic extension of the signal in row
for (unsigned j = 0; j < tmp.size(); j++)
tmp[j] = signal[d_s + i * N + ind_per[j]];
//! Low and High frequencies filtering
for (unsigned j = 0; j < N_2; j++)
{
float v_l = 0.0f, v_h = 0.0f;
for (unsigned k = 0; k < S_1; k++)
{
v_l += lpr[k] * tmp[k * N_2 + j];
v_h += hpr[k] * tmp[k * N_2 + j];
}
signal[d_s + i * N + j * 2] = v_h;
signal[d_s + i * N + j * 2 + 1] = v_l;
}
}
//! Sizes update
N_1 *= 2;
N_2 *= 2;
}
}
/**
* @brief Initialize forward and backward low and high filter
* for a Bior1.5 spline wavelet.
*
* @param lp1: low frequencies forward filter;
* @param hp1: high frequencies forward filter;
* @param lp2: low frequencies backward filter;
* @param hp2: high frequencies backward filter.
**/
void bior15_coef(
vector<float> &lp1
, vector<float> &hp1
, vector<float> &lp2
, vector<float> &hp2
){
const float coef_norm = 1.f / (sqrtf(2.f) * 128.f);
const float sqrt2_inv = 1.f / sqrtf(2.f);
lp1.resize(10);
lp1[0] = 3.f ;
lp1[1] = -3.f ;
lp1[2] = -22.f ;
lp1[3] = 22.f ;
lp1[4] = 128.f;
lp1[5] = 128.f;
lp1[6] = 22.f ;
lp1[7] = -22.f ;
lp1[8] = -3.f ;
lp1[9] = 3.f ;
hp1.resize(10);
hp1[0] = 0.f;
hp1[1] = 0.f;
hp1[2] = 0.f;
hp1[3] = 0.f;
hp1[4] = -sqrt2_inv;
hp1[5] = sqrt2_inv;
hp1[6] = 0.f;
hp1[7] = 0.f;
hp1[8] = 0.f;
hp1[9] = 0.f;
lp2.resize(10);
lp2[0] = 0.f;
lp2[1] = 0.f;
lp2[2] = 0.f;
lp2[3] = 0.f;
lp2[4] = sqrt2_inv;
lp2[5] = sqrt2_inv;
lp2[6] = 0.f;
lp2[7] = 0.f;
lp2[8] = 0.f;
lp2[9] = 0.f;
hp2.resize(10);
hp2[0] = 3.f ;
hp2[1] = 3.f ;
hp2[2] = -22.f ;
hp2[3] = -22.f ;
hp2[4] = 128.f;
hp2[5] = -128.f;
hp2[6] = 22.f ;
hp2[7] = 22.f ;
hp2[8] = -3.f ;
hp2[9] = -3.f ;
for (unsigned k = 0; k < 10; k++)
{
lp1[k] *= coef_norm;
hp2[k] *= coef_norm;
}
}
/**
* @brief Apply Welsh-Hadamard transform on vec (non normalized !!)
*
* @param vec: vector on which a Hadamard transform will be applied.
* It will contain the transform at the end;
* @param tmp: must have the same size as vec. Used for convenience;
* @param N, d: the Hadamard transform will be applied on vec[d] -> vec[d + N].
* N must be a power of 2!!!!
*
* @return None.
**/
void hadamard_transform(
vector<float> &vec
, vector<float> &tmp
, const unsigned N
, const unsigned D
){
if (N == 1)
return;
else if (N == 2)
{
const float a = vec[D + 0];
const float b = vec[D + 1];
vec[D + 0] = a + b;
vec[D + 1] = a - b;
}
else
{
const unsigned n = N / 2;
for (unsigned k = 0; k < n; k++)
{
const float a = vec[D + 2 * k];
const float b = vec[D + 2 * k + 1];
vec[D + k] = a + b;
tmp[k] = a - b;
}
for (unsigned k = 0; k < n; k++)
vec[D + n + k] = tmp[k];
hadamard_transform(vec, tmp, n, D);
hadamard_transform(vec, tmp, n, D + n);
}
}
/**
* @brief Obtain the ceil of log_2(N)
*
* @param N: in the case N = 2^n, return n.
*
* @return n;
**/
unsigned log2(
const unsigned N
){
unsigned k = 1;
unsigned n = 0;
while (k < N)
{
k *= 2;
n++;
}
return n;
}
/**
* @brief Obtain index for periodic extension.
*
* @param ind_per: will contain index. Its size must be N + 2 * L;
* @param N: size of the original signal;
* @param L: size of boundaries to add on each side of the signal.
*
* @return none.
**/
void per_ext_ind(
vector<unsigned> &ind_per
, const unsigned N
, const unsigned L
){
for (unsigned k = 0; k < N; k++)
ind_per[k + L] = k;
int ind1 = (N - L);
while (ind1 < 0)
ind1 += N;
unsigned ind2 = 0;
unsigned k = 0;
while(k < L)
{
ind_per[k] = (unsigned) ind1;
ind_per[k + L + N] = ind2;
ind1 = ((unsigned) ind1 < N - 1 ? (unsigned) ind1 + 1 : 0);
ind2 = (ind2 < N - 1 ? ind2 + 1 : 0);
k++;
}
}