forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.c
2185 lines (1975 loc) · 53.4 KB
/
plot.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <string.h>
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "nanovna.h"
#define SWAP(x,y) do { int z=x; x = y; y = z; } while(0)
static void cell_draw_marker_info(int m, int n, int w, int h);
void frequency_string(char *buf, size_t len, int32_t freq);
void frequency_string_short(char *buf, size_t len, int32_t freq, char prefix);
void markmap_all_markers(void);
//#define GRID_COLOR 0x0863
//uint16_t grid_color = 0x1084;
/* indicate dirty cells */
uint16_t markmap[2][10];
uint16_t current_mappage = 0;
int32_t fgrid = 50000000;
int16_t grid_offset;
int16_t grid_width;
int area_width = AREA_WIDTH_NORMAL;
int area_height = HEIGHT;
#define GRID_RECTANGULAR (1<<0)
#define GRID_SMITH (1<<1)
#define GRID_ADMIT (1<<2)
#define GRID_POLAR (1<<3)
#define CELLWIDTH 32
#define CELLHEIGHT 32
/*
* CELL_X0[27:31] cell position
* CELL_Y0[22:26]
* CELL_N[10:21] original order
* CELL_X[5:9] position in the cell
* CELL_Y[0:4]
*/
uint32_t trace_index[TRACES_MAX][SWEEP_POINTS];
#define INDEX(x, y, n) \
((((x)&0x03e0UL)<<22) | (((y)&0x03e0UL)<<17) | (((n)&0x0fffUL)<<10) \
| (((x)&0x1fUL)<<5) | ((y)&0x1fUL))
#define CELL_X(i) (int)((((i)>>5)&0x1f) | (((i)>>22)&0x03e0))
#define CELL_Y(i) (int)(((i)&0x1f) | (((i)>>17)&0x03e0))
#define CELL_N(i) (int)(((i)>>10)&0xfff)
#define CELL_X0(i) (int)(((i)>>22)&0x03e0)
#define CELL_Y0(i) (int)(((i)>>17)&0x03e0)
#define CELL_P(i, x, y) (((((x)&0x03e0UL)<<22) | (((y)&0x03e0UL)<<17)) == ((i)&0xffc00000UL))
void update_grid(void)
{
int32_t gdigit = 100000000;
int32_t fstart, fspan;
int32_t grid;
if (frequency1 > 0) {
fstart = frequency0;
fspan = frequency1 - frequency0;
} else {
fspan = -frequency1;
fstart = frequency0 - fspan/2;
}
while (gdigit > 100) {
grid = 5 * gdigit;
if (fspan / grid >= 4)
break;
grid = 2 * gdigit;
if (fspan / grid >= 4)
break;
grid = gdigit;
if (fspan / grid >= 4)
break;
gdigit /= 10;
}
fgrid = grid;
grid_offset = (WIDTH-1) * ((fstart % fgrid) / 100) / (fspan / 100);
grid_width = (WIDTH-1) * (fgrid / 100) / (fspan / 1000);
force_set_markmap();
redraw_request |= REDRAW_FREQUENCY;
}
static inline int
circle_inout(int x, int y, int r)
{
int d = x*x + y*y - r*r;
if (d <= -r)
return 1;
if (d > r)
return -1;
return 0;
}
#define P_CENTER_X (LCD_WIDTH/2-OFFSETX-CELLOFFSETX)
#define P_CENTER_Y (LCD_HEIGHT/2-4)
#define P_RADIUS P_CENTER_Y
static int
polar_grid(int x, int y)
{
int c = config.grid_color;
int d;
// offset to center
x -= P_CENTER_X;
y -= P_CENTER_Y;
// outer circle
d = circle_inout(x, y, P_RADIUS);
if (d < 0) return 0;
if (d == 0) return c;
// vertical and horizontal axis
if (x == 0 || y == 0)
return c;
d = circle_inout(x, y, P_RADIUS / 5);
if (d == 0) return c;
if (d > 0) return 0;
d = circle_inout(x, y, P_RADIUS * 2 / 5);
if (d == 0) return c;
if (d > 0) return 0;
// cross sloping lines
if (x == y || x == -y)
return c;
d = circle_inout(x, y, P_RADIUS * 3 / 5);
if (d == 0) return c;
if (d > 0) return 0;
d = circle_inout(x, y, P_RADIUS * 4 / 5);
if (d == 0) return c;
return 0;
}
/*
* Constant Resistance circle: (u - r/(r+1))^2 + v^2 = 1/(r+1)^2
* Constant Reactance circle: (u - 1)^2 + (v-1/x)^2 = 1/x^2
*/
int
smith_grid(int x, int y)
{
int c = config.grid_color;
int d;
// offset to center
x -= P_CENTER_X;
y -= P_CENTER_Y;
// outer circle
d = circle_inout(x, y, P_RADIUS);
if (d < 0)
return 0;
if (d == 0)
return c;
// horizontal axis
if (y == 0)
return c;
// shift circle center to right origin
x -= P_RADIUS;
// Constant Reactance Circle: 2j : R/2 = (P_RADIUS/2)
if (circle_inout(x, y+(P_RADIUS/2), (P_RADIUS/2)) == 0)
return c;
if (circle_inout(x, y-(P_RADIUS/2), (P_RADIUS/2)) == 0)
return c;
// Constant Resistance Circle: 3 : R/4 = (P_RADIUS/4)
d = circle_inout(x+(P_RADIUS/4), y, (P_RADIUS/4));
if (d > 0) return 0;
if (d == 0) return c;
// Constant Reactance Circle: 1j : R = P_RADIUS
if (circle_inout(x, y+P_RADIUS, P_RADIUS) == 0)
return c;
if (circle_inout(x, y-P_RADIUS, P_RADIUS) == 0)
return c;
// Constant Resistance Circle: 1 : R/2 = (P_RADIUS/2)
d = circle_inout(x+(P_RADIUS/2), y, (P_RADIUS/2));
if (d > 0) return 0;
if (d == 0) return c;
// Constant Reactance Circle: 1/2j : R*2 = (P_RADIUS*2)
if (circle_inout(x, y+(P_RADIUS*2), (P_RADIUS*2)) == 0)
return c;
if (circle_inout(x, y-(P_RADIUS*2), (P_RADIUS*2)) == 0)
return c;
// Constant Resistance Circle: 1/3 : R*3/4 = (P_RADIUS*3/4)
if (circle_inout(x+(P_RADIUS*3/4), y, (P_RADIUS*3/4)) == 0)
return c;
return 0;
}
#if 0
int
smith_grid2(int x, int y, float scale)
{
int c = config.grid_color;
int d;
// offset to center
x -= P_CENTER_X;
y -= P_CENTER_Y;
// outer circle
d = circle_inout(x, y, P_RADIUS);
if (d < 0)
return 0;
if (d == 0)
return c;
// shift circle center to right origin
x -= P_RADIUS * scale;
// Constant Reactance Circle: 2j : R/2 = (P_RADIUS/2)
if (circle_inout(x, y+(P_RADIUS/2)*scale, (P_RADIUS/2)*scale) == 0)
return c;
if (circle_inout(x, y-(P_RADIUS/2)*scale, (P_RADIUS/2)*scale) == 0)
return c;
#if 0
// Constant Resistance Circle: 3 : R/4 = (P_RADIUS/4)
d = circle_inout(x+(P_RADIUS/4)*scale, y, (P_RADIUS/4)*scale);
if (d > 0) return 0;
if (d == 0) return c;
d = circle_inout(x-(P_RADIUS/4)*scale, y, (P_RADIUS/4)*scale);
if (d > 0) return 0;
if (d == 0) return c;
#endif
// Constant Reactance Circle: 1j : R = P_RADIUS
if (circle_inout(x, y+P_RADIUS*scale, P_RADIUS*scale) == 0)
return c;
if (circle_inout(x, y-P_RADIUS*scale, P_RADIUS*scale) == 0)
return c;
// Constant Resistance Circle: 1 : R/2 = (P_RADIUS/2)
d = circle_inout(x+(P_RADIUS/2)*scale, y, (P_RADIUS/2)*scale);
if (d > 0) return 0;
if (d == 0) return c;
d = circle_inout(x-(P_RADIUS/2)*scale, y, (P_RADIUS/2)*scale);
if (d > 0) return 0;
if (d == 0) return c;
// Constant Reactance Circle: 1/2j : R*2 = (P_RADIUS*2)
if (circle_inout(x, y+(P_RADIUS*2)*scale, (P_RADIUS*2)*scale) == 0)
return c;
if (circle_inout(x, y-(P_RADIUS*2)*scale, (P_RADIUS*2)*scale) == 0)
return c;
#if 0
// Constant Resistance Circle: 1/3 : R*3/4 = (P_RADIUS*3/4)
d = circle_inout(x+(P_RADIUS*3/4)*scale, y, (P_RADIUS*3/4)*scale);
if (d > 0) return 0;
if (d == 0) return c;
d = circle_inout(x+(P_RADIUS*3/4)*scale, y, (P_RADIUS*3/4)*scale);
if (d > 0) return 0;
if (d == 0) return c;
#endif
// Constant Resistance Circle: 0 : R
d = circle_inout(x+P_RADIUS*scale, y, P_RADIUS*scale);
if (d > 0) return 0;
if (d == 0) return c;
d = circle_inout(x-P_RADIUS*scale, y, P_RADIUS*scale);
if (d > 0) return 0;
if (d == 0) return c;
// Constant Resistance Circle: -1/3 : R*3/2 = (P_RADIUS*3/2)
d = circle_inout(x+(P_RADIUS*3/2)*scale, y, (P_RADIUS*3/2)*scale);
if (d > 0) return 0;
if (d == 0) return c;
d = circle_inout(x-(P_RADIUS*3/2)*scale, y, (P_RADIUS*3/2)*scale);
//if (d > 0) return 0;
if (d == 0) return c;
return 0;
}
#endif
const int cirs[][4] = {
{ 0, (P_RADIUS/2)/2, (P_RADIUS/2)/2, 0 }, // Constant Reactance Circle: 2j : R/2 = (P_RADIUS/2)
{ (P_RADIUS/4)/2, 0, (P_RADIUS/4)/2, 1 }, // Constant Resistance Circle: 3 : R/4 = (P_RADIUS/4)
{ 0, P_RADIUS/2, P_RADIUS/2, 0 }, // Constant Reactance Circle: 1j : R = P_RADIUS
{ (P_RADIUS/2)/2, 0, (P_RADIUS/2)/2, 1 }, // Constant Resistance Circle: 1 : R/2 = (P_RADIUS/2)
{ 0, (P_RADIUS*2)/2, (P_RADIUS*2)/2, 0 }, // Constant Reactance Circle: 1/2j : R*2 = (P_RADIUS*2)
{ (P_RADIUS*3/4)/2, 0, (P_RADIUS*3/4)/2, 1 }, // Constant Resistance Circle: 1/3 : R*3/4 = (P_RADIUS*3/4)
{ 0, (P_RADIUS*4)/2, (P_RADIUS*4)/2, 0 }, // Constant Reactance Circle: 1/4j : R*4 = (P_RADIUS*4)
{ P_RADIUS/2, 0, P_RADIUS/2, 1 }, // Constant Resistance Circle: 0 : R
{ (P_RADIUS*3/2)/2, 0, (P_RADIUS*3/2)/2, 1 }, // Constant Resistance Circle: -1/3 : R*3/2 = (P_RADIUS*3/2)
{ 0, 0, 0, 0 } // sentinel
};
int
smith_grid3(int x, int y)
{
int c = config.grid_color;
int d;
// offset to center
x -= P_CENTER_X;
y -= P_CENTER_Y;
// outer circle
d = circle_inout(x, y, P_RADIUS);
if (d < 0)
return 0;
if (d == 0)
return c;
// shift circle center to right origin
x -= P_RADIUS /2;
int i;
for (i = 0; cirs[i][2]; i++) {
d = circle_inout(x+cirs[i][0], y+cirs[i][1], cirs[i][2]);
if (d == 0)
return c;
if (d > 0 && cirs[i][3])
return 0;
d = circle_inout(x-cirs[i][0], y-cirs[i][1], cirs[i][2]);
if (d == 0)
return c;
if (d > 0 && cirs[i][3])
return 0;
}
return 0;
}
#if 0
int
rectangular_grid(int x, int y)
{
int c = config.grid_color;
//#define FREQ(x) (((x) * (fspan / 1000) / (WIDTH-1)) * 1000 + fstart)
//int32_t n = FREQ(x-1) / fgrid;
//int32_t m = FREQ(x) / fgrid;
//if ((m - n) > 0)
//if (((x * 6) % (WIDTH-1)) < 6)
//if (((x - grid_offset) % grid_width) == 0)
if (x == 0 || x == WIDTH-1)
return c;
if ((y % GRIDY) == 0)
return c;
if ((((x + grid_offset) * 10) % grid_width) < 10)
return c;
return 0;
}
#endif
static int
rectangular_grid_x(int x)
{
int c = config.grid_color;
if (x < 0)
return 0;
if (x == 0 || x == WIDTH)
return c;
if ((((x + grid_offset) * 10) % grid_width) < 10)
return c;
return 0;
}
static int
rectangular_grid_y(int y)
{
int c = config.grid_color;
if (y < 0)
return 0;
if ((y % GRIDY) == 0)
return c;
return 0;
}
#if 0
int
set_strut_grid(int x)
{
uint16_t *buf = spi_buffer;
int y;
for (y = 0; y < HEIGHT; y++) {
int c = rectangular_grid(x, y);
c |= smith_grid(x, y);
*buf++ = c;
}
return y;
}
void
draw_on_strut(int v0, int d, int color)
{
int v;
int v1 = v0 + d;
if (v0 < 0) v0 = 0;
if (v1 < 0) v1 = 0;
if (v0 >= HEIGHT) v0 = HEIGHT-1;
if (v1 >= HEIGHT) v1 = HEIGHT-1;
if (v0 == v1) {
v = v0; d = 2;
} else if (v0 < v1) {
v = v0; d = v1 - v0 + 1;
} else {
v = v1; d = v0 - v1 + 1;
}
while (d-- > 0)
spi_buffer[v++] |= color;
}
#endif
/*
* calculate log10(abs(gamma))
*/
float logmag(const float *v)
{
return log10f(v[0]*v[0] + v[1]*v[1]) * 10;
}
/*
* calculate phase[-2:2] of coefficient
*/
float phase(const float *v)
{
return 2 * atan2f(v[1], v[0]) / M_PI * 90;
}
/*
* calculate groupdelay
*/
float groupdelay(const float *v, const float *w, float deltaf)
{
#if 1
// atan(w)-atan(v) = atan((w-v)/(1+wv))
float r = w[0]*v[1] - w[1]*v[0];
float i = w[0]*v[0] + w[1]*v[1];
return atan2f(r, i) / (2 * M_PI * deltaf);
#else
return (atan2f(w[0], w[1]) - atan2f(v[0], v[1])) / (2 * M_PI * deltaf);
#endif
}
/*
* calculate abs(gamma)
*/
float linear(const float *v)
{
return - sqrtf(v[0]*v[0] + v[1]*v[1]);
}
/*
* calculate vswr; (1+gamma)/(1-gamma)
*/
float swr(const float *v)
{
float x = sqrtf(v[0]*v[0] + v[1]*v[1]);
if (x > 1)
return INFINITY;
return (1 + x)/(1 - x);
}
float resitance(const float *v) {
float z0 = 50;
float d = z0 / ((1-v[0])*(1-v[0])+v[1]*v[1]);
float zr = ((1+v[0])*(1-v[0]) - v[1]*v[1]) * d;
return zr;
}
float reactance(const float *v) {
float z0 = 50;
float d = z0 / ((1-v[0])*(1-v[0])+v[1]*v[1]);
float zi = 2*v[1] * d;
return zi;
}
#define RADIUS ((HEIGHT-1)/2)
void
cartesian_scale(float re, float im, int *xp, int *yp, float scale)
{
//float scale = 4e-3;
int x = re * RADIUS * scale;
int y = im * RADIUS * scale;
if (x < -RADIUS) x = -RADIUS;
if (y < -RADIUS) y = -RADIUS;
if (x > RADIUS) x = RADIUS;
if (y > RADIUS) y = RADIUS;
*xp = WIDTH/2 + x;
*yp = HEIGHT/2 - y;
}
float
groupdelay_from_array(int i, float array[101][2])
{
if (i == 0) {
float deltaf = frequencies[1] - frequencies[0];
return groupdelay(array[0], array[1], deltaf);
} else if (i == 100) {
float deltaf = frequencies[i] - frequencies[i-1];
return groupdelay(array[i-1], array[i], deltaf);
} else {
float deltaf = frequencies[i+1] - frequencies[i-1];
return groupdelay(array[i-1], array[i+1], deltaf);
}
}
uint32_t
trace_into_index(int x, int t, int i, float array[101][2])
{
int y = 0;
float v = 0;
float *coeff = array[i];
float refpos = 8 - get_trace_refpos(t);
float scale = 1 / get_trace_scale(t);
switch (trace[t].type) {
case TRC_LOGMAG:
v = refpos - logmag(coeff) * scale;
break;
case TRC_PHASE:
v = refpos - phase(coeff) * scale;
break;
case TRC_DELAY:
v = refpos - groupdelay_from_array(i, array) * scale;
break;
case TRC_LINEAR:
v = refpos + linear(coeff) * scale;
break;
case TRC_SWR:
v = refpos+ (1 - swr(coeff)) * scale;
break;
case TRC_REAL:
v = refpos - coeff[0] * scale;
break;
case TRC_IMAG:
v = refpos - coeff[1] * scale;
break;
case TRC_R:
v = refpos - resitance(coeff) * scale;
break;
case TRC_X:
v = refpos - reactance(coeff) * scale;
break;
case TRC_SMITH:
//case TRC_ADMIT:
case TRC_POLAR:
cartesian_scale(coeff[0], coeff[1], &x, &y, scale);
return INDEX(x +CELLOFFSETX, y, i);
break;
}
if (v < 0) v = 0;
if (v > 8) v = 8;
y = v * GRIDY;
return INDEX(x +CELLOFFSETX, y, i);
}
static int
string_value_with_prefix(char *buf, int len, float val, char unit)
{
char prefix;
int n = 0;
if (val < 0) {
val = -val;
*buf = '-';
n++;
len--;
}
if (val < 1e-12) {
prefix = 'f';
val *= 1e15;
} else if (val < 1e-9) {
prefix = 'p';
val *= 1e12;
} else if (val < 1e-6) {
prefix = 'n';
val *= 1e9;
} else if (val < 1e-3) {
prefix = S_MICRO[0];
val *= 1e6;
} else if (val < 1) {
prefix = 'm';
val *= 1e3;
} else if (val < 1e3) {
prefix = 0;
} else if (val < 1e6) {
prefix = 'k';
val /= 1e3;
} else if (val < 1e9) {
prefix = 'M';
val /= 1e6;
} else {
prefix = 'G';
val /= 1e9;
}
if (val < 10) {
n += chsnprintf(&buf[n], len, "%.2f", val);
} else if (val < 100) {
n += chsnprintf(&buf[n], len, "%.1f", val);
} else {
n += chsnprintf(&buf[n], len, "%d", (int)val);
}
if (prefix)
buf[n++] = prefix;
if (unit)
buf[n++] = unit;
buf[n] = '\0';
return n;
}
#define PI2 6.283184
static void
format_smith_value(char *buf, int len, const float coeff[2], uint32_t frequency)
{
// z = (gamma+1)/(gamma-1) * z0
float z0 = 50;
float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]);
float zr = ((1+coeff[0])*(1-coeff[0]) - coeff[1]*coeff[1]) * d;
float zi = 2*coeff[1] * d;
int n;
switch (uistat.marker_smith_format) {
case MS_LIN:
chsnprintf(buf, len, "%.2f %.1f" S_DEGREE, linear(coeff), phase(coeff));
break;
case MS_LOG: {
float v = logmag(coeff);
if (v == -INFINITY)
chsnprintf(buf, len, "-INF dB");
else
chsnprintf(buf, len, "%.1fdB %.1f" S_DEGREE, v, phase(coeff));
}
break;
case MS_REIM:
n = string_value_with_prefix(buf, len, coeff[0], '\0');
if (coeff[1] >= 0) buf[n++] = '+';
string_value_with_prefix(buf+n, len-n, coeff[1], 'j');
break;
case MS_RX:
n = string_value_with_prefix(buf, len, zr, S_OHM[0]);
buf[n++] = ' ';
string_value_with_prefix(buf+n, len-n, zi, 'j');
break;
case MS_RLC:
n = string_value_with_prefix(buf, len, zr, S_OHM[0]);
buf[n++] = ' ';
if (zi < 0) {
float c = -1 / (PI2 * frequency * zi);
string_value_with_prefix(buf+n, len-n, c, 'F');
} else {
float l = zi / (PI2 * frequency);
string_value_with_prefix(buf+n, len-n, l, 'H');
}
break;
}
}
static void
gamma2resistance(char *buf, int len, const float coeff[2])
{
float z0 = 50;
float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]);
float zr = ((1+coeff[0])*(1-coeff[0]) - coeff[1]*coeff[1]) * d;
string_value_with_prefix(buf, len, zr, S_OHM[0]);
}
static void
gamma2reactance(char *buf, int len, const float coeff[2])
{
float z0 = 50;
float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]);
float zi = 2*coeff[1] * d;
string_value_with_prefix(buf, len, zi, S_OHM[0]);
}
static void
trace_get_value_string(int t, char *buf, int len, float array[101][2], int i)
{
float *coeff = array[i];
float v;
switch (trace[t].type) {
case TRC_LOGMAG:
v = logmag(coeff);
if (v == -INFINITY)
chsnprintf(buf, len, "-INF dB");
else
chsnprintf(buf, len, "%.2fdB", v);
break;
case TRC_PHASE:
v = phase(coeff);
chsnprintf(buf, len, "%.2f" S_DEGREE, v);
break;
case TRC_DELAY:
v = groupdelay_from_array(i, array);
string_value_with_prefix(buf, len, v, 's');
break;
case TRC_LINEAR:
v = linear(coeff);
chsnprintf(buf, len, "%.2f", v);
break;
case TRC_SWR:
v = swr(coeff);
chsnprintf(buf, len, "%.2f", v);
break;
case TRC_SMITH:
format_smith_value(buf, len, coeff, frequencies[i]);
break;
case TRC_REAL:
chsnprintf(buf, len, "%.2f", coeff[0]);
break;
case TRC_IMAG:
chsnprintf(buf, len, "%.2fj", coeff[1]);
break;
case TRC_R:
gamma2resistance(buf, len, coeff);
break;
case TRC_X:
gamma2reactance(buf, len, coeff);
break;
//case TRC_ADMIT:
case TRC_POLAR:
chsnprintf(buf, len, "%.2f %.2fj", coeff[0], coeff[1]);
break;
}
}
static void
trace_get_value_string_delta(int t, char *buf, int len, float array[101][2], int index, int index_ref)
{
float *coeff = array[index];
float *coeff_ref = array[index_ref];
float v;
switch (trace[t].type) {
case TRC_LOGMAG:
v = logmag(coeff) - logmag(coeff_ref);
if (v == -INFINITY)
chsnprintf(buf, len, S_DELTA "-INF dB");
else
chsnprintf(buf, len, S_DELTA "%.2fdB", v);
break;
case TRC_PHASE:
v = phase(coeff) - phase(coeff_ref);
chsnprintf(buf, len, S_DELTA "%.2f" S_DEGREE, v);
break;
case TRC_DELAY:
v = groupdelay_from_array(index, array) - groupdelay_from_array(index_ref, array);
string_value_with_prefix(buf, len, v, 's');
break;
case TRC_LINEAR:
v = linear(coeff) - linear(coeff_ref);
chsnprintf(buf, len, S_DELTA "%.2f", v);
break;
case TRC_SWR:
v = swr(coeff) - swr(coeff_ref);
chsnprintf(buf, len, S_DELTA "%.2f", v);
break;
case TRC_SMITH:
format_smith_value(buf, len, coeff, frequencies[index]);
break;
case TRC_REAL:
chsnprintf(buf, len, S_DELTA "%.2f", coeff[0] - coeff_ref[0]);
break;
case TRC_IMAG:
chsnprintf(buf, len, S_DELTA "%.2fj", coeff[1] - coeff_ref[1]);
break;
case TRC_R:
gamma2resistance(buf, len, coeff);
break;
case TRC_X:
gamma2reactance(buf, len, coeff);
break;
//case TRC_ADMIT:
case TRC_POLAR:
chsnprintf(buf, len, "%.2f %.2fj", coeff[0], coeff[1]);
break;
}
}
void
trace_get_info(int t, char *buf, int len)
{
const char *type = get_trace_typename(t);
int n;
switch (trace[t].type) {
case TRC_LOGMAG:
chsnprintf(buf, len, "%s %ddB/", type, (int)get_trace_scale(t));
break;
case TRC_PHASE:
chsnprintf(buf, len, "%s %d" S_DEGREE "/", type, (int)get_trace_scale(t));
break;
case TRC_SMITH:
//case TRC_ADMIT:
case TRC_POLAR:
chsnprintf(buf, len, "%s %.1fFS", type, get_trace_scale(t));
break;
default:
n = chsnprintf(buf, len, "%s ", type);
string_value_with_prefix(buf+n, len-n, get_trace_scale(t), '/');
break;
}
}
static float time_of_index(int idx) {
return 1.0 / (float)(frequencies[1] - frequencies[0]) / (float)FFT_SIZE * idx;
}
static float distance_of_index(int idx) {
#define SPEED_OF_LIGHT 299792458
float distance = ((float)idx * (float)SPEED_OF_LIGHT) / ( (float)(frequencies[1] - frequencies[0]) * (float)FFT_SIZE * 2.0);
return distance * (velocity_factor / 100.0);
}
static inline void
mark_map(int x, int y)
{
if (y >= 0 && y < 10 && x >= 0 && x < 16)
markmap[current_mappage][y] |= 1<<x;
}
static inline int
is_mapmarked(int x, int y)
{
uint16_t bit = 1<<x;
return (markmap[0][y] & bit) || (markmap[1][y] & bit);
}
static inline void
markmap_upperarea(void)
{
markmap[current_mappage][0] |= 0xffff;
}
static inline void
swap_markmap(void)
{
current_mappage = 1 - current_mappage;
}
static inline void
clear_markmap(void)
{
memset(markmap[current_mappage], 0, sizeof markmap[current_mappage]);
}
inline void
force_set_markmap(void)
{
memset(markmap[current_mappage], 0xff, sizeof markmap[current_mappage]);
}
void
mark_cells_from_index(void)
{
int t;
/* mark cells between each neighber points */
for (t = 0; t < TRACES_MAX; t++) {
if (!trace[t].enabled)
continue;
int x0 = CELL_X(trace_index[t][0]);
int y0 = CELL_Y(trace_index[t][0]);
int m0 = x0 >> 5;
int n0 = y0 >> 5;
int i;
mark_map(m0, n0);
for (i = 1; i < sweep_points; i++) {
int x1 = CELL_X(trace_index[t][i]);
int y1 = CELL_Y(trace_index[t][i]);
int m1 = x1 >> 5;
int n1 = y1 >> 5;
while (m0 != m1 || n0 != n1) {
if (m0 == m1) {
if (n0 < n1) n0++; else n0--;
} else if (n0 == n1) {
if (m0 < m1) m0++; else m0--;
} else {
int x = (m0 < m1) ? (m0 + 1)<<5 : m0<<5;
int y = (n0 < n1) ? (n0 + 1)<<5 : n0<<5;
int sgn = (n0 < n1) ? 1 : -1;
if (sgn*(y-y0)*(x1-x0) < sgn*(x-x0)*(y1-y0)) {
if (m0 < m1) m0++;
else m0--;
} else {
if (n0 < n1) n0++;
else n0--;
}
}
mark_map(m0, n0);
}
x0 = x1;
y0 = y1;
m0 = m1;
n0 = n1;
}
}
}
void plot_into_index(float measured[2][SWEEP_POINTS][2])
{
int i, t;
for (i = 0; i < sweep_points; i++) {
int x = i * (WIDTH-1) / (sweep_points-1);
for (t = 0; t < TRACES_MAX; t++) {
if (!trace[t].enabled)
continue;
int n = trace[t].channel;
trace_index[t][i] = trace_into_index(x, t, i, measured[n]);
}
}
#if 0
for (t = 0; t < TRACES_MAX; t++)
if (trace[t].enabled && trace[t].polar)
quicksort(trace_index[t], 0, sweep_points);
#endif
mark_cells_from_index();
markmap_all_markers();
}
const uint8_t INSIDE = 0b0000;
const uint8_t LEFT = 0b0001;
const uint8_t RIGHT = 0b0010;
const uint8_t BOTTOM = 0b0100;
const uint8_t TOP = 0b1000;
inline static uint8_t
_compute_outcode(int w, int h, int x, int y)
{
uint8_t code = 0;
if (x < 0) {
code |= LEFT;
} else
if (x > w) {
code |= RIGHT;
}
if (y < 0) {
code |= BOTTOM;
} else
if (y > h) {
code |= TOP;
}
return code;
}
static void
cell_drawline(int w, int h, int x0, int y0, int x1, int y1, int c)
{
uint8_t outcode0 = _compute_outcode(w, h, x0, y0);
uint8_t outcode1 = _compute_outcode(w, h, x1, y1);
if (outcode0 & outcode1) {
// this line is out of requested area. early return
return;
}
if (x0 > x1) {
SWAP(x0, x1);
SWAP(y0, y1);
}
int dx = x1 - x0;
int dy = y1 - y0;
int sy = dy > 0 ? 1 : -1;
int e = 0;
dy *= sy;
if (dx >= dy) {
e = dy * 2 - dx;
while (x0 != x1) {
if (y0 >= 0 && y0 < h && x0 >= 0 && x0 < w) spi_buffer[y0*w+x0] |= c;
x0++;
e += dy * 2;
if (e >= 0) {
e -= dx * 2;
y0 += sy;
}
}