-
Notifications
You must be signed in to change notification settings - Fork 0
/
blank1.c
1648 lines (1624 loc) · 43.4 KB
/
blank1.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
// Copyright (c) <2012> <Leif Asbrink>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
#include "globdef.h"
#include "uidef.h"
#include "screendef.h"
#include "fft1def.h"
#include "blnkdef.h"
#include "fft2def.h"
#include "rusage.h"
#include "thrdef.h"
int blnk_pbeg, blnk_pend;
float subtract_onechan_pulse(int p_max, int sub_size)
{
int pa,pb,p0,mask;
int i, j, k, m, wid, imax;
float t1,t2,t3,t4,t5,t6;
float blanker_phase_c1,blanker_phase_c2;
int re_x_int, im_x_int;
float re_x_float, im_x_float;
float retval;
// **************************
// Max amplitude is at p_max in timf2
// Use blanker_phasefunc to compensate for the phase
// rotation of the strongest part of the pulse.
k=refpul_size-2*blanker_pulsewidth;
mask=timf2_mask&-4;
pa=(4*(p_max-blanker_pulsewidth)+mask)&mask;
pb=(4*(p_max+blanker_pulsewidth) )&mask;
i=0;
if(swfloat)
{
while(pa != pb)
{
pa=(pa+4)&mask;
t1=timf2_float[pa ];
t2=timf2_float[pa+1];
t3=blanker_phasefunc[k ];
t4=blanker_phasefunc[k+1];
t5=t1*t3+t2*t4;
k+=2;
t6=t2*t3-t1*t4;
blanker_input[i ]=t5;
blanker_input[i+1]=t6;
i+=2;
}
}
else
{
while(pa != pb)
{
pa=(pa+4)&mask;
t1=timf2_shi[pa ];
t2=timf2_shi[pa+1];
t3=blanker_phasefunc[k ];
t4=blanker_phasefunc[k+1];
t5=t1*t3+t2*t4;
k+=2;
t6=t2*t3-t1*t4;
blanker_input[i ]=t5;
blanker_input[i+1]=t6;
i+=2;
}
}
// Collect the power averaged phase of the 3 center points.
imax=blanker_pulsewidth;
blanker_phase_c1=0;
blanker_phase_c2=0;
t4=0;
for(i=imax-1; i<=imax+1; i++)
{
t1=blanker_input[2*i ];
t2=blanker_input[2*i+1];
t3=sqrt(t1*t1+t2*t2);
blanker_phase_c1+=t3*t1;
blanker_phase_c2+=t3*t2;
}
t1=blanker_phase_c1*blanker_phase_c1+blanker_phase_c2*blanker_phase_c2;
// Return if the pulse is extremely small (should never happen)
if(t1 < 32)return -1;
t1=sqrt(t1);
blanker_phase_c1/=t1;
blanker_phase_c2/=t1;
// Rotate the phase of the input pulse for zero average phase.
// Collect the I and Q powers
wid=2*blanker_pulsewidth;
t3=0;
t4=0;
for(i=0; i<=wid; i++)
{
t1=blanker_input[2*i ];
t2=blanker_input[2*i+1];
blanker_input[2*i ]=blanker_phase_c1*t1+blanker_phase_c2*t2;
blanker_input[2*i+1]=blanker_phase_c1*t2-blanker_phase_c2*t1;
t3+=blanker_input[2*i ]*blanker_input[2*i ];
t4+=blanker_input[2*i+1]*blanker_input[2*i+1];
}
if(t4>0.25*t3)return -1.;
// Now imax points to the maximum value for the current pulse.
// Find the decimals of the position by a parabolic fit.
// Check blanker_init (buf.c) for details.
// Use decimals of position to get index to standard pulses
t4=blanker_input[2*imax-2]-blanker_input[2*imax+2];
t3=2*(blanker_input[2*imax-2]+blanker_input[2*imax+2]-2*blanker_input[2*imax]);
if(t3 == 0)return -2.;
t4/=t3;
if(t4<0)
{
t4=-sqrt(0.5)*sqrt(-t4);
}
else
{
t4=sqrt(0.5)*sqrt(t4);
}
j=MAX_REFPULSES*(t4+0.5)+0.5;
if(j<0)j=0;
if(j>=MAX_REFPULSES)j=MAX_REFPULSES-1;
m=2*blanker_pulindex[j]*refpul_size;
mask=timf2_mask&-4;
p0=(4*(p_max-sub_size/2)+mask)&mask;
pb=(4*(p_max+sub_size/2) )&mask;
k=refpul_size-sub_size;
// scale phase coefficients so we get the total pulse
// from a multiplication of two coefficients.
blanker_phase_c1*=blanker_input[2*imax]*liminfo_amplitude_factor;
blanker_phase_c2*=blanker_input[2*imax]*liminfo_amplitude_factor;
// Subtract the standard pulse and make a new power function.
// Collect new and old total power.
t3=0;
t4=0;
if(swfloat)
{
while(p0 != pb)
{
p0=(p0+4)&mask;
t1=blanker_refpulse[m+k];
t2=blanker_refpulse[m+k+1];
re_x_float=timf2_float[p0 ]-blanker_phase_c1*t1+blanker_phase_c2*t2;
im_x_float=timf2_float[p0+1]-blanker_phase_c1*t2-blanker_phase_c2*t1;
timf2_float[p0 ]=re_x_float;
timf2_float[p0+1]=im_x_float;
t1=re_x_float*re_x_float+im_x_float*im_x_float;
t3+=timf2_pwr_float[p0>>2];
timf2_pwr_float[p0>>2]=t1;
k+=2;
t4+=t1;
}
}
else
{
while(p0 != pb)
{
p0=(p0+4)&mask;
t1=blanker_refpulse[m+k];
t2=blanker_refpulse[m+k+1];
re_x_int=timf2_shi[p0 ]-blanker_phase_c1*t1+blanker_phase_c2*t2;
im_x_int=timf2_shi[p0+1]-blanker_phase_c1*t2-blanker_phase_c2*t1;
timf2_shi[p0 ]=re_x_int;
timf2_shi[p0+1]=im_x_int;
j=re_x_int*re_x_int+im_x_int*im_x_int;
t3+=timf2_pwr_int[p0>>2];
timf2_pwr_int[p0>>2]=j;
k+=2;
t4+=j;
}
}
retval=t4/t3;
if(retval > 0.5)
{
// Pulse subtraction was not sucessful.
// Restore original data and report failure.
p0=(4*(p_max-sub_size/2)+mask)&mask;
k=refpul_size-sub_size;
if(swfloat)
{
while(p0 != pb)
{
p0=(p0+4)&mask;
t1=blanker_refpulse[m+k];
t2=blanker_refpulse[m+k+1];
re_x_float=timf2_float[p0 ]+blanker_phase_c1*t1+blanker_phase_c2*t2;
im_x_float=timf2_float[p0+1]+blanker_phase_c1*t2-blanker_phase_c2*t1;
timf2_float[p0 ]=re_x_float;
timf2_float[p0+1]=im_x_float;
timf2_pwr_float[p0>>2]=re_x_float*re_x_float+im_x_float*im_x_float;
k+=2;
}
}
else
{
while(p0 != pb)
{
p0=(p0+4)&mask;
t1=blanker_refpulse[m+k];
t2=blanker_refpulse[m+k+1];
re_x_int=timf2_shi[p0 ]+blanker_phase_c1*t1+blanker_phase_c2*t2;
im_x_int=timf2_shi[p0+1]+blanker_phase_c1*t2-blanker_phase_c2*t1;
timf2_shi[p0 ]=re_x_int;
timf2_shi[p0+1]=im_x_int;
timf2_pwr_int[p0>>2]=re_x_int*re_x_int+im_x_int*im_x_int;
k+=2;
}
}
return -5;
}
return retval;
}
float subtract_twochan_pulse(int p_max, int sub_size)
{
int pb,p0,mask;
int i, j, k, m, wid, imax;
float pw;
float t1,t2,t3,t4,t5,t6;
float blanker_phase_c1,blanker_phase_c2,c1,c2;
float re_x,im_x,re_y,im_y,re_a,im_a;
float retval;
// **************************
// Max amplitude is at p_max in timf2 which corresponds to
// blanker_pulsewidth/2 in blanker_input.
// Use blanker_phasefunc to compensate for the phase
// rotation of the strongest part of the pulse
// and collect the average phase.
k=refpul_size-2*blanker_pulsewidth;
wid=2*blanker_pulsewidth;
for(i=0; i<=wid; i++)
{
t1=blanker_input[2*i ];
t2=blanker_input[2*i+1];
t3=blanker_phasefunc[k ];
t4=blanker_phasefunc[k+1];
t5=t1*t3+t2*t4;
t6=t2*t3-t1*t4;
blanker_input[2*i ]=t5;
blanker_input[2*i+1]=t6;
k+=2;
}
// Collect the power averaged phase of the 3 center points.
imax=blanker_pulsewidth;
blanker_phase_c1=0;
blanker_phase_c2=0;
t4=0;
for(i=imax-1; i<=imax+1; i++)
{
t1=blanker_input[2*i ];
t2=blanker_input[2*i+1];
t3=sqrt(t1*t1+t2*t2);
blanker_phase_c1+=t3*t1;
blanker_phase_c2+=t3*t2;
}
t1=sqrt(blanker_phase_c1*blanker_phase_c1+blanker_phase_c2*blanker_phase_c2);
if(t1 < 4)return -1;
blanker_phase_c1/=t1;
blanker_phase_c2/=t1;
// Rotate the phase of the input pulse for zero average phase.
// Collect the I and Q powers
t3=0;
t4=0;
for(i=0; i<=wid; i++)
{
t1=blanker_input[2*i ];
t2=blanker_input[2*i+1];
blanker_input[2*i ]=blanker_phase_c1*t1+blanker_phase_c2*t2;
blanker_input[2*i+1]=blanker_phase_c1*t2-blanker_phase_c2*t1;
t3+=blanker_input[2*i ]*blanker_input[2*i ];
t4+=blanker_input[2*i+1]*blanker_input[2*i+1];
}
if(t4>0.25*t3)return -1.;
// Now imax points to the maximum value for the current pulse.
// Find the decimals of the position by a parabolic fit.
// Check blanker_init (buf.c) for details (pulses are stored
// in order to be fitted this way).
// Use decimals of position to get index to standard pulses
t4=blanker_input[2*imax-2]-blanker_input[2*imax+2];
t3=2*(blanker_input[2*imax-2]+blanker_input[2*imax+2]-2*blanker_input[2*imax]);
if(t3 == 0)return -2.;
t4/=2*t3;
if(t4<0)
{
t4=-sqrt(-t4);
}
else
{
t4=sqrt(t4);
}
j=MAX_REFPULSES*(t4+0.5)+0.5;
if(j<0)j=0;
if(j>=MAX_REFPULSES)j=MAX_REFPULSES-1;
m=2*blanker_pulindex[j]*refpul_size;
mask=timf2_mask&-8;
p0=(8*(p_max-sub_size/2)+mask)&mask;
pb=(8*(p_max+sub_size/2) )&mask;
k=refpul_size-sub_size;
// scale phase coefficients so we get the total pulse
// from a multiplication of two coefficients.
// liminfo_amplitude_factor corrects for the fraction of the total
// pulse energy that is lost among the strong signals.
c1=blanker_phase_c1*blanker_input[2*imax]*liminfo_amplitude_factor;
c2=blanker_phase_c2*blanker_input[2*imax]*liminfo_amplitude_factor;
t3=0;
t4=0;
if(swfloat)
{
while(p0 != pb)
{
p0=(p0+8)&mask;
t1=blanker_refpulse[m+k];
t2=blanker_refpulse[m+k+1];
re_a=c1*t1-c2*t2;
im_a=c1*t2+c2*t1;
re_x=timf2_float[p0 ]-blanker_pol_c1*re_a;
im_x=timf2_float[p0+1]-blanker_pol_c1*im_a;
re_y=timf2_float[p0+2]-(blanker_pol_c2*re_a+blanker_pol_c3*im_a);
im_y=timf2_float[p0+3]-(blanker_pol_c2*im_a-blanker_pol_c3*re_a);
timf2_float[p0 ]=re_x;
timf2_float[p0+1]=im_x;
timf2_float[p0+2]=re_y;
timf2_float[p0+3]=im_y;
pw=re_x*re_x+im_x*im_x+re_y*re_y+im_y*im_y;
t3+=timf2_pwr_float[p0>>3];
timf2_pwr_float[p0>>3]=pw;
k+=2;
t4+=pw;
}
}
else
{
while(p0 != pb)
{
p0=(p0+8)&mask;
t1=blanker_refpulse[m+k];
t2=blanker_refpulse[m+k+1];
re_a=c1*t1-c2*t2;
im_a=c1*t2+c2*t1;
re_x=timf2_shi[p0 ]-blanker_pol_c1*re_a;
im_x=timf2_shi[p0+1]-blanker_pol_c1*im_a;
re_y=timf2_shi[p0+2]-(blanker_pol_c2*re_a+blanker_pol_c3*im_a);
im_y=timf2_shi[p0+3]-(blanker_pol_c2*im_a-blanker_pol_c3*re_a);
timf2_shi[p0 ]=re_x;
timf2_shi[p0+1]=im_x;
timf2_shi[p0+2]=re_y;
timf2_shi[p0+3]=im_y;
pw=0.5*(re_x*re_x+im_x*im_x+re_y*re_y+im_y*im_y);
t3+=timf2_pwr_int[p0>>3];
timf2_pwr_int[p0>>3]=pw;
k+=2;
t4+=pw;
}
}
retval=t4/t3;
if(retval > 0.5)
{
// Pulse subtraction was not sucessful.
// Restore original data and report failure.
p0=(8*(p_max-sub_size/2)+mask)&mask;
k=refpul_size-sub_size;
if(swfloat)
{
while(p0 != pb)
{
p0=(p0+8)&mask;
t1=blanker_refpulse[m+k];
t2=blanker_refpulse[m+k+1];
re_a=c1*t1-c2*t2;
im_a=c1*t2+c2*t1;
re_x=timf2_float[p0 ]+blanker_pol_c1*re_a;
im_x=timf2_float[p0+1]+blanker_pol_c1*im_a;
re_y=timf2_float[p0+2]+(blanker_pol_c2*re_a+blanker_pol_c3*im_a);
im_y=timf2_float[p0+3]+(blanker_pol_c2*im_a-blanker_pol_c3*re_a);
timf2_float[p0 ]=re_x;
timf2_float[p0+1]=im_x;
timf2_float[p0+2]=re_y;
timf2_float[p0+3]=im_y;
pw=re_x*re_x+im_x*im_x+re_y*re_y+im_y*im_y;
timf2_pwr_float[p0>>3]=pw;
k+=2;
}
}
else
{
while(p0 != pb)
{
p0=(p0+8)&mask;
t1=blanker_refpulse[m+k];
t2=blanker_refpulse[m+k+1];
re_a=blanker_phase_c1*t1-blanker_phase_c2*t2;
im_a=blanker_phase_c1*t2+blanker_phase_c2*t1;
re_x=timf2_shi[p0 ]+blanker_pol_c1*re_a;
im_x=timf2_shi[p0+1]+blanker_pol_c1*im_a;
re_y=timf2_shi[p0+2]+(blanker_pol_c2*re_a+blanker_pol_c3*im_a);
im_y=timf2_shi[p0+3]+(blanker_pol_c2*im_a-blanker_pol_c3*re_a);
timf2_shi[p0 ]=re_x;
timf2_shi[p0+1]=im_x;
timf2_shi[p0+2]=re_y;
timf2_shi[p0+3]=im_y;
pw=0.5*(re_x*re_x+im_x*im_x+re_y*re_y+im_y*im_y);
timf2_pwr_int[p0>>3]=pw;
k+=2;
}
}
return -5;
}
return retval;
}
int get_pulse_pol(int p_max)
{
int pa,pb,mask;
float t1,t2,re_x,im_x,re_y,im_y,x2,y2,sina,noi2,x2s,y2s;
float re_xy,im_xy;
// *********************************
//Assume that everything in timf2 (weak signal part) is interference
//from a common origin.
//We have 2 complex arrays X and Y.
//( /X is the complex conjugate of X )
//Find the total signals, X2=(X * /X) and (Y * /Y).
//Get the total sum of squares, norm=X2+Y2
//Get the normalised signals squared x2 = (X * /X) / norm
// and y2 = (Y * /Y) / norm
//Get the normalised 'overlap' xy = (X * /Y) / norm
// ******************************
mask=timf2_mask&-8;
pa=(8*(p_max-blanker_pulsewidth)+mask)&mask;
pb=(8*(p_max+blanker_pulsewidth) )&mask;
x2=0;
y2=0;
re_xy=0;
im_xy=0;
if(swfloat)
{
while(pa != pb)
{
pa=(pa+8)&mask;
re_x=timf2_float[pa ];
im_x=timf2_float[pa+1];
re_y=timf2_float[pa+2];
im_y=timf2_float[pa+3];
x2+=re_x*re_x+im_x*im_x;
y2+=re_y*re_y+im_y*im_y;
re_xy+=re_x*re_y+im_x*im_y;
im_xy+=im_x*re_y-re_x*im_y;
}
}
else
{
while(pa != pb)
{
pa=(pa+8)&mask;
re_x=timf2_shi[pa ];
im_x=timf2_shi[pa+1];
re_y=timf2_shi[pa+2];
im_y=timf2_shi[pa+3];
x2+=re_x*re_x+im_x*im_x;
y2+=re_y*re_y+im_y*im_y;
re_xy+=re_x*re_y+im_x*im_y;
im_xy+=im_x*re_y-re_x*im_y;
}
}
t1=x2+y2;
x2/=t1;
y2/=t1;
re_xy/=t1;
im_xy/=t1;
// *****************************************
//Now we have x2,y2 (real values) and xy (complex).
//For explanation purposes, assume im_xy == 0, which corresponds to linear
//polarization. The signal vill then be polarised in a plane.
//a = angle between polarization plane and the horisontal antenna.
//Assume that the noise level n is the same in the two antennas, and that
//the noise is uncorrelated.
//We then find:
// x2 = cos(a)**2 + n**2
// y2 = sin(a)**2 + n**2
// xy = sin(a)*cos(a)
//From this we find: x2 * y2 - xy*xy = n**2 + n**4
//Neglect n**4:
//cos(a)=sqr( x2 - ( x2 * y2 - xy*xy) )
//sin(a)=sqr( y2 - ( x2 * y2 - xy*xy) )
//The transformation formula to use for rotating the polarization
//plane to produce new signals A and B, where A has all the signal and B
//only noise, will then be:
// A = X * cos(a) + Y * sin(a)
// B = Y * cos(a) - X * sin(a)
//Extending to im_xy != 0 the transformation becomes
//re_A=C1*re_X+C2*re_Y-C3*im_Y
//im_A=C1*im_X+C2*im_Y+C3*re_Y
//re_B=C1*re_Y-C2*re_X-C3*im_X
//im_B=C1*im_Y-C2*im_X+C3*re_X
//C1 = cos(a)
//C2 = sin(a) * re_xy / sqr( re_xy**2 + im_xy**2)
//C3 = sin(a) * im_xy / sqr( re_xy**2 + im_xy**2)
// **************************************
t2=re_xy*re_xy+im_xy*im_xy;
noi2=x2*y2-t2;
if(noi2 > 0.15)return -1;
x2s=x2-noi2;
y2s=y2-noi2;
if(x2s > 0)
{
blanker_pol_c1=sqrt(x2s);
if(y2s > 0 && t2 > 0)
{
sina=sqrt(y2s);
blanker_pol_c2=sina*re_xy/sqrt(t2);
blanker_pol_c3=sina*im_xy/sqrt(t2);
t1=sqrt(blanker_pol_c1*blanker_pol_c1+
blanker_pol_c2*blanker_pol_c2+
blanker_pol_c3*blanker_pol_c3);
blanker_pol_c1/=t1;
blanker_pol_c2/=t1;
blanker_pol_c3/=t1;
}
else
{
if(x2 > y2)
{
blanker_pol_c1=1;
blanker_pol_c2=0;
}
else
{
blanker_pol_c1=0;
blanker_pol_c2=1;
}
blanker_pol_c3=0;
}
}
else
{
blanker_pol_c1=0;
blanker_pol_c2=1;
blanker_pol_c3=0;
}
return 1;
}
void transform_timf2_pol(int p_max)
{
int i,pa,pb,mask;
float re_x,im_x,re_y,im_y;
mask=timf2_mask&-8;
pa=(8*(p_max-blanker_pulsewidth)+mask)&mask;
pb=(8*(p_max+blanker_pulsewidth) )&mask;
i=0;
if(swfloat)
{
while(pa != pb)
{
pa=(pa+8)&mask;
re_x=timf2_float[pa ];
im_x=timf2_float[pa+1];
re_y=timf2_float[pa+2];
im_y=timf2_float[pa+3];
blanker_input[i ]= blanker_pol_c1*re_x
+blanker_pol_c2*re_y
-blanker_pol_c3*im_y;
blanker_input[i+1]= blanker_pol_c1*im_x
+blanker_pol_c2*im_y
+blanker_pol_c3*re_y;
i+=2;
}
}
else
{
while(pa != pb)
{
pa=(pa+8)&mask;
re_x=timf2_shi[pa ];
im_x=timf2_shi[pa+1];
re_y=timf2_shi[pa+2];
im_y=timf2_shi[pa+3];
blanker_input[i ]= blanker_pol_c1*re_x
+blanker_pol_c2*re_y
-blanker_pol_c3*im_y;
blanker_input[i+1]= blanker_pol_c1*im_x
+blanker_pol_c2*im_y
+blanker_pol_c3*re_y;
i+=2;
}
}
}
void set_flag(char value, int p_max)
{
int i, pa, pb, p0;
int begin_flag, end_flag;
// This pulse can not be fitted.
// Set flag to "value" over +/- blanker pulsewidth and for as
// long as power decreases.
blanker_flag[p_max]=value;
pa=p_max;
pb=p_max;
for(i=0; i<blanker_pulsewidth; i++)
{
pb=(pb+timf2pow_mask)&timf2pow_mask;
pa=(pa+1)&timf2pow_mask;
blanker_flag[pa]=value;
blanker_flag[pb]=value;
}
p0=pb;
pb=(pb+timf2pow_mask)&timf2pow_mask;
begin_flag=( ((pb-blnk_pbeg+timf2pow_mask)&timf2pow_mask) > timf2pow_mask/2);
if(!begin_flag)
{
if(swfloat)
{
while( timf2_pwr_float[pb] < timf2_pwr_float[p0] && pb!=blnk_pbeg)
{
blanker_flag[pb]=value;
p0=pb;
pb=(pb+timf2pow_mask)&timf2pow_mask;
}
}
else
{
while( timf2_pwr_int[pb] < timf2_pwr_int[p0] && pb!=blnk_pbeg)
{
blanker_flag[pb]=value;
p0=pb;
pb=(pb+timf2pow_mask)&timf2pow_mask;
}
}
}
p0=pa;
pa=(pa+1)&timf2pow_mask;
end_flag=( ((blnk_pend-pa+timf2pow_mask)&timf2pow_mask) > timf2pow_mask/2);
if(!end_flag)
{
if(swfloat)
{
while( timf2_pwr_float[pa] < timf2_pwr_float[p0] && pa!=blnk_pend)
{
p0=pa;
blanker_flag[pa]=value;
pa=(pa+1)&timf2pow_mask;
}
}
else
{
while( timf2_pwr_int[pa] < timf2_pwr_int[p0] && pa!=blnk_pend)
{
p0=pa;
blanker_flag[pa]=value;
pa=(pa+1)&timf2pow_mask;
}
}
}
}
// ****************************************************************
// *******************************************************************
void first_noise_blanker(void)
{
int i, j, k, m, ifirst;
int stupid_clr1, stupid_clr2, mm;
int bln_no, p0, pa, pb, pf, pk;
int cleared_points, total_points;
int fitted_pulses, p_max;
float t0,t1,t2,t3,t4;
float avgpwr[BLN_INFO_SIZE];
unsigned int nfl;
float totnoise;
float sizlim;
unsigned int powermax_uint, pwrlim_uint;
float powermax_float, pwrlim_float;
int timf2_findmax;
unsigned int pulmax;
float pulmax_float;
// timf2_pa is the latest point in timf2 containing valid data.
// timf2p_fit is the latest point for which the blanker has been
// run already.
mm=4*ui.rx_rf_channels;
blnk_pbeg=timf2p_fit;
pf=blnk_pbeg;
// Leave blnfit_range+1 points for next time. (step size=mm=4*ui.rx_rf_channels)
blnk_pend=(timf2_pa/mm-blnfit_range+timf2pow_mask)&timf2pow_mask;
blnk_pend&=0xfffffffc;
total_points=(blnk_pend-blnk_pbeg+1+timf2pow_mask)&timf2pow_mask;
// Do not run this routine more often than 50 times per second.
if(total_points < min_delay_time*ui.rx_ad_speed)
{
return;
}
if(ampinfo_flag != 0)
{
p0=blnk_pbeg;
if(swfloat)
{
t0=0;
t1=0;
if(sw_onechan)
{
while(p0 != blnk_pend )
{
p0=(p0+1)&timf2pow_mask;
k=p0<<2;
if(t0<fabs(timf2_float[k ]))t0=fabs(timf2_float[k ]);
if(t0<fabs(timf2_float[k+1]))t0=fabs(timf2_float[k+1]);
if(t1<fabs(timf2_float[k+2]))t1=fabs(timf2_float[k+2]);
if(t1<fabs(timf2_float[k+3]))t1=fabs(timf2_float[k+3]);
}
}
else
{
t2=0;
t3=0;
while(p0 != blnk_pend )
{
p0=(p0+1)&timf2pow_mask;
k=p0<<3;
if(t0<fabs(timf2_float[k ]))t0=fabs(timf2_float[k ]);
if(t0<fabs(timf2_float[k+1]))t0=fabs(timf2_float[k+1]);
if(t1<fabs(timf2_float[k+2]))t1=fabs(timf2_float[k+2]);
if(t1<fabs(timf2_float[k+3]))t1=fabs(timf2_float[k+3]);
if(t2<fabs(timf2_float[k+4]))t2=fabs(timf2_float[k+4]);
if(t2<fabs(timf2_float[k+5]))t2=fabs(timf2_float[k+5]);
if(t3<fabs(timf2_float[k+6]))t3=fabs(timf2_float[k+6]);
if(t3<fabs(timf2_float[k+7]))t3=fabs(timf2_float[k+7]);
}
if(t2 > 32767)t2=32767;
if(t3 > 32767)t3=32767;
if(timf2_maxamp[2]<t2)timf2_maxamp[2]=t2;
if(timf2_maxamp[3]<t3)timf2_maxamp[3]=t3;
}
if(t0 > 32767)t0=32767;
if(t1 > 32767)t1=32767;
if(timf2_maxamp[0]<t0)timf2_maxamp[0]=t0;
if(timf2_maxamp[1]<t1)timf2_maxamp[1]=t1;
}
else
{
if(sw_onechan)
{
while(p0 != blnk_pend )
{
p0=(p0+1)&timf2pow_mask;
k=p0<<2;
i=abs(timf2_shi[k ]);
if(timf2_maxamp[0]<i)timf2_maxamp[0]=i;
i=abs(timf2_shi[k+1]);
if(timf2_maxamp[0]<i)timf2_maxamp[0]=i;
i=abs(timf2_shi[k+2]);
if(timf2_maxamp[2]<i)timf2_maxamp[2]=i;
i=abs(timf2_shi[k+3]);
if(timf2_maxamp[2]<i)timf2_maxamp[2]=i;
}
}
else
{
while(p0 != blnk_pend )
{
p0=(p0+1)&timf2pow_mask;
k=p0<<3;
i=abs(timf2_shi[k ]);
if(timf2_maxamp[0]<i)timf2_maxamp[0]=i;
i=abs(timf2_shi[k+1]);
if(timf2_maxamp[0]<i)timf2_maxamp[0]=i;
i=abs(timf2_shi[k+2]);
if(timf2_maxamp[1]<i)timf2_maxamp[1]=i;
i=abs(timf2_shi[k+3]);
if(timf2_maxamp[1]<i)timf2_maxamp[1]=i;
i=abs(timf2_shi[k+4]);
if(timf2_maxamp[2]<i)timf2_maxamp[2]=i;
i=abs(timf2_shi[k+5]);
if(timf2_maxamp[2]<i)timf2_maxamp[2]=i;
i=abs(timf2_shi[k+6]);
if(timf2_maxamp[3]<i)timf2_maxamp[3]=i;
i=abs(timf2_shi[k+7]);
if(timf2_maxamp[3]<i)timf2_maxamp[3]=i;
}
}
}
}
fitted_pulses=0;
cleared_points=0;
timf2_findmax=0;
if(hg.clever_bln_mode == 0)
{
goto skip_fit;
}
// Clear blanker_flag. This is a cyclic buffer char variable
p0=blnk_pbeg;
blanker_flag[p0]=0;
while(p0 != blnk_pend)
{
p0=(p0+1)&timf2pow_mask;
blanker_flag[p0]=0;
}
// Set the limit well below the noise floor in case the noise floor
// is raised because we used too few points before.
nfl=hg.clever_bln_limit;
sizlim=0.001*timf2_noise_floor;
fitted_pulses=0;
find_pulse:;
// Step until we find power above the limit.
if(swfloat)
{
while( (timf2_pwr_float[pf] <= nfl || blanker_flag[pf]>64)
&& pf != blnk_pend)pf=(pf+1)&timf2pow_mask;
}
else
{
while( (timf2_pwr_int[pf] <= nfl || blanker_flag[pf]>64)
&& pf != blnk_pend)pf=(pf+1)&timf2pow_mask;
}
if(pf == blnk_pend)goto clever_x;
p0=pf-1;
p_max=pf;
// Anything below 10 in summed power is irrelevant.
powermax_uint=10;
powermax_float=10;
m=blnfit_range;
// ****************************************************
// blanker_flag bit 0 is set each time a value is found
// that is larger than preceding points in current range.
// Values above 64 indicate point is already very much changed.
if(swfloat)
{
while(p0 != blnk_pend && m>0)
{
p0=(p0+1)&timf2pow_mask;
if(timf2_pwr_float[p0] > powermax_float && blanker_flag[p0]<64)
{
blanker_flag[p0]=1;
powermax_float=timf2_pwr_float[p0];
p_max=p0;
m=blnfit_range;
}
m--;
}
}
else
{
while(p0 != blnk_pend && m>0)
{
p0=(p0+1)&timf2pow_mask;
if(timf2_pwr_int[p0] > powermax_uint && blanker_flag[p0]<64)
{
blanker_flag[p0]=1;
powermax_uint=timf2_pwr_int[p0];
p_max=p0;
m=blnfit_range;
}
m--;
}
}
if(m>0)
{
goto clever_x;
}
// Verify that p_max is a maximum and not a point next to a region
// with flag above 64
p0=(p_max+timf2pow_mask)&timf2pow_mask;
if(blanker_flag[p0] >= 64)
{
pf=p_max;
no_pulse:;
if(swfloat)
{
while( (timf2_pwr_float[pf] <= powermax_float || blanker_flag[pf]>64)
&& pf != blnk_pend)
{
powermax_float=timf2_pwr_float[pf];
pf=(pf+1)&timf2pow_mask;
}
}
else
{
while( (timf2_pwr_int[pf] <= powermax_uint || blanker_flag[pf]>64)
&& pf != blnk_pend)
{
powermax_uint=timf2_pwr_int[pf];
pf=(pf+1)&timf2pow_mask;
}
}
if(pf == blnk_pend)goto clever_x;
goto find_pulse;
}
p0=(p_max+1)&timf2pow_mask;
if(p0 == blnk_pend)goto clever_x;
if(blanker_flag[p0] >= 64)goto no_pulse;
if(timf2_findmax == 0)
{
if(swfloat)
{
if(timf2_display_powermax_float < powermax_float)
{
timf2_display_powermax_float=powermax_float;
timf2_display_maxpoint=p_max;
}
if(sw_onechan)
{
k=p_max<<2;
if(timf2_display_maxval_float<fabs(timf2_float[k ]))
timf2_display_maxval_float=fabs(timf2_float[k ]);
if(timf2_display_maxval_float<fabs(timf2_float[k+1]))
timf2_display_maxval_float=fabs(timf2_float[k+1]);
}
else
{
k=p_max<<3;
if(timf2_display_maxval_float<fabs(timf2_float[k ]))
timf2_display_maxval_float=fabs(timf2_float[k ]);
if(timf2_display_maxval_float<fabs(timf2_float[k+1]))
timf2_display_maxval_float=fabs(timf2_float[k+1]);
if(timf2_display_maxval_float<fabs(timf2_float[k+2]))
timf2_display_maxval_float=fabs(timf2_float[k+2]);
if(timf2_display_maxval_float<fabs(timf2_float[k+3]))
timf2_display_maxval_float=fabs(timf2_float[k+3]);
}
}
else
{
if(timf2_display_powermax_uint < powermax_uint)
{
timf2_display_powermax_uint=powermax_uint;
timf2_display_maxpoint=p_max;
}
i=timf2_display_maxval_uint;
if(sw_onechan)
{
k=p_max<<2;
if(i<abs(timf2_shi[k ]))i=abs(timf2_shi[k ]);
if(i<abs(timf2_shi[k+1]))i=abs(timf2_shi[k+1]);
}
else
{
k=p_max<<3;
if(i<abs(timf2_shi[k ]))i=abs(timf2_shi[k ]);
if(i<abs(timf2_shi[k+1]))i=abs(timf2_shi[k+1]);
if(i<abs(timf2_shi[k+2]))i=abs(timf2_shi[k+2]);
if(i<abs(timf2_shi[k+3]))i=abs(timf2_shi[k+3]);
}
if(timf2_display_maxval_uint<(unsigned int)(i))
{
timf2_display_maxval_uint=i;
}
}
}
// We do not want to handle unresolved multiple pulses with
// this rather slow method. Check the average power over bln.size points
// for the widths that may be useful.
bln_no=0;
pa=(p_max+1)&timf2pow_mask;
pb=(p_max+timf2pow_mask)&timf2pow_mask;
k=2;
check_avgpwr:;
if(swfloat)
{
powermax_float=timf2_pwr_float[p_max];
t1=powermax_float*bln[bln_no].rest;
}
else
{
powermax_uint=timf2_pwr_int[p_max];
t1=powermax_uint*bln[bln_no].rest;
}
if(t1 < sizlim)
{
goto get_fit_size;
}
t1=0;
if(swfloat)
{
while(k < bln[bln_no].size)
{
t1+=timf2_pwr_float[pa]+timf2_pwr_float[pb];
pa=(pa+1)&timf2pow_mask;