-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfwht.h
232 lines (188 loc) · 4.8 KB
/
fwht.h
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
#ifndef __FWHT__
#define __FWHT__
//================================================
// @title fwht.h
// @author Jonathan Hadida
// @contact Jonathan.hadida [at] dtc.ox.ac.uk
//================================================
#include <cmath>
#include <vector>
#include <cstdio>
#include <iostream>
#include <stdexcept>
#define for_i(n) for ( unsigned i=0; i<n; ++i )
/******************** ********** ********************/
/******************** ********** ********************/
/**
* Count the number of bits set.
*/
template <typename IntType>
unsigned bit_count( IntType n )
{
unsigned c;
for (c = 0; n; ++c)
n &= n - 1;
return c;
}
// ------------------------------------------------------------------------
/**
* Reverse the bits (left-right) of an integer.
*/
template <typename IntType>
IntType bit_reverse( IntType n, unsigned nbits )
{
IntType r = n;
IntType l, m;
// Compute shift and bitmask
l = (IntType) (nbits - 1);
m = (1 << nbits) - 1;
// Permute
while ( n >>= 1 )
{
r <<= 1;
r |= n & 1;
--l;
}
// Final shift and masking
r <<= l;
return r & m;
}
template <typename IntType>
inline IntType bit_reverse( IntType n )
{
return bit_reverse( n, 8*sizeof(n) );
}
// ------------------------------------------------------------------------
/**
* Print the bits of an integer.
*/
template <typename IntType>
void bit_print( IntType n )
{
static const unsigned b = 8*sizeof(IntType);
static char buf[b+1]; buf[b] = '\0';
std::cout<< n;
for (unsigned m = b; m; n >>= 1, --m)
buf[m-1] = (n & 1) + '0';
printf(" = %s\n",buf);
}
// ------------------------------------------------------------------------
/**
* Gray codes transforms.
*/
template <typename IntType>
inline IntType bin2gray( IntType n )
{
return (n >> 1) ^ n;
}
template <typename IntType>
IntType gray2bin( IntType n )
{
for ( IntType m = n; m >>= 1; n = n ^ m );
return n;
}
// ------------------------------------------------------------------------
/**
* Integer logarithm base 2.
*/
template <typename IntType>
unsigned ilog2( IntType n )
{
unsigned l;
for ( l = 0; n; n >>= 1, ++l );
return l;
}
template <typename IntType>
inline bool is_pow2( IntType n )
{
return (n > 1) && ((n & (n-1)) == 0);
}
// ------------------------------------------------------------------------
/**
* Orthogonal transformation.
*/
template <typename T>
void rotate( T& a, T& b )
{
static T A;
A = a;
a = A + b;
b = A - b;
}
// ------------------------------------------------------------------------
/**
* Compute the permutation of WH coefficients to sort them by sequency.
*/
template <typename IntType>
void fwht_sequency_permutation( std::vector<IntType>& perm, unsigned order )
{
if ( perm.size() == (size_t)(1 << order) ) return;
perm.resize(1 << order); IntType k = 0;
for ( auto& p: perm ) p = bit_reverse(bin2gray(k++),order);
}
// ------------------------------------------------------------------------
/**
* Fast Walsh-Hadamard transform.
*/
template <typename T>
void fwht( T *data, unsigned n,
bool sequency_ordered = false,
bool normalize = true )
{
// Require n to be a power of 2
unsigned l2 = ilog2(n) - 1;
if ( n != (unsigned)(1<<l2) )
throw std::length_error("Data length should be a power of 2.");
// Normalizer
const unsigned norm = normalize ? n : 1;
// Compute the WHT
for ( unsigned i = 0; i < l2; ++i )
{
for ( unsigned j = 0; j < n; j += (1 << (i+1)) )
for ( unsigned k = 0; k < (unsigned)(1 << i); ++k )
rotate( data[j+k], data[j+ k + (unsigned)(1<<i)] );
}
static std::vector<unsigned> perm;
if ( sequency_ordered )
{
// Compute permutation
fwht_sequency_permutation( perm, l2 );
// Copy transform
std::vector<T> tmp( data, data+n );
for_i(n) data[i] = tmp[perm[i]] / norm;
}
else if ( normalize )
for_i(n) data[i] /= norm;
}
// ------------------------------------------------------------------------
template <typename T>
void ifwht( T *data, unsigned n,
bool sequency_ordered = false )
{
fwht( data, n, sequency_ordered, false );
}
// ------------------------------------------------------------------------
template <typename T>
void fwht( std::vector<T>& data, bool sequency_ordered = false )
{
// Round to the next power of 2
unsigned n = data.size();
unsigned l2 = ilog2(n) - is_pow2(n);
unsigned N = 1 << l2;
// 0-padding to the next power of 2
data.resize( N, T(0) );
fwht( data.data(), N, sequency_ordered );
}
// ------------------------------------------------------------------------
template <typename T>
void ifwht( std::vector<T>& data, bool sequency_ordered = false )
{
// Round to the next power of 2
unsigned n = data.size();
unsigned l2 = ilog2(n) - is_pow2(n);
unsigned N = 1 << l2;
// 0-padding to the next power of 2
data.resize( N, T(0) );
ifwht( data.data(), N, sequency_ordered );
}
#endif