forked from xdsopl/LDPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qam.hh
254 lines (221 loc) · 8.65 KB
/
qam.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
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
/*
Quadrature amplitude modulation
Copyright 2018 Ahmet Inan <[email protected]>
*/
#ifndef QAM_HH
#define QAM_HH
template <int NUM, typename TYPE, typename CODE>
struct QuadratureAmplitudeModulation;
template <typename TYPE, typename CODE>
struct QuadratureAmplitudeModulation<16, TYPE, CODE>
{
static const int NUM = 16;
static const int BITS = 4;
typedef TYPE complex_type;
typedef typename TYPE::value_type value_type;
typedef CODE code_type;
static constexpr value_type FAC = 1.0540925533894596;
static constexpr value_type RCP = 3 * FAC;
static constexpr value_type AMP = 1 / RCP;
static constexpr value_type DIST = 2 * AMP;
static constexpr value_type amp(int i)
{
return AMP * i;
}
static code_type quantize(value_type precision, value_type value)
{
value *= DIST * precision;
if (std::is_integral<code_type>::value)
value = std::nearbyint(value);
if (std::is_same<code_type, int8_t>::value)
value = std::min<value_type>(std::max<value_type>(value, -128), 127);
return value;
}
static void hard(code_type *b, complex_type c)
{
b[0] = c.real() < amp(0) ? code_type(-1) : code_type(1);
b[1] = c.imag() < amp(0) ? code_type(-1) : code_type(1);
b[2] = std::abs(c.real()) < amp(2) ? code_type(-1) : code_type(1);
b[3] = std::abs(c.imag()) < amp(2) ? code_type(-1) : code_type(1);
}
static void soft(code_type *b, complex_type c, value_type precision)
{
b[0] = quantize(precision, c.real());
b[1] = quantize(precision, c.imag());
b[2] = quantize(precision, std::abs(c.real())-amp(2));
b[3] = quantize(precision, std::abs(c.imag())-amp(2));
}
static complex_type map(code_type *b)
{
return AMP * complex_type(
b[0]*(b[2]+value_type(2)),
b[1]*(b[3]+value_type(2))
);
}
};
template <typename TYPE, typename CODE>
struct QuadratureAmplitudeModulation<64, TYPE, CODE>
{
static const int NUM = 64;
static const int BITS = 6;
typedef TYPE complex_type;
typedef typename TYPE::value_type value_type;
typedef CODE code_type;
static constexpr value_type FAC = 0.9258200997725516;
static constexpr value_type RCP = 7 * FAC;
static constexpr value_type AMP = 1 / RCP;
static constexpr value_type DIST = 2 * AMP;
static constexpr value_type amp(int i)
{
return AMP * i;
}
static code_type quantize(value_type precision, value_type value)
{
value *= DIST * precision;
if (std::is_integral<code_type>::value)
value = std::nearbyint(value);
if (std::is_same<code_type, int8_t>::value)
value = std::min<value_type>(std::max<value_type>(value, -128), 127);
return value;
}
static void hard(code_type *b, complex_type c)
{
b[0] = c.real() < amp(0) ? code_type(-1) : code_type(1);
b[1] = c.imag() < amp(0) ? code_type(-1) : code_type(1);
b[2] = std::abs(c.real()) < amp(4) ? code_type(-1) : code_type(1);
b[3] = std::abs(c.imag()) < amp(4) ? code_type(-1) : code_type(1);
b[4] = std::abs(std::abs(c.real())-amp(4)) < amp(2) ? code_type(-1) : code_type(1);
b[5] = std::abs(std::abs(c.imag())-amp(4)) < amp(2) ? code_type(-1) : code_type(1);
}
static void soft(code_type *b, complex_type c, value_type precision)
{
b[0] = quantize(precision, c.real());
b[1] = quantize(precision, c.imag());
b[2] = quantize(precision, std::abs(c.real())-amp(4));
b[3] = quantize(precision, std::abs(c.imag())-amp(4));
b[4] = quantize(precision, std::abs(std::abs(c.real())-amp(4))-amp(2));
b[5] = quantize(precision, std::abs(std::abs(c.imag())-amp(4))-amp(2));
}
static complex_type map(code_type *b)
{
return AMP * complex_type(
b[0]*(b[2]*(b[4]+value_type(2))+value_type(4)),
b[1]*(b[3]*(b[5]+value_type(2))+value_type(4))
);
}
};
template <typename TYPE, typename CODE>
struct QuadratureAmplitudeModulation<256, TYPE, CODE>
{
static const int NUM = 256;
static const int BITS = 8;
typedef TYPE complex_type;
typedef typename TYPE::value_type value_type;
typedef CODE code_type;
static constexpr value_type FAC = 0.8692269873603529;
static constexpr value_type RCP = 15 * FAC;
static constexpr value_type AMP = 1 / RCP;
static constexpr value_type DIST = 2 * AMP;
static constexpr value_type amp(int i)
{
return AMP * i;
}
static code_type quantize(value_type precision, value_type value)
{
value *= DIST * precision;
if (std::is_integral<code_type>::value)
value = std::nearbyint(value);
if (std::is_same<code_type, int8_t>::value)
value = std::min<value_type>(std::max<value_type>(value, -128), 127);
return value;
}
static void hard(code_type *b, complex_type c)
{
b[0] = c.real() < amp(0) ? code_type(-1) : code_type(1);
b[1] = c.imag() < amp(0) ? code_type(-1) : code_type(1);
b[2] = std::abs(c.real()) < amp(8) ? code_type(-1) : code_type(1);
b[3] = std::abs(c.imag()) < amp(8) ? code_type(-1) : code_type(1);
b[4] = std::abs(std::abs(c.real())-amp(8)) < amp(4) ? code_type(-1) : code_type(1);
b[5] = std::abs(std::abs(c.imag())-amp(8)) < amp(4) ? code_type(-1) : code_type(1);
b[6] = std::abs(std::abs(std::abs(c.real())-amp(8))-amp(4)) < amp(2) ? code_type(-1) : code_type(1);
b[7] = std::abs(std::abs(std::abs(c.imag())-amp(8))-amp(4)) < amp(2) ? code_type(-1) : code_type(1);
}
static void soft(code_type *b, complex_type c, value_type precision)
{
b[0] = quantize(precision, c.real());
b[1] = quantize(precision, c.imag());
b[2] = quantize(precision, std::abs(c.real())-amp(8));
b[3] = quantize(precision, std::abs(c.imag())-amp(8));
b[4] = quantize(precision, std::abs(std::abs(c.real())-amp(8))-amp(4));
b[5] = quantize(precision, std::abs(std::abs(c.imag())-amp(8))-amp(4));
b[6] = quantize(precision, std::abs(std::abs(std::abs(c.real())-amp(8))-amp(4))-amp(2));
b[7] = quantize(precision, std::abs(std::abs(std::abs(c.imag())-amp(8))-amp(4))-amp(2));
}
static complex_type map(code_type *b)
{
return AMP * complex_type(
b[0]*(b[2]*(b[4]*(b[6]+value_type(2))+value_type(4))+value_type(8)),
b[1]*(b[3]*(b[5]*(b[7]+value_type(2))+value_type(4))+value_type(8))
);
}
};
template <typename TYPE, typename CODE>
struct QuadratureAmplitudeModulation<1024, TYPE, CODE>
{
static const int NUM = 1024;
static const int BITS = 10;
typedef TYPE complex_type;
typedef typename TYPE::value_type value_type;
typedef CODE code_type;
static constexpr value_type FAC = 0.8424235391742344;
static constexpr value_type RCP = 31 * FAC;
static constexpr value_type AMP = 1 / RCP;
static constexpr value_type DIST = 2 * AMP;
static constexpr value_type amp(int i)
{
return AMP * i;
}
static code_type quantize(value_type precision, value_type value)
{
value *= DIST * precision;
if (std::is_integral<code_type>::value)
value = std::nearbyint(value);
if (std::is_same<code_type, int8_t>::value)
value = std::min<value_type>(std::max<value_type>(value, -128), 127);
return value;
}
static void hard(code_type *b, complex_type c)
{
b[0] = c.real() < amp(0) ? code_type(-1) : code_type(1);
b[1] = c.imag() < amp(0) ? code_type(-1) : code_type(1);
b[2] = std::abs(c.real()) < amp(16) ? code_type(-1) : code_type(1);
b[3] = std::abs(c.imag()) < amp(16) ? code_type(-1) : code_type(1);
b[4] = std::abs(std::abs(c.real())-amp(16)) < amp(8) ? code_type(-1) : code_type(1);
b[5] = std::abs(std::abs(c.imag())-amp(16)) < amp(8) ? code_type(-1) : code_type(1);
b[6] = std::abs(std::abs(std::abs(c.real())-amp(16))-amp(8)) < amp(4) ? code_type(-1) : code_type(1);
b[7] = std::abs(std::abs(std::abs(c.imag())-amp(16))-amp(8)) < amp(4) ? code_type(-1) : code_type(1);
b[8] = std::abs(std::abs(std::abs(std::abs(c.real())-amp(16))-amp(8))-amp(4)) < amp(2) ? code_type(-1) : code_type(1);
b[9] = std::abs(std::abs(std::abs(std::abs(c.imag())-amp(16))-amp(8))-amp(4)) < amp(2) ? code_type(-1) : code_type(1);
}
static void soft(code_type *b, complex_type c, value_type precision)
{
b[0] = quantize(precision, c.real());
b[1] = quantize(precision, c.imag());
b[2] = quantize(precision, std::abs(c.real())-amp(16));
b[3] = quantize(precision, std::abs(c.imag())-amp(16));
b[4] = quantize(precision, std::abs(std::abs(c.real())-amp(16))-amp(8));
b[5] = quantize(precision, std::abs(std::abs(c.imag())-amp(16))-amp(8));
b[6] = quantize(precision, std::abs(std::abs(std::abs(c.real())-amp(16))-amp(8))-amp(4));
b[7] = quantize(precision, std::abs(std::abs(std::abs(c.imag())-amp(16))-amp(8))-amp(4));
b[8] = quantize(precision, std::abs(std::abs(std::abs(std::abs(c.real())-amp(16))-amp(8))-amp(4))-amp(2));
b[9] = quantize(precision, std::abs(std::abs(std::abs(std::abs(c.imag())-amp(16))-amp(8))-amp(4))-amp(2));
}
static complex_type map(code_type *b)
{
return AMP * complex_type(
b[0]*(b[2]*(b[4]*(b[6]*(b[8]+value_type(2))+value_type(4))+value_type(8))+value_type(16)),
b[1]*(b[3]*(b[5]*(b[7]*(b[9]+value_type(2))+value_type(4))+value_type(8))+value_type(16))
);
}
};
#endif