-
Notifications
You must be signed in to change notification settings - Fork 3
/
fwht.c
42 lines (36 loc) · 827 Bytes
/
fwht.c
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
#include "fwht.h"
void fwht_transform(int n, const int *src, int *dst)
{
int adata[n];
int bdata[n];
int *a = adata;
int *b = bdata;
void *tmp;
memcpy(a, src, sizeof(int)*n);
// Fast Walsh Hadamard Transform.
int i, j, s;
for (i = n>>1; i > 0; i>>=1) {
for (j = 0; j < n; j++) {
s = j/i%2;
b[j]=a[(s?-i:0)+j]+(s?-1:1)*a[(s?0:i)+j];
}
tmp = a; a = b; b = tmp;
}
memcpy(dst, a, sizeof(int)*n);
}
void fwht_normalize(int n, int *src)
{
int i;
for (i = 0; i < n; i++) src[i] /= n;
}
double fwht_sum_absolute_difference(int n, int *a, int *b)
{
long sum = 0;
int diff;
int i;
for (i = 0; i < n; i++) {
diff = b[i]-a[i];
sum += diff > 0 ? diff : -diff;
}
return sum/(double)n;
}