-
Notifications
You must be signed in to change notification settings - Fork 78
/
convert.c
500 lines (412 loc) · 12.9 KB
/
convert.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Part of readsb, a Mode-S/ADSB/TIS message decoder.
//
// convert.c: support for various IQ -> magnitude conversions
//
// Copyright (c) 2019 Michael Wolf <michael@mictronics.de>
//
// This code is based on a detached fork of dump1090-fa.
//
// Copyright (c) 2015 Oliver Jowett <oliver@mutability.co.uk>
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This file is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "readsb.h"
struct converter_state {
float dc_a;
float dc_b;
float z1_I;
float z1_Q;
};
static uint16_t *uc8_lookup;
static bool init_uc8_lookup() {
if (uc8_lookup)
return true;
uc8_lookup = cmalloc(sizeof (uint16_t) * 256 * 256);
if (!uc8_lookup) {
fprintf(stderr, "can't allocate UC8 conversion lookup table\n");
return false;
}
for (int i = 0; i <= 255; i++) {
for (int q = 0; q <= 255; q++) {
float fI, fQ, magsq;
fI = (i - 127.5) / 127.5;
fQ = (q - 127.5) / 127.5;
magsq = fI * fI + fQ * fQ;
//fprintf(stderr, "%d %d %.2f\n", i, q, magsq);
if (magsq > 1)
magsq = 1;
float mag = sqrtf(magsq);
uc8_lookup[le16toh((i * 256) + q)] = (uint16_t) (mag * 65535.0f + 0.5f);
}
}
return true;
}
static void convert_uc8_nodc(void *iq_data,
uint16_t *mag_data,
unsigned nsamples,
struct converter_state *state,
double *out_mean_level,
double *out_mean_power) {
uint16_t *in = iq_data;
unsigned i;
uint64_t sum_level = 0;
uint64_t sum_power = 0;
uint16_t mag;
MODES_NOTUSED(state);
// Increases readability but no optimization
#define DO_ONE_SAMPLE \
do { \
mag = uc8_lookup[*in++]; \
*mag_data++ = mag; \
sum_level += mag; \
sum_power += (uint32_t)mag * (uint32_t)mag; \
} while(0)
// unroll this a bit
for (i = 0; i < (nsamples / 4); ++i) {
DO_ONE_SAMPLE;
DO_ONE_SAMPLE;
DO_ONE_SAMPLE;
DO_ONE_SAMPLE;
}
for (i = 0; i < (nsamples % 4); ++i) {
DO_ONE_SAMPLE;
}
#undef DO_ONE_SAMPLE
if (out_mean_level) {
*out_mean_level = sum_level / 65536.0 / nsamples;
}
if (out_mean_power) {
*out_mean_power = sum_power / 65535.0 / 65535.0 / nsamples;
}
}
static void convert_uc8_generic(void *iq_data,
uint16_t *mag_data,
unsigned nsamples,
struct converter_state *state,
double *out_mean_level,
double *out_mean_power) {
uint8_t *in = iq_data;
float z1_I = state->z1_I;
float z1_Q = state->z1_Q;
const float dc_a = state->dc_a;
const float dc_b = state->dc_b;
unsigned i;
uint8_t I, Q;
float fI, fQ, magsq;
float sum_level = 0, sum_power = 0;
for (i = 0; i < nsamples; ++i) {
I = *in++;
Q = *in++;
fI = (I - 127.5f) / 127.5f;
fQ = (Q - 127.5f) / 127.5f;
// DC block
z1_I = fI * dc_a + z1_I * dc_b;
z1_Q = fQ * dc_a + z1_Q * dc_b;
fI -= z1_I;
fQ -= z1_Q;
magsq = fI * fI + fQ * fQ;
if (magsq > 1)
magsq = 1;
float mag = sqrtf(magsq);
sum_power += magsq;
sum_level += mag;
*mag_data++ = (uint16_t) (mag * 65535.0f + 0.5f);
}
state->z1_I = z1_I;
state->z1_Q = z1_Q;
if (out_mean_level) {
*out_mean_level = sum_level / nsamples;
}
if (out_mean_power) {
*out_mean_power = sum_power / nsamples;
}
}
static void convert_sc16_generic(void *iq_data,
uint16_t *mag_data,
unsigned nsamples,
struct converter_state *state,
double *out_mean_level,
double *out_mean_power) {
uint16_t *in = iq_data;
float z1_I = state->z1_I;
float z1_Q = state->z1_Q;
const float dc_a = state->dc_a;
const float dc_b = state->dc_b;
unsigned i;
int16_t I, Q;
float fI, fQ, magsq;
float sum_level = 0, sum_power = 0;
for (i = 0; i < nsamples; ++i) {
I = (int16_t) le16toh(*in++);
Q = (int16_t) le16toh(*in++);
fI = I / 32768.0f;
fQ = Q / 32768.0f;
// DC block
z1_I = fI * dc_a + z1_I * dc_b;
z1_Q = fQ * dc_a + z1_Q * dc_b;
fI -= z1_I;
fQ -= z1_Q;
magsq = fI * fI + fQ * fQ;
if (magsq > 1)
magsq = 1;
float mag = sqrtf(magsq);
sum_power += magsq;
sum_level += mag;
*mag_data++ = (uint16_t) (mag * 65535.0f + 0.5f);
}
state->z1_I = z1_I;
state->z1_Q = z1_Q;
if (out_mean_level) {
*out_mean_level = sum_level / nsamples;
}
if (out_mean_power) {
*out_mean_power = sum_power / nsamples;
}
}
static void convert_sc16_nodc(void *iq_data,
uint16_t *mag_data,
unsigned nsamples,
struct converter_state *state,
double *out_mean_level,
double *out_mean_power) {
MODES_NOTUSED(state);
uint16_t *in = iq_data;
unsigned i;
int16_t I, Q;
float fI, fQ, magsq;
float sum_level = 0, sum_power = 0;
for (i = 0; i < nsamples; ++i) {
I = (int16_t) le16toh(*in++);
Q = (int16_t) le16toh(*in++);
fI = I / 32768.0f;
fQ = Q / 32768.0f;
magsq = fI * fI + fQ * fQ;
if (magsq > 1)
magsq = 1;
float mag = sqrtf(magsq);
sum_power += magsq;
sum_level += mag;
*mag_data++ = (uint16_t) (mag * 65535.0f + 0.5f);
}
if (out_mean_level) {
*out_mean_level = sum_level / nsamples;
}
if (out_mean_power) {
*out_mean_power = sum_power / nsamples;
}
}
// SC16Q11_TABLE_BITS controls the size of the lookup table
// for SC16Q11 data. The size of the table is 2 * (1 << (2*BITS))
// bytes. Reducing the number of bits reduces precision but
// can run substantially faster by staying in cache.
// See convert_benchmark.c for some numbers.
// Leaving SC16QQ_TABLE_BITS undefined will disable the table lookup and always use
// the floating-point path, which may be faster on some systems
#if defined(SC16Q11_TABLE_BITS)
#define USE_BITS SC16Q11_TABLE_BITS
#define LOSE_BITS (11 - SC16Q11_TABLE_BITS)
static uint16_t *sc16q11_lookup;
static bool init_sc16q11_lookup() {
if (sc16q11_lookup)
return true;
sc16q11_lookup = cmalloc(sizeof (uint16_t) * (1 << (USE_BITS * 2)));
if (!sc16q11_lookup) {
fprintf(stderr, "can't allocate SC16Q11 conversion lookup table\n");
return false;
}
for (int i = 0; i < 2048; i += (1 << LOSE_BITS)) {
for (int q = 0; q < 2048; q += (1 << LOSE_BITS)) {
float fI = i / 2048.0, fQ = q / 2048.0;
float magsq = fI * fI + fQ * fQ;
if (magsq > 1)
magsq = 1;
float mag = sqrtf(magsq);
unsigned index = ((i >> LOSE_BITS) << USE_BITS) | (q >> LOSE_BITS);
sc16q11_lookup[index] = (uint16_t) (mag * 65535.0f + 0.5f);
}
}
return true;
}
static void convert_sc16q11_table(void *iq_data,
uint16_t *mag_data,
unsigned nsamples,
struct converter_state *state,
double *out_mean_level,
double *out_mean_power) {
uint16_t *in = iq_data;
unsigned i;
uint16_t I, Q;
uint64_t sum_level = 0;
uint64_t sum_power = 0;
uint16_t mag;
MODES_NOTUSED(state);
for (i = 0; i < nsamples; ++i) {
I = abs((int16_t) le16toh(*in++)) & 2047;
Q = abs((int16_t) le16toh(*in++)) & 2047;
mag = sc16q11_lookup[((I >> LOSE_BITS) << USE_BITS) | (Q >> LOSE_BITS)];
*mag_data++ = mag;
sum_level += mag;
sum_power += (uint32_t) mag * (uint32_t) mag;
}
if (out_mean_level) {
*out_mean_level = sum_level / 65536.0 / nsamples;
}
if (out_mean_power) {
*out_mean_power = sum_power / 65535.0 / 65535.0 / nsamples;
}
}
#else /* ! defined(SC16Q11_TABLE_BITS) */
static void convert_sc16q11_nodc(void *iq_data,
uint16_t *mag_data,
unsigned nsamples,
struct converter_state *state,
double *out_mean_level,
double *out_mean_power) {
MODES_NOTUSED(state);
uint16_t *in = iq_data;
unsigned i;
int16_t I, Q;
float fI, fQ, magsq;
float sum_level = 0, sum_power = 0;
for (i = 0; i < nsamples; ++i) {
I = (int16_t) le16toh(*in++);
Q = (int16_t) le16toh(*in++);
fI = I / 2048.0f;
fQ = Q / 2048.0f;
magsq = fI * fI + fQ * fQ;
if (magsq > 1)
magsq = 1;
float mag = sqrtf(magsq);
sum_power += magsq;
sum_level += mag;
*mag_data++ = (uint16_t) (mag * 65535.0f + 0.5f);
}
if (out_mean_level) {
*out_mean_level = sum_level / nsamples;
}
if (out_mean_power) {
*out_mean_power = sum_power / nsamples;
}
}
#endif /* defined(SC16Q11_TABLE_BITS) */
static void convert_sc16q11_generic(void *iq_data,
uint16_t *mag_data,
unsigned nsamples,
struct converter_state *state,
double *out_mean_level,
double *out_mean_power) {
uint16_t *in = iq_data;
float z1_I = state->z1_I;
float z1_Q = state->z1_Q;
const float dc_a = state->dc_a;
const float dc_b = state->dc_b;
unsigned i;
int16_t I, Q;
float fI, fQ, magsq;
float sum_level = 0, sum_power = 0;
for (i = 0; i < nsamples; ++i) {
I = (int16_t) le16toh(*in++);
Q = (int16_t) le16toh(*in++);
fI = I / 2048.0f;
fQ = Q / 2048.0f;
// DC block
z1_I = fI * dc_a + z1_I * dc_b;
z1_Q = fQ * dc_a + z1_Q * dc_b;
fI -= z1_I;
fQ -= z1_Q;
magsq = fI * fI + fQ * fQ;
if (magsq > 1)
magsq = 1;
float mag = sqrtf(magsq);
sum_power += magsq;
sum_level += mag;
*mag_data++ = (uint16_t) (mag * 65535.0f + 0.5f);
}
state->z1_I = z1_I;
state->z1_Q = z1_Q;
if (out_mean_level) {
*out_mean_level = sum_level / nsamples;
}
if (out_mean_power) {
*out_mean_power = sum_power / nsamples;
}
}
static struct {
input_format_t format;
int can_filter_dc;
iq_convert_fn fn;
const char *description;
bool(*init)();
} converters_table[] = {
// In order of preference
{ INPUT_UC8, 0, convert_uc8_nodc, "UC8, integer/table path", init_uc8_lookup},
{ INPUT_UC8, 1, convert_uc8_generic, "UC8, float path", NULL},
{ INPUT_SC16, 0, convert_sc16_nodc, "SC16, float path, no DC", NULL},
{ INPUT_SC16, 1, convert_sc16_generic, "SC16, float path", NULL},
#if defined(SC16Q11_TABLE_BITS)
{ INPUT_SC16Q11, 0, convert_sc16q11_table, "SC16Q11, integer/table path", init_sc16q11_lookup},
#else
{ INPUT_SC16Q11, 0, convert_sc16q11_nodc, "SC16Q11, float path, no DC", NULL},
#endif
{ INPUT_SC16Q11, 1, convert_sc16q11_generic, "SC16Q11, float path", NULL},
{ 0, 0, NULL, NULL, NULL}
};
iq_convert_fn init_converter(input_format_t format,
double sample_rate,
int filter_dc,
struct converter_state **out_state) {
int i;
for (i = 0; converters_table[i].fn; ++i) {
if (converters_table[i].format != format)
continue;
if (filter_dc && !converters_table[i].can_filter_dc)
continue;
break;
}
if (!converters_table[i].fn) {
fprintf(stderr, "no suitable converter for format=%d dc=%d\n",
format, filter_dc);
return NULL;
}
if (converters_table[i].init) {
if (!converters_table[i].init())
return NULL;
}
*out_state = cmalloc(sizeof (struct converter_state));
if (! *out_state) {
fprintf(stderr, "can't allocate converter state\n");
return NULL;
}
(*out_state)->z1_I = 0;
(*out_state)->z1_Q = 0;
if (filter_dc) {
// init DC block @ 1Hz
(*out_state)->dc_b = exp(-2.0 * M_PI * 1.0 / sample_rate);
(*out_state)->dc_a = 1.0 - (*out_state)->dc_b;
} else {
// if the converter does filtering, make sure it has no effect
(*out_state)->dc_b = 1.0;
(*out_state)->dc_a = 0.0;
}
if (Modes.sdr_type == SDR_IFILE) {
fprintf(stderr, "init_converter: using %s\n", converters_table[i].description);
}
return converters_table[i].fn;
}
void cleanup_converter(struct converter_state **state) {
sfree(uc8_lookup);
#if defined(SC16Q11_TABLE_BITS)
sfree(sc16q11_lookup);
#endif
sfree(*state);
}