forked from flightaware/dump978
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.cc
325 lines (285 loc) · 11.2 KB
/
convert.cc
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
// Copyright (c) 2019, FlightAware LLC.
// All rights reserved.
// Licensed under the 2-clause BSD license; see the LICENSE file
#include "convert.h"
#include <assert.h>
#include <cmath>
#include <stdexcept>
using namespace flightaware::uat;
static inline std::uint16_t scaled_atan2(double y, double x) {
double ang = std::atan2(y, x);
if (ang < 0) {
// atan2 returns [-pi..pi], normalize to [0..2*pi]
ang += 2 * M_PI;
}
double scaled_ang = std::round(32768 * ang / M_PI);
return scaled_ang < 0 ? 0 : scaled_ang > 65535 ? 65535 : (std::uint16_t)scaled_ang;
}
static inline std::uint16_t scaled_atan(double x) {
double ang = std::atan(x);
if (ang < 0) {
// atan returns [-pi/2..pi/2], normalize to [0..2*pi]
ang += 2 * M_PI;
}
double scaled_ang = std::round(32768 * ang / M_PI);
return scaled_ang < 0 ? 0 : scaled_ang > 65535 ? 65535 : (std::uint16_t)scaled_ang;
}
static inline double magsq(double i, double q) { return i * i + q * q; }
SampleConverter::Pointer SampleConverter::Create(SampleFormat format) {
switch (format) {
case SampleFormat::CU8:
return Pointer(new CU8Converter());
case SampleFormat::CS8_:
return Pointer(new CS8Converter());
case SampleFormat::CS16H:
return Pointer(new CS16HConverter());
case SampleFormat::CF32H:
return Pointer(new CF32HConverter());
default:
throw std::runtime_error("format not implemented yet");
}
}
CU8Converter::CU8Converter() : SampleConverter(SampleFormat::CU8) {
cu8_alias u;
unsigned i, q;
for (i = 0; i < 256; ++i) {
double d_i = (i - 127.5) / 128.0;
for (q = 0; q < 256; ++q) {
double d_q = (q - 127.5) / 128.0;
u.iq[0] = i;
u.iq[1] = q;
lookup_phase_[u.iq16] = scaled_atan2(d_q, d_i);
lookup_magsq_[u.iq16] = magsq(d_i, d_q);
}
}
}
void CU8Converter::ConvertPhase(Bytes::const_iterator begin, Bytes::const_iterator end, PhaseBuffer::iterator out) {
const cu8_alias *in_iq = reinterpret_cast<const cu8_alias *>(&*begin);
// unroll the loop
const auto n = std::distance(begin, end) / 2;
const auto n8 = n / 8;
const auto n7 = n & 7;
for (auto i = 0; i < n8; ++i, in_iq += 8) {
*out++ = lookup_phase_[in_iq[0].iq16];
*out++ = lookup_phase_[in_iq[1].iq16];
*out++ = lookup_phase_[in_iq[2].iq16];
*out++ = lookup_phase_[in_iq[3].iq16];
*out++ = lookup_phase_[in_iq[4].iq16];
*out++ = lookup_phase_[in_iq[5].iq16];
*out++ = lookup_phase_[in_iq[6].iq16];
*out++ = lookup_phase_[in_iq[7].iq16];
}
for (auto i = 0; i < n7; ++i, ++in_iq) {
*out++ = lookup_phase_[in_iq[0].iq16];
}
}
void CU8Converter::ConvertMagSq(Bytes::const_iterator begin, Bytes::const_iterator end, std::vector<double>::iterator out) {
const cu8_alias *in_iq = reinterpret_cast<const cu8_alias *>(&*begin);
// unroll the loop
const auto n = std::distance(begin, end) / 2;
const auto n8 = n / 8;
const auto n7 = n & 7;
for (auto i = 0; i < n8; ++i, in_iq += 8) {
*out++ = lookup_magsq_[in_iq[0].iq16];
*out++ = lookup_magsq_[in_iq[1].iq16];
*out++ = lookup_magsq_[in_iq[2].iq16];
*out++ = lookup_magsq_[in_iq[3].iq16];
*out++ = lookup_magsq_[in_iq[4].iq16];
*out++ = lookup_magsq_[in_iq[5].iq16];
*out++ = lookup_magsq_[in_iq[6].iq16];
*out++ = lookup_magsq_[in_iq[7].iq16];
}
for (auto i = 0; i < n7; ++i, ++in_iq) {
*out++ = lookup_magsq_[in_iq[0].iq16];
}
}
CS8Converter::CS8Converter() : SampleConverter(SampleFormat::CS8_) {
cs8_alias u;
int i, q;
for (i = -128; i <= 127; ++i) {
double d_i = i / 128.0;
for (q = -128; q <= 127; ++q) {
double d_q = q / 128.0;
u.iq[0] = i;
u.iq[1] = q;
lookup_phase_[u.iq16] = scaled_atan2(d_q, d_i);
lookup_magsq_[u.iq16] = magsq(d_i, d_q);
}
}
}
void CS8Converter::ConvertPhase(Bytes::const_iterator begin, Bytes::const_iterator end, PhaseBuffer::iterator out) {
auto in_iq = reinterpret_cast<const cs8_alias *>(&*begin);
// unroll the loop
const auto n = std::distance(begin, end) / 2;
const auto n8 = n / 8;
const auto n7 = n & 7;
for (auto i = 0; i < n8; ++i, in_iq += 8) {
*out++ = lookup_phase_[in_iq[0].iq16];
*out++ = lookup_phase_[in_iq[1].iq16];
*out++ = lookup_phase_[in_iq[2].iq16];
*out++ = lookup_phase_[in_iq[3].iq16];
*out++ = lookup_phase_[in_iq[4].iq16];
*out++ = lookup_phase_[in_iq[5].iq16];
*out++ = lookup_phase_[in_iq[6].iq16];
*out++ = lookup_phase_[in_iq[7].iq16];
}
for (auto i = 0; i < n7; ++i, ++in_iq) {
*out++ = lookup_phase_[in_iq[0].iq16];
}
}
void CS8Converter::ConvertMagSq(Bytes::const_iterator begin, Bytes::const_iterator end, std::vector<double>::iterator out) {
auto in_iq = reinterpret_cast<const cs8_alias *>(&*begin);
// unroll the loop
const auto n = std::distance(begin, end) / 2;
const auto n8 = n / 8;
const auto n7 = n & 7;
for (auto i = 0; i < n8; ++i, in_iq += 8) {
*out++ = lookup_magsq_[in_iq[0].iq16];
*out++ = lookup_magsq_[in_iq[1].iq16];
*out++ = lookup_magsq_[in_iq[2].iq16];
*out++ = lookup_magsq_[in_iq[3].iq16];
*out++ = lookup_magsq_[in_iq[4].iq16];
*out++ = lookup_magsq_[in_iq[5].iq16];
*out++ = lookup_magsq_[in_iq[6].iq16];
*out++ = lookup_magsq_[in_iq[7].iq16];
}
for (auto i = 0; i < n7; ++i, ++in_iq) {
*out++ = lookup_magsq_[in_iq[0].iq16];
}
}
static inline std::int16_t PhaseDifference(std::uint16_t from, std::uint16_t to) {
int32_t difference = to - from; // lies in the range -65535 .. +65535
if (difference >= 32768) // +32768..+65535
return difference - 65536; // -> -32768..-1: always in range
else if (difference < -32768) // -65535..-32769
return difference + 65536; // -> +1..32767: always in range
else
return difference;
}
CS16HConverter::CS16HConverter() : SampleConverter(SampleFormat::CS16H) {
// atan lookup, positive values only, 8-bit fixed point covering 0.0 .. 256.0
for (std::size_t i = 0; i < lookup_atan_.size(); ++i) {
lookup_atan_[i] = scaled_atan(i / 256.0);
}
}
// caution, expects unsigned (positive) input only
inline std::uint16_t CS16HConverter::TableAtan(std::uint32_t r) {
if (r > lookup_atan_.size())
return 16384; // pi/2
else
return lookup_atan_[r];
}
inline std::uint16_t CS16HConverter::TableAtan2(std::int16_t y, std::int16_t x) {
// atan2 using the atan lookup table
// we rely on unsigned 16-bit integer overflow/wrap semantics
// max error is about 0.2 degrees
if (x == 0) {
if (y >= 0) {
return 16384; // pi/2
} else {
return 49152; // 3/2 pi
}
}
const std::int32_t r = (std::int32_t)(256 * y) / x;
if (x < 0) {
if (y < 0) {
// x < 0, y < 0 => y/x > 0
// atan2(y,x) = pi + atan(y/x)
return (std::uint16_t)32768 + TableAtan((std::uint32_t)r);
} else {
// x < 0, y >= 0 => y/x <= 0
// atan2(y,x) = -pi + atan(y/x) = -pi - atan(-y/x)
return (std::uint16_t)32768 - TableAtan((std::uint32_t)-r);
}
} else {
if (y < 0) {
// x > 0, y < 0 => y/x < 0
// atan2(y,x) = atan(y/x) = -atan(-y/x)
return (std::uint16_t)0 - TableAtan((std::uint32_t)-r);
} else {
// x > 0, y >= 0 => y/x >= 0
// atan2(y,x) = atan(y/x)
return TableAtan((std::uint32_t)r);
}
}
}
void CS16HConverter::ConvertPhase(Bytes::const_iterator begin, Bytes::const_iterator end, PhaseBuffer::iterator out) {
auto in_iq = reinterpret_cast<const std::int16_t *>(&*begin);
// unroll the loop
const auto n = std::distance(begin, end) / 4;
const auto n8 = n / 8;
const auto n7 = n & 7;
for (auto i = 0; i < n8; ++i, in_iq += 16) {
*out++ = TableAtan2(in_iq[1], in_iq[0]);
*out++ = TableAtan2(in_iq[3], in_iq[2]);
*out++ = TableAtan2(in_iq[5], in_iq[4]);
*out++ = TableAtan2(in_iq[7], in_iq[6]);
*out++ = TableAtan2(in_iq[9], in_iq[8]);
*out++ = TableAtan2(in_iq[11], in_iq[10]);
*out++ = TableAtan2(in_iq[13], in_iq[12]);
*out++ = TableAtan2(in_iq[15], in_iq[14]);
}
for (auto i = 0; i < n7; ++i, in_iq += 2) {
*out++ = TableAtan2(in_iq[1], in_iq[0]);
}
}
void CS16HConverter::ConvertMagSq(Bytes::const_iterator begin, Bytes::const_iterator end, std::vector<double>::iterator out) {
auto in_iq = reinterpret_cast<const std::int16_t *>(&*begin);
// unroll the loop
const auto n = std::distance(begin, end) / 4;
const auto n8 = n / 8;
const auto n7 = n & 7;
for (auto i = 0; i < n8; ++i, in_iq += 16) {
*out++ = magsq(in_iq[1], in_iq[0]) / 32768.0 / 32768.0;
*out++ = magsq(in_iq[3], in_iq[2]) / 32768.0 / 32768.0;
*out++ = magsq(in_iq[5], in_iq[4]) / 32768.0 / 32768.0;
*out++ = magsq(in_iq[7], in_iq[6]) / 32768.0 / 32768.0;
*out++ = magsq(in_iq[9], in_iq[8]) / 32768.0 / 32768.0;
*out++ = magsq(in_iq[11], in_iq[10]) / 32768.0 / 32768.0;
*out++ = magsq(in_iq[13], in_iq[12]) / 32768.0 / 32768.0;
*out++ = magsq(in_iq[15], in_iq[14]) / 32768.0 / 32768.0;
}
for (auto i = 0; i < n7; ++i, in_iq += 2) {
*out++ = magsq(in_iq[1], in_iq[0]) / 32768.0 / 32768.0;
}
}
void CF32HConverter::ConvertPhase(Bytes::const_iterator begin, Bytes::const_iterator end, PhaseBuffer::iterator out) {
auto in_iq = reinterpret_cast<const float *>(&*begin);
// unroll the loop
const auto n = std::distance(begin, end) / 8;
const auto n8 = n / 8;
const auto n7 = n & 7;
for (auto i = 0; i < n8; ++i, in_iq += 16) {
*out++ = scaled_atan2(in_iq[1], in_iq[0]);
*out++ = scaled_atan2(in_iq[3], in_iq[2]);
*out++ = scaled_atan2(in_iq[5], in_iq[4]);
*out++ = scaled_atan2(in_iq[7], in_iq[6]);
*out++ = scaled_atan2(in_iq[9], in_iq[8]);
*out++ = scaled_atan2(in_iq[11], in_iq[10]);
*out++ = scaled_atan2(in_iq[13], in_iq[12]);
*out++ = scaled_atan2(in_iq[15], in_iq[14]);
}
for (auto i = 0; i < n7; ++i, in_iq += 2) {
*out++ = scaled_atan2(in_iq[1], in_iq[0]);
}
}
void CF32HConverter::ConvertMagSq(Bytes::const_iterator begin, Bytes::const_iterator end, std::vector<double>::iterator out) {
auto in_iq = reinterpret_cast<const float *>(&*begin);
// unroll the loop
const auto n = std::distance(begin, end) / 8;
const auto n8 = n / 8;
const auto n7 = n & 7;
for (auto i = 0; i < n8; ++i, in_iq += 16) {
*out++ = magsq(in_iq[1], in_iq[0]);
*out++ = magsq(in_iq[3], in_iq[2]);
*out++ = magsq(in_iq[5], in_iq[4]);
*out++ = magsq(in_iq[7], in_iq[6]);
*out++ = magsq(in_iq[9], in_iq[8]);
*out++ = magsq(in_iq[11], in_iq[10]);
*out++ = magsq(in_iq[13], in_iq[12]);
*out++ = magsq(in_iq[15], in_iq[14]);
}
for (auto i = 0; i < n7; ++i, in_iq += 2) {
*out++ = magsq(in_iq[1], in_iq[0]);
}
}