-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhadamard_decoder.hh
163 lines (154 loc) · 2.71 KB
/
hadamard_decoder.hh
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
/*
Soft decoder for augmented Hadamard codes
Copyright 2020 Ahmet Inan <[email protected]>
*/
#pragma once
namespace CODE {
template <int K>
class HadamardDecoder
{
static const int N = 1 << (K - 1);
public:
int operator()(const int8_t *code)
{
int sum[N];
for (int i = 0; i < N-1; i += 2) {
sum[i] = code[i] + code[i+1];
sum[i+1] = code[i] - code[i+1];
}
for (int h = 2; h < N; h *= 2) {
for (int i = 0; i < N; i += 2 * h) {
for (int j = i; j < i + h; ++j) {
int x = sum[j] + sum[j+h];
int y = sum[j] - sum[j+h];
sum[j] = x;
sum[j+h] = y;
}
}
}
int word = 0, best = 0, next = 0;
for (int i = 0; i < N; ++i) {
int mag = std::abs(sum[i]);
int msg = i + N * (sum[i] < 0);
if (mag > best) {
next = best;
best = mag;
word = msg;
} else if (mag > next) {
next = mag;
}
}
if (best == next)
return -1;
return word;
}
};
template <>
struct HadamardDecoder<2>
{
int operator()(const int8_t *code)
{
int x = code[0] + code[1];
int y = code[0] - code[1];
int mx = std::abs(x);
int my = std::abs(y);
if (mx == my)
return -1;
if (mx > my)
return 2 * (x < 0);
return 1 + 2 * (y < 0);
}
};
template <>
class HadamardDecoder<3>
{
static const int K = 3;
static const int N = 1 << (K - 1);
public:
int operator()(const int8_t *code)
{
int tmp[4] = {
code[0] + code[1],
code[0] - code[1],
code[2] + code[3],
code[2] - code[3],
};
int sum[4] = {
tmp[0] + tmp[2],
tmp[1] + tmp[3],
tmp[0] - tmp[2],
tmp[1] - tmp[3],
};
int word = 0, best = 0, next = 0;
for (int i = 0; i < N; ++i) {
int mag = std::abs(sum[i]);
int msg = i + N * (sum[i] < 0);
if (mag > best) {
next = best;
best = mag;
word = msg;
} else if (mag > next) {
next = mag;
}
}
if (best == next)
return -1;
return word;
}
};
template <>
class HadamardDecoder<4>
{
static const int K = 4;
static const int N = 1 << (K - 1);
public:
int operator()(const int8_t *c)
{
int d[8] = {
c[0] + c[1],
c[0] - c[1],
c[2] + c[3],
c[2] - c[3],
c[4] + c[5],
c[4] - c[5],
c[6] + c[7],
c[6] - c[7],
};
int e[8] = {
d[0] + d[2],
d[1] + d[3],
d[0] - d[2],
d[1] - d[3],
d[4] + d[6],
d[5] + d[7],
d[4] - d[6],
d[5] - d[7],
};
int sum[8] = {
e[0] + e[4],
e[1] + e[5],
e[2] + e[6],
e[3] + e[7],
e[0] - e[4],
e[1] - e[5],
e[2] - e[6],
e[3] - e[7],
};
int word = 0, best = 0, next = 0;
for (int i = 0; i < N; ++i) {
int mag = std::abs(sum[i]);
int msg = i + N * (sum[i] < 0);
if (mag > best) {
next = best;
best = mag;
word = msg;
} else if (mag > next) {
next = mag;
}
}
if (best == next)
return -1;
return word;
}
};
}