-
Notifications
You must be signed in to change notification settings - Fork 28
/
cal_ad9361.c
731 lines (634 loc) · 19.5 KB
/
cal_ad9361.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
* Copyright (C) 2014,2017 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
* Robin Getz <[email protected]>
*
* Licensed under the GPL-2.
*
* */
#include <errno.h>
#include <getopt.h>
#include <iio.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
#include <values.h>
#include <complex.h>
#include <fftw3.h>
#define SAMPLES_PER_READ 1048576
static const struct option options[] = {
{"help", no_argument, 0, 'h'},
{"uri", required_argument, 0, 'u'},
{"buffer-size", required_argument, 0, 'b'},
{"samples", required_argument, 0, 's' },
{"sample-rate", required_argument, 0, 'S'},
{"rxlo-freq", required_argument, 0, 'r'},
{"txlo-freq", required_argument, 0, 't'},
{"external-tone", required_argument, 0, 'e'},
{"gps-pps", no_argument, 0, 'g'},
{"timeout", required_argument, 0, 'T'},
{"auto", no_argument, 0, 'a'},
{"data-file", no_argument, 0, 'f'},
{ NULL, no_argument, NULL, 0 }
};
static const char *options_descriptions[] = {
"Show this help and quit.",
"Use the context with the provided URI.",
"Size of capture buffers. Default is 1048576.",
"Number of buffers to capture, 0 = infinite. Default is 1",
"Sample rate. Default is 30720000.",
"Rx LO frequency in Hz. Default is 0. 0 is off",
"Tx LO frequency in Hz. Default is 0. 0 is off",
"External Tone in Hz. Default is 0. 0 is off",
"Buffer timeout in milliseconds. 0 = no timeout",
"Scan for available contexts and if only one is available use it.",
"Save captured data to a file.",
};
static void usage(const char *name)
{
unsigned int i;
printf("Usage:\n\t %s [-T <timeout-ms>] [-b <buffer-size>] [-s <samples>] "
"<iio_device> [<channel> ...]\n\nOptions:\n", name);
for (i = 0; options[i].name; i++)
printf("\t-%c, --%s\n\t\t\t%s\n",
options[i].val, options[i].name,
options_descriptions[i]);
}
static struct iio_context *ctx;
static struct iio_buffer *buffer;
static size_t num_samples = 1;
static volatile sig_atomic_t app_running = true;
static int exit_code = EXIT_SUCCESS;
static void quit_all(int sig)
{
exit_code = sig;
app_running = false;
if (buffer)
iio_buffer_cancel(buffer);
}
#ifdef _WIN32
#include <windows.h>
BOOL WINAPI sig_handler_fn(DWORD dwCtrlType)
{
/* Runs in its own thread */
switch (dwCtrlType) {
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
quit_all(SIGTERM);
return TRUE;
default:
return FALSE;
}
}
static void setup_sig_handler(void)
{
SetConsoleCtrlHandler(sig_handler_fn, TRUE);
}
#elif NO_THREADS
static void sig_handler(int sig)
{
/*
* If the main function is stuck waiting for data it will not abort. If the
* user presses Ctrl+C a second time we abort without cleaning up.
*/
if (!app_running)
exit(sig);
app_running = false;
}
static void set_handler(int sig)
{
struct sigaction action;
sigaction(sig, NULL, &action);
action.sa_handler = sig_handler;
sigaction(sig, &action, NULL);
}
static void setup_sig_handler(void)
{
set_handler(SIGHUP);
set_handler(SIGPIPE);
set_handler(SIGINT);
set_handler(SIGSEGV);
set_handler(SIGTERM);
}
#else
#include <pthread.h>
static void * sig_handler_thd(void *data)
{
sigset_t *mask = data;
int ret, sig;
/* Blocks until one of the termination signals is received */
do {
ret = sigwait(mask, &sig);
} while (ret == EINTR);
quit_all(ret);
return NULL;
}
static void setup_sig_handler(void)
{
sigset_t mask, oldmask;
pthread_t thd;
int ret;
/*
* Async signals are difficult to handle and the IIO API is not signal
* safe. Use a seperate thread and handle the signals synchronous so we
* can call iio_buffer_cancel().
*/
sigemptyset(&mask);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGPIPE);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGSEGV);
sigaddset(&mask, SIGTERM);
pthread_sigmask(SIG_BLOCK, &mask, &oldmask);
ret = pthread_create(&thd, NULL, sig_handler_thd, &mask);
if (ret) {
fprintf(stderr, "Failed to create signal handler thread: %d\n", ret);
pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
}
}
#endif
static struct iio_context *scan(void)
{
struct iio_scan_context *scan_ctx;
struct iio_context_info **info;
struct iio_context *ctx = NULL;
unsigned int i;
ssize_t ret;
scan_ctx = iio_create_scan_context(NULL, 0);
if (!scan_ctx) {
fprintf(stderr, "Unable to create scan context\n");
return NULL;
}
ret = iio_scan_context_get_info_list(scan_ctx, &info);
if (ret < 0) {
char err_str[1024];
iio_strerror(-ret, err_str, sizeof(err_str));
fprintf(stderr, "Scanning for IIO contexts failed: %s\n", err_str);
goto err_free_ctx;
}
if (ret == 0) {
printf("No IIO context found.\n");
goto err_free_info_list;
}
if (ret == 1) {
ctx = iio_create_context_from_uri(iio_context_info_get_uri(info[0]));
} else {
fprintf(stderr, "Multiple contexts found. Please select one using --uri:\n");
for (i = 0; i < (size_t) ret; i++) {
fprintf(stderr, "\t%d: %s [%s]\n", i,
iio_context_info_get_description(info[i]),
iio_context_info_get_uri(info[i]));
}
}
err_free_info_list:
iio_context_info_list_free(info);
err_free_ctx:
iio_scan_context_destroy(scan_ctx);
return ctx;
}
struct write_l {
char *device;
char *channel;
bool in_out;
char *attribute;
char *value;
};
struct write_l write_log[128];
static int wl_index = 0;
static bool iio_set_attribute(char * device, char * channel, bool in_out,
char * attribute, char * buffer, bool log)
{
struct iio_device *dev;
struct iio_channel *chan;
const char * attr;
ssize_t ret;
char buf[1024];
dev = iio_context_find_device(ctx, device);
if (!dev) {
fprintf(stderr, "Device %s not found\n", device);
iio_context_destroy(ctx);
return false;
}
chan = iio_device_find_channel(dev, channel, in_out);
if (!chan) {
fprintf(stderr, "%s channel not found in %s\n", channel, device);
iio_context_destroy(ctx);
return false;
}
attr = iio_channel_find_attr(chan, attribute);
if (!attr) {
fprintf(stderr, "%s attribute not found in %s:%s\n", attribute, device, channel);
iio_context_destroy(ctx);
return false;
}
ret = iio_channel_attr_read(chan, attribute, buf, sizeof(buf));
if (ret > 0) {
if (log) {
write_log[wl_index].device = strdup(device);
write_log[wl_index].channel = strdup(channel);
write_log[wl_index].in_out = in_out;
write_log[wl_index].attribute = strdup(attribute);
write_log[wl_index].value = strdup(buf);
wl_index++;
}
}
if (buffer) {
ret = iio_channel_attr_write(chan, attribute, buffer);
if (ret < 0) {
printf("write '%s' failed to %s:%s:%s\n", buffer, device, channel, attribute);
iio_context_destroy(ctx);
return false;
}
}
return true;
}
static double win_hanning(int j, int n)
{
double a = 2.0 * M_PI / (n - 1), w;
w = 0.5 * (1.0 - cos(a * j));
return (w);
}
static double lo_frequency (uint32_t xo)
{
uint32_t tmp1, tmp2, tmp3;
double int_portion, fract_portion, vco_div;
/* registers are 8-bits*/
/* 0x231 is lower Rx synthesizer word */
iio_device_reg_read(iio_context_find_device(ctx, "ad9361-phy"), 0x231, &tmp1);
/* lower two bits in 0x232 are bits 10:8 */
iio_device_reg_read(iio_context_find_device(ctx, "ad9361-phy"), 0x232, &tmp2);
int_portion = ((tmp2 & 7) << 8) | tmp1;
iio_device_reg_read(iio_context_find_device(ctx, "ad9361-phy"), 0x233, &tmp1);
iio_device_reg_read(iio_context_find_device(ctx, "ad9361-phy"), 0x234, &tmp2);
iio_device_reg_read(iio_context_find_device(ctx, "ad9361-phy"), 0x235, &tmp3);
fract_portion = ((tmp3 & 0x7F) << 16) | (tmp2 << 8) | tmp1;
iio_device_reg_read(iio_context_find_device(ctx, "ad9361-phy"), 0x5, &tmp1);
vco_div = (tmp1 & 7);
if (xo < 41000000)
xo = xo * 2;
return ((xo * fract_portion) + (xo * int_portion * 8388593)) /
(pow(2, (vco_div + 1)) * 8388593);
}
#define IN false
#define OUT true
int main(int argc, char **argv)
{
unsigned int i, nb_channels, tx_lo = 0,
sample_rate = 30720000;
uint64_t rx_lo = 0, external_tone = 0;
unsigned int buffer_size = SAMPLES_PER_READ;
long long xo = 0, new_xo = 0;
int c, option_index = 0, arg_index = 0, uri_index = 0;
struct iio_device *dev;
size_t sample_size;
int timeout = -1;
bool scan_for_context = false, save_data = false, gps = false;
char buf[256];
fftw_complex *in_c, *out;
fftw_plan plan_forward;
double *win, exact_rx_lo, mult_fs, mult_lo;
struct iio_channel *rx_i;
FILE * fd = NULL;
while ((c = getopt_long(argc, argv, "+hfu:b:s:T:aS:r:t:e:g",
options, &option_index)) != -1) {
switch (c) {
case 'h':
usage(argv[0]);
return EXIT_SUCCESS;
case 'f':
arg_index += 1;
save_data = true;
break;
case 'u':
arg_index += 2;
uri_index = arg_index;
break;
case 'a':
arg_index += 1;
scan_for_context = true;
break;
case 'b':
arg_index += 2;
buffer_size = atoi(argv[arg_index]);
break;
case 's':
arg_index += 2;
num_samples = atoi(argv[arg_index]);
break;
case 'T':
arg_index += 2;
timeout = atoi(argv[arg_index]);
break;
case 'S':
arg_index += 2;
sample_rate = atoi(argv[arg_index]);
break;
case 'r':
arg_index += 2;
rx_lo = strtoll(argv[arg_index], NULL, 10);
if (tx_lo || external_tone) {
fprintf(stderr, "-e -r -t are not compatible\n");
return EXIT_FAILURE;
}
if (rx_lo >= 6000000000) {
fprintf(stderr, "rx_out of range\n");
return EXIT_FAILURE;
}
break;
case 't':
arg_index += 2;
tx_lo = atoi(argv[arg_index]);
if (rx_lo || external_tone) {
fprintf(stderr, "-e -r -t are not compatible\n");
return EXIT_FAILURE;
}
break;
case 'e':
arg_index += 2;
external_tone = strtoll(argv[arg_index], NULL, 10);
if (rx_lo || tx_lo) {
fprintf(stderr, "-e -r -t are not compatible\n");
return EXIT_FAILURE;
}
break;
case 'g':
arg_index += 1;
gps = true;
break;
case '?':
return EXIT_FAILURE;
}
}
num_samples = num_samples * buffer_size;
if (arg_index >= argc) {
fprintf(stderr, "Incorrect number of arguments.\n\n");
usage(argv[0]);
return EXIT_FAILURE;
}
if (!rx_lo && tx_lo) {
rx_lo = tx_lo;
}
if (!rx_lo && external_tone) {
rx_lo = external_tone - sample_rate/3;
}
setup_sig_handler();
if (scan_for_context)
ctx = scan();
else if (uri_index)
ctx = iio_create_context_from_uri(argv[uri_index]);
else
ctx = iio_create_default_context();
if (!ctx) {
fprintf(stderr, "Unable to create IIO context\n");
return EXIT_FAILURE;
}
if (timeout >= 0)
iio_context_set_timeout(ctx, timeout);
if (gps) {
long long val = 0;
int ret;
struct iio_device *dev;
struct iio_channel *pps;
dev = iio_context_find_device(ctx, "cf-ad9361-lpc");
if (!dev) {
printf("no device cf-ad9361-lpc\n");
return EXIT_FAILURE;
}
pps = iio_device_find_channel(dev, "voltage0", 0);
if (!pps) {
printf("no channel voltage0\n");
return EXIT_FAILURE;
}
sprintf(buf, "%u", sample_rate);
if (!iio_set_attribute("ad9361-phy", "voltage0", OUT,
"sampling_frequency", buf, true))
return EXIT_FAILURE;
ret = iio_channel_attr_read_longlong(pps, "in_voltage_samples_pps", &val);
if (ret) {
printf("no in_voltage_samples_pps\n");
return EXIT_FAILURE;
}
iio_device_attr_read_longlong(iio_context_find_device(ctx, "ad9361-phy"), "xo_correction", &xo);
printf("pps: %llu (%f ppm off) xo was: %llu ", val, (double)(val - sample_rate)/(double)sample_rate * 1e6, xo);
new_xo = (long long)((double)(1.0 + (double)(val - sample_rate)/(double)sample_rate) * xo);
if (xo == new_xo) {
printf("\n");
} else {
printf("now %llu\n", new_xo);
iio_device_attr_write_longlong(
iio_context_find_device(ctx, "ad9361-phy"),
"xo_correction", new_xo);
}
sleep(2);
quit_all(SIGTERM);
return EXIT_SUCCESS;
}
sprintf(buf, "%lu", rx_lo);
if (!iio_set_attribute("ad9361-phy", "RX_LO", OUT, "frequency", buf, true))
return EXIT_FAILURE;
iio_channel_attr_read(
iio_device_find_channel(
iio_context_find_device(ctx, "ad9361-phy"),
"RX_LO", OUT),
"frequency", buf, sizeof(buf));
rx_lo = strtoll(buf, NULL, 10);
iio_device_attr_read_longlong(iio_context_find_device(ctx, "ad9361-phy"), "xo_correction", &xo);
exact_rx_lo=lo_frequency(xo);
mult_lo = exact_rx_lo / (double)xo;
mult_fs = sample_rate / (double)xo;
sprintf(buf, "%u", sample_rate);
if (!iio_set_attribute("ad9361-phy", "voltage0", OUT, "sampling_frequency", buf, true))
return EXIT_FAILURE;
if (!iio_set_attribute("ad9361-phy", "voltage0", IN, "gain_control_mode", "slow_attack", true))
return EXIT_FAILURE;
rx_i = iio_device_find_channel(iio_context_find_device(ctx, "cf-ad9361-lpc"), "voltage0", 0);
if (tx_lo) {
sprintf(buf, "%u", tx_lo);
if (!iio_set_attribute("ad9361-phy", "TX_LO", OUT, "powerdown", "0", true) ||
!iio_set_attribute("ad9361-phy", "TX_LO", OUT, "frequency", buf, true))
return EXIT_FAILURE;
sprintf(buf, "%u", sample_rate / 3);
if (!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_I_F1", OUT, "frequency" , buf, true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_I_F2", OUT, "frequency" , buf, true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_Q_F1", OUT, "frequency" , buf, true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_Q_F2", OUT, "frequency" , buf, true))
return EXIT_FAILURE;
else {
iio_channel_attr_read(
iio_device_find_channel(
iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc"),
"TX1_I_F1", OUT),
"frequency", buf, sizeof(buf));
printf("got %s\n", buf);
}
if (!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_I_F1", OUT, "scale" , "0.4", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_I_F2", OUT, "scale" , "0.4", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_Q_F1", OUT, "scale" , "0.4", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_Q_F2", OUT, "scale" , "0.4", true))
return EXIT_FAILURE;
if (!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_I_F1", OUT, "phase" , "90000", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_I_F2", OUT, "phase" , "90000", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_Q_F1", OUT, "phase" , "0", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_Q_F2", OUT, "phase" , "0", true))
return EXIT_FAILURE;
if (!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_I_F1", OUT, "raw" , "1", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_I_F2", OUT, "raw" , "1", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_Q_F1", OUT, "raw" , "1", true) ||
!iio_set_attribute("cf-ad9361-dds-core-lpc", "TX1_Q_F2", OUT, "raw" , "1", true))
return EXIT_FAILURE;
} else {
if (!iio_set_attribute("ad9361-phy", "TX_LO", OUT, "powerdown", "1", true))
return EXIT_FAILURE;
}
/* pause for RSSI to stablize */
usleep (100000);
iio_channel_attr_read(
iio_device_find_channel(
iio_context_find_device(ctx, "ad9361-phy"),
"voltage0", false),
"rssi", buf, sizeof(buf));
i=atoi(buf);
if (i > 75) {
printf("signal too weak (rssi = %i), adjust transmitter\n", i);
quit_all(EXIT_FAILURE);
}
iio_device_attr_read(iio_context_find_device(ctx, "ad9361-phy"), "xo_correction", buf, sizeof(buf));
xo=atoi(buf);
dev = iio_context_find_device(ctx, "cf-ad9361-lpc");
if (!dev) {
fprintf(stderr, "Device %s not found\n", argv[arg_index + 1]);
iio_context_destroy(ctx);
return EXIT_FAILURE;
}
nb_channels = iio_device_get_channels_count(dev);
printf("enabled %i channels\n", nb_channels);
/* Enable all channels */
for (i = 0; i < nb_channels; i++)
iio_channel_enable(iio_device_get_channel(dev, i));
sample_size = iio_device_get_sample_size(dev);
buffer = iio_device_create_buffer(dev, buffer_size, false);
if (!buffer) {
char buf[256];
iio_strerror(errno, buf, sizeof(buf));
fprintf(stderr, "Unable to allocate buffer: %s\n", buf);
iio_context_destroy(ctx);
return EXIT_FAILURE;
}
win = fftw_malloc(sizeof(double) * buffer_size);
in_c = fftw_malloc(sizeof(fftw_complex) * buffer_size);
out = fftw_malloc(sizeof(fftw_complex) * (buffer_size + 1));
plan_forward = fftw_plan_dft_1d(buffer_size, in_c, out, FFTW_FORWARD, FFTW_ESTIMATE);
for (i = 0; i < buffer_size; i ++)
win[i] = win_hanning(i, buffer_size);
while (app_running) {
int ret;
unsigned int j, k, cnt, bin[5];
double mag[5], peak[5], side[2];
ret = iio_buffer_refill(buffer);
if (ret < 0) {
if (app_running) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "Unable to refill buffer: %s\n", buf);
}
break;
}
/* If there are only the samples we requested, we don't need to
* demux */
if (iio_buffer_step(buffer) == (ptrdiff_t)sample_size) {
void *data, *end;
ptrdiff_t inc;
double actual_bin;
end = iio_buffer_end(buffer);
inc = iio_buffer_step(buffer);
for (cnt = 0, data = iio_buffer_first(buffer, rx_i); data < end; data += inc, cnt++) {
const int16_t real = ((int16_t*)data)[0]; // Real (I)
const int16_t imag = ((int16_t*)data)[1]; // Imag (Q)
in_c[cnt] = (real * win[cnt] + I * imag * win[cnt]) / 2048;
}
fftw_execute(plan_forward);
for (j = 0; j <= 2; j++) {
peak[j] = -FLT_MAX;
bin[j] = 0;
}
if (save_data)
fd = fopen("./dat.bin", "w");
for (i = 1; i < buffer_size; ++i) {
mag[2] = mag[1];
mag[1] = mag[0];
mag[0] = 10 * log10((creal(out[i]) * creal(out[i]) + cimag(out[i]) * cimag(out[i])) /
((unsigned long long)buffer_size * (unsigned long long)buffer_size));
if (fd)
fprintf(fd, "%f\n", mag[0]);
if (i < 2)
continue;
for (j = 0; j <= 2; j++) {
if ((mag[1] > peak[j]) &&
((!((mag[2] > mag[1]) && (mag[1] > mag[0]))) &&
(!((mag[2] < mag[1]) && (mag[1] < mag[0]))))) {
for (k = 2; k > j; k--) {
peak[k] = peak[k - 1];
bin[k] = bin[k - 1];
}
peak[j] = mag[1];
bin[j] = i - 1;
if (j == 0) {
side[0] = mag[0];
side[1] = mag[2];
}
break;
}
}
}
if (fd)
fclose(fd);
fd = NULL;
printf("peaks at ");
for (j = 0; j <= 2; j++)
printf("%f (%i); ", peak[j], bin[j]);
printf("\nSFDR = %f\n", peak[0] - peak[1]);
if (peak[0] < (peak[1] + 20)) {
printf("can't find strong signal, fix signal source\n");
quit_all(EXIT_FAILURE);
}
/* based on
* https://ccrma.stanford.edu/~jos/sasp/Quadratic_Interpolation_Spectral_Peaks.html
*/
actual_bin = bin[0] + (side[0] - side[1])/(2.0 * (side[0] - 2*peak[0] + side[1]));
printf("reading : %f\n", actual_bin * sample_rate / buffer_size + exact_rx_lo);
printf("error : %f\n", fabs((actual_bin * sample_rate / buffer_size + exact_rx_lo) -
(double)external_tone));
printf("guess : %lf\n", actual_bin);
/* fftw folds things, so:
* sample 0 is LO, buffer_size/2 is +FS/2
* (buffer_size/2)+1 is -FS/2, and buffer_size is LO
*/
if (bin[0] < (buffer_size/2)) {
double ppm;
ppm = (double)(external_tone * buffer_size) /
(xo * (mult_fs * actual_bin + mult_lo * buffer_size));
printf("error = %2.4f ppm\n", (1-ppm) * 1000000);
printf("old xo : %lli\n", xo);
if (abs(1 - ppm) * 1000000 <= 25)
xo = (int)round(xo * ppm);
else
printf("calculated ppm value too high %f\n", (1 - ppm) * 1000000);
printf("new xo : %lli\n", xo);
} else {
}
iio_device_attr_write_longlong(iio_context_find_device(ctx, "ad9361-phy"), "xo_correction", xo);
// iio_buffer_foreach_sample(buffer, print_sample, NULL);
}
app_running = false;
}
for (c = wl_index - 1; c >= 0; c--) {
iio_set_attribute(write_log[c].device, write_log[c].channel, write_log[c].in_out,
write_log[c].attribute, write_log[c].value, false);
}
iio_buffer_destroy(buffer);
iio_context_destroy(ctx);
return exit_code;
}