-
Notifications
You must be signed in to change notification settings - Fork 3
/
afcsub.c
1074 lines (1054 loc) · 26.2 KB
/
afcsub.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 "osnum.h"
#include "globdef.h"
#include "uidef.h"
#include "fft1def.h"
#include "fft2def.h"
#include "screendef.h"
#include "vernr.h"
#include "seldef.h"
#include "llsqdef.h"
void collect_initial_spectrum(void)
{
// char s[80];
int i, j, k, m, ia, ib, ja, jb, ka, kb, first_fqp, fqp;
int np, lowres_points;
int nn,na,nb,ma,mb,n1,n2,n3;
int pol_known;
int fq_points;
float drifti, sina;
float t1, t2, t3, t4, r1, r2, drift, maxval2, minval;
float der;
float noi2,x2s, y2s;
float *pwra;
float fq, ampl[3], pos[3];
int kk, res, imax, jmax, lowres_imax, imax2, jmax2 ;
float x2, y2, re_xy, im_xy;
float *x;
short int * zxy;
TWOCHAN_POWER *pxy;
if(pg.adapt == 0 && ui.rx_rf_channels == 2)pol_known = 0; else pol_known=1;
afcf_search_range=genparm[AFC_LOCK_RANGE]*fftx_points_per_hz*ag.search_range;
if(afcf_search_range < 12)afcf_search_range=12;
fq_points=1.1*afcf_search_range;
if(fq_points < afcf_search_range+20)fq_points=afcf_search_range+20;
if(fq_points > max_afcf_points)
{
fq_points = max_afcf_points;
afcf_search_range=fq_points-20;
}
afc_ia=(fq_points-afcf_search_range)/2;
afc_ib=fq_points-afc_ia;
search_again:;
afc_noise=-1;
// Collect summed power spectra with appropriate frequency shift
first_fqp=mix1_selfreq[ag_ss]*fftx_points_per_hz-fq_points/2;
np=(fftx_na-1+max_fftxn)&fftxn_mask;
drifti=0;
kk=fq_points;
if(sw_onechan)
{
// With one channel only, just use the power spectrum.
for(i=0; i<fq_points*afc_speknum; i++)afc_spectrum[i]=0;
for(i=0; i<ag.fit_points; i++)
{
drift=-drifti*afc_half_speknum;
pwra=&fftx_pwr[np*fftx_size];
for(j=0; j<afc_speknum; j++)
{
fqp=first_fqp+drift;
for(k=0; k<kk; k++)
{
afc_spectrum[fq_points*j+k]+=pwra[k+fqp];
}
drift+=drifti;
}
np=(np+fftxn_mask)&fftxn_mask;
drifti+=afc_drift_step;
}
}
else //ui.rx_rf_channels = 2
{
if(pol_known == 0)
{
// We have two channels and polarization is unknown.
// First make an average of channel powers and correlations assuming
// the different frequency drifts we look for.
for(i=0; i<fq_points*afc_speknum; i++)
{
afc_xysum[i].x2=0;
afc_xysum[i].y2=0;
afc_xysum[i].re_xy=0;
afc_xysum[i].im_xy=0;
}
for(i=0; i<ag.fit_points; i++)
{
drift=-drifti*afc_half_speknum;
pxy=(TWOCHAN_POWER*)(&fftx_xypower[np*fftx_size].x2);
for(j=0; j<afc_speknum; j++)
{
fqp=first_fqp+drift;
for(k=0; k<kk; k++)
{
afc_xysum[fq_points*j+k].x2+=pxy[k+fqp].x2;
afc_xysum[fq_points*j+k].y2+=pxy[k+fqp].y2;
afc_xysum[fq_points*j+k].re_xy+=pxy[k+fqp].re_xy;
afc_xysum[fq_points*j+k].im_xy+=pxy[k+fqp].im_xy;
}
drift+=drifti;
}
np=(np+fftxn_mask)&fftxn_mask;
drifti+=afc_drift_step;
}
// Now extract the power for a signal using the channel correlation
// (Slightly better than just adding x2 and y2)
for(i=0; i<fq_points*afc_speknum; i++)
{
t1=afc_xysum[i].x2+afc_xysum[i].y2;
afc_spectrum[i]=t1 + 2*(afc_xysum[i].re_xy*afc_xysum[i].re_xy
+afc_xysum[i].im_xy*afc_xysum[i].im_xy
-afc_xysum[i].x2*afc_xysum[i].y2 ) / t1;
}
}
else
{
// When the polarization is known we have just one channel.
// The power spectra have to be calculated directly from the transforms
// since the channel is a linear combination of two channels.
for(i=0; i<fq_points*afc_speknum; i++)afc_spectrum[i]=0;
if(swmmx_fft2)
{
for(i=0; i<ag.fit_points; i++)
{
drift=-drifti*afc_half_speknum;
zxy=&fft2_short_int[4*np*fft2_size];
for(j=0; j<afc_speknum; j++)
{
fqp=first_fqp+drift;
for(k=0; k<kk; k++)
{
m=4*(k+fqp);
t1=pg.c1*zxy[m ]+pg.c2*zxy[m+2]+pg.c3*zxy[m+3];
t2=pg.c1*zxy[m+1]+pg.c2*zxy[m+3]-pg.c3*zxy[m+2];
afc_spectrum[fq_points*j+k]+=t1*t1+t2*t2;
}
drift+=drifti;
}
np=(np+fftxn_mask)&fftxn_mask;
drifti+=afc_drift_step;
}
}
else
{
for(i=0; i<ag.fit_points; i++)
{
drift=-drifti*afc_half_speknum;
x=&fftx[np*fftx_size*4];
for(j=0; j<afc_speknum; j++)
{
fqp=first_fqp+drift;
for(k=0; k<kk; k++)
{
m=4*(k+fqp);
t1=pg.c1*x[m ]+pg.c2*x[m+2]+pg.c3*x[m+3];
t2=pg.c1*x[m+1]+pg.c2*x[m+3]-pg.c3*x[m+2];
afc_spectrum[fq_points*j+k]+=t1*t1+t2*t2;
}
drift+=drifti;
}
np=(np+fftxn_mask)&fftxn_mask;
drifti+=afc_drift_step;
}
}
}
}
// Look for the largest value.
afc_maxval=-1;
imax=0;
jmax=0;
ia=afc_ia;
ib=afc_ib;
// In case the user has zoomed in very much the search range
// may reach outside the permitted frequency range.
// Adjust ia and ib in case they are too far out.
j=mix1_lowest_fq*fftx_points_per_hz-first_fqp+5;
if( ia< j)ia=j;
j=mix1_highest_fq*fftx_points_per_hz-first_fqp-5;
if( ib > j)ib=j;
for(j=0; j<afc_speknum; j++)
{
for(k=ia; k<ib; k++)
{
if(afc_maxval<afc_spectrum[fq_points*j+k])
{
afc_maxval=afc_spectrum[fq_points*j+k];
imax=k;
jmax=j;
}
}
}
// Assuming the operator selected a suitable baseband filter we look
// outside the region of the current signal to see if there is another
// candidate.
// This time we look at the spectrum with a resolution
// corresponding to 25% of the baseband filter.
res=baseband_bw_fftxpts/2;
if(res < 2 || fq_points < 6*baseband_bw_fftxpts)goto desicion_made;
//ööö Check this when afc_speknum == 1
lowres_points=fq_points/res;
if(pol_known == 1)
{
// For a single channel just add neighbouring points.
for(j=0; j<afc_speknum; j++)
{
ja=0;
i=0;
for(jb=res; jb<fq_points; jb+=res)
{
afc_lowres_spectrum[i]=0;
for(k=ja; k<jb; k++)
{
afc_lowres_spectrum[i]+=afc_spectrum[fq_points*j+k];
}
ja=jb;
i++;
}
}
}
else
{
// We have two channels and the polarization is unknown.
// Average individual channels and correlations first, then
// calculate power!
for(j=0; j<afc_speknum; j++)
{
ja=0;
i=0;
for(jb=res; jb<fq_points; jb+=res)
{
t1=t2=t3=t4=0;
for(k=ja; k<jb; k++)
{
t1+=afc_xysum[fq_points*j+k].x2;
t2+=afc_xysum[fq_points*j+k].y2;
t3+=afc_xysum[fq_points*j+k].re_xy;
t4+=afc_xysum[fq_points*j+k].im_xy;
}
afc_lowres_spectrum[i]=t1+t2+2*(t3*t3+t4*t4-t1*t2)/(t1+t2);
ja=jb;
i++;
}
}
}
minval=BIGFLOAT;
for(i=1; i<lowres_points-1; i++)
{
if(minval > afc_lowres_spectrum[i])
{
minval=afc_lowres_spectrum[i];
}
}
t1=-1;
lowres_imax=0;
ja=(imax-baseband_bw_fftxpts)/res;
jb=(imax+baseband_bw_fftxpts)/res;
ia/=res;
ib/=res;
for(i=ia; i<ja; i++)
{
if(t1 < afc_lowres_spectrum[i])
{
t1=afc_lowres_spectrum[i];
lowres_imax=i;
}
}
for(i=jb; i<ib; i++)
{
if(t1 < afc_lowres_spectrum[i])
{
t1=afc_lowres_spectrum[i];
lowres_imax=i;
}
}
if(minval < 0.02*t1)minval=0.02*t1;
lowres_imax*=res;
minval/=afc_speknum*res;
// Another signal seems better at a larger bandwidth.
// This one may be the desired signal while the other one may be
// a frequency stable and unmodulated spur.
// Get max point and frequency drift of second candidate.
ka=lowres_imax-baseband_bw_fftxpts;
kb=lowres_imax+baseband_bw_fftxpts;
maxval2=-1;
imax2=0;
jmax2=0;
for(j=0; j<afc_speknum; j++)
{
for(k=ka; k<kb; k++)
{
if(maxval2<afc_spectrum[fq_points*j+k])
{
maxval2=afc_spectrum[fq_points*j+k];
imax2=k;
jmax2=j;
}
}
}
// Get an estimate of the noise floor as the average of points that
// are smaller than 5*minval. For verification, get also the
// average of points that are in the interval 5*minval to 25*minval
if(imax2 > imax)
{
na=ja;
nb=jb;
ma=ka;
mb=kb;
}
else
{
na=ka;
nb=kb;
ma=ja;
mb=jb;
}
t1=0;
t2=0;
n1=0;
n2=0;
r1=5*minval;
r2=25*minval;
kk=0;
for(j=0; j<afc_speknum; j++)
{
for(k=0; k<na; k++)
{
if( afc_spectrum[kk+k] < r2)
{
if( afc_spectrum[kk+k] < r1)
{
t1+=afc_spectrum[kk+k];
n1++;
}
else
{
t2+=afc_spectrum[kk+k];
n2++;
}
}
}
for(k=nb+1; k<ma; k++)
{
if( afc_spectrum[kk+k] < r2)
{
if( afc_spectrum[kk+k] < r1)
{
t1+=afc_spectrum[kk+k];
n1++;
}
else
{
t2+=afc_spectrum[kk+k];
n2++;
}
}
}
for(k=mb+1; k<fq_points; k++)
{
if( afc_spectrum[kk+k] < r2)
{
if( afc_spectrum[kk+k] < r1)
{
t1+=afc_spectrum[kk+k];
n1++;
}
else
{
t2+=afc_spectrum[kk+k];
n2++;
}
}
}
kk+=fq_points;
}
t3=t1+t2;
n3=n1+n2;
// If noise floor could not be established, just use maximum.
if(n3 < 12+(3+fq_points/20)*afc_speknum/2)goto desicion_made;
if(n2 > n3/3)
{
afc_noise=t3/n3;
}
else
{
afc_noise=t1/n1;
}
// If the second signal is not as high above the noise floor
// as the operator specified, use the first signal.
if(maxval2 < ag.minston*afc_noise)goto desicion_made;
// Both signals are strong enough.
// Get the modulation sidebands
t1=2*afc_noise;
for(i=0; i<res; i++)
{
afc_lowres_spectrum[2*i ]=afc_spectrum[fq_points*jmax+imax+i]+
afc_spectrum[fq_points*jmax+imax-i]-t1;
afc_lowres_spectrum[2*i+1]=afc_spectrum[fq_points*jmax2+imax2+i]+
afc_spectrum[fq_points*jmax2+imax2-i]-t1;
}
// This routine is currently for Morse code only.
// Besides a carrier there should be keying sidebands.
// The signal may be phase unstable and smeared out so be careful.
// Get the center of gravity for the signals.
t1=0;
t2=0;
r1=0;
r2=0;
for(i=0; i<res; i++)
{
if( afc_lowres_spectrum[2*i ] <0) afc_lowres_spectrum[2*i ]=0;
t1+=i*afc_lowres_spectrum[2*i ];
r1+=afc_lowres_spectrum[2*i ];
if( afc_lowres_spectrum[2*i+1] <0) afc_lowres_spectrum[2*i+1]=0;
t2+=i*afc_lowres_spectrum[2*i+1];
r2+=afc_lowres_spectrum[2*i+1];
}
t1/=r1*res;
t2/=r2*res;
// An unmodulated spur is typically below 0.05 while a
// normal keyed signal is in the region 0.1 to 0.3
if(t2 > 0.1 && t1 < 0.05)
{
use2:;
imax=imax2;
jmax=jmax2;
afc_maxval=maxval2;
goto desicion_made;
}
if(t1 > 0.1 && t2 < 0.1)goto desicion_made;
if(t1 < 0.05 && t2 < 0.05)goto desicion_made;
if(t1 > 0.1 && t2 > 0.1)
{
if(maxval2/afc_maxval < 0.25)goto desicion_made;
if(fabs(t1-0.2)/(fabs(t2-0.2)+0.000001)>2)goto use2;
}
if(t2/t1 > 2)goto use2;
desicion_made:;
// We now have imax and jmax pointing to a signal.
// We may have a noise floor, but if it is not present, get it now.
if(afc_noise < 0)
{
minval=BIGFLOAT;
kk=fq_points*jmax;
for(i=0; i<fq_points; i++)
{
if(afc_spectrum[kk+i]<minval)minval=afc_spectrum[kk+i];
}
t1=minval*5;
if(t1 < 0.1*afc_maxval)t1=0.1*afc_maxval;
// use 5* lowest or 0.1* maximum to get some idea about the noise level
afc_noise=0;
j=0;
for(i=0; i<fq_points; i++)
{
if(afc_spectrum[kk+i]<t1)
{
afc_noise+=afc_spectrum[kk+i];
j++;
}
}
if(j == 0)goto errexit;
t1=afc_noise*5./j;
// Include points within 5 times the average we obtained in the
// final estimate of the noise floor.
afc_noise=0;
j=0;
for(i=0; i<fq_points; i++)
{
if(afc_spectrum[kk+i]<t1)
{
afc_noise+=afc_spectrum[kk+i];
j++;
}
}
if(j == 0)goto errexit;
afc_noise/=j;
}
if(afc_maxval < ag.minston*afc_noise)
{
// xytext(90,4,"S/N too low ");
// If the signal is poor, store a constant frequency
// and return failure code.
errexit:;
der=afc_half_speknum;
fq=0;
mix1_status[ag_ss]=1;
goto fillfq;
}
mix1_status[ag_ss]=2;
// The signal is good enough.
if(pol_known == 0)
{
// If we have two channels and have found the signal from
// the unpolarised power spectrum.
// (Actually slightly better using correlations!)
// We can determine the polarization from channel correlations
// at the peak.
// get the S/N weighted average over the peak.
kk=fq_points*jmax;
ia=imax-res;
ib=imax+res;
x2=0;
y2=0;
re_xy=0;
im_xy=0;
for(i=ia; i<=ib; i++)
{
t1=(afc_xysum[kk+i].x2+afc_xysum[kk+i].y2)-afc_noise;
if(t1 > 0)
{
x2+=t1*afc_xysum[kk+i].x2;
y2+=t1*afc_xysum[kk+i].y2;
re_xy+=t1*afc_xysum[kk+i].re_xy;
im_xy+=t1*afc_xysum[kk+i].im_xy;
}
}
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;
x2s=x2-noi2;
if(x2s > 0)
{
y2s=y2-noi2;
pg.c1=sqrt(x2s);
if(y2s > 0 && t2 > 0)
{
sina=sqrt(y2s);
pg.c2=sina*re_xy/sqrt(t2);
pg.c3=-sina*im_xy/sqrt(t2);
t1=sqrt(pg.c1*pg.c1+pg.c2*pg.c2+pg.c3*pg.c3)+0.000001;
if(pg.c2 < 0)t1=-t1;
pg.c1/=t1;
pg.c2/=t1;
pg.c3/=t1;
}
else
{
if(x2 > y2)
{
pg.c1=1;
pg.c2=0;
}
else
{
pg.c1=0;
pg.c2=1;
}
pg.c3=0;
}
}
else
{
pg.c1=1;
pg.c2=0;
pg.c3=0;
}
if(recent_time-show_pol_time > 0.1)
{
sc[SC_SHOW_POL]++;
show_pol_time=current_time();
}
// Set the flag and search again. This time using the correct optimum
// polarization only.
pol_known = 1;
goto search_again;
}
// Store different frequencies for each transform if signal drifts.
if(jmax == 0 || jmax == afc_speknum-1)
{
// Here it would perhaps be appropriate to add interpolation between
// two spectra, but poor fit for fast drifting signals is no problem,
// the user just specified inadequate drift range!
der=jmax;
fq=imax;
goto fillfq;
}
// Fit a parabola to the peak in the best spectrum and in the
// surrounding ones by use of 3 points.
nn=0;
for(j=jmax-1; j<=jmax+1; j++)
{
ja=imax-2;
jb=imax+2;
t1=0;
k=imax;
kk=fq_points*j;
for(i=ja; i<=jb; i++)
{
if(afc_spectrum[kk+i]>t1)
{
t1=afc_spectrum[kk+i];
k=i;
}
}
parabolic_fit(&l[nn],&pos[nn],afc_spectrum[kk+k-1],
afc_spectrum[kk+k],afc_spectrum[kk+k+1]);
pos[nn]+=k;
nn++;
}
parabolic_fit(&t1,&t2,ampl[0],ampl[1],ampl[2]);
der=jmax+t2;
// Interpolate peak position between spectra
if(t2 < 0)
{
fq=(t2+1)*pos[1]-t2*pos[0];
}
else
{
fq=(1-t2)*pos[1]+t2*pos[2];
}
fillfq:;
fq=(fq+first_fqp)/fftx_points_per_hz;
der=(afc_half_speknum-der)*afc_drift_step/fftx_points_per_hz;
// Now fill the frequencies in mix1_fq_mid[].
np=fftx_na;
fq+=der;
der/=2;
for(i=0; i<max_fftxn; i++)
{
if(fq < mix1_lowest_fq)fq = mix1_lowest_fq;
if(fq > mix1_highest_fq)fq = mix1_highest_fq;
mix1_fq_mid[mix1p0+np]=fq;
mix1_fq_curv[mix1p0+np]=0;
mix1_fq_slope[mix1p0+np]=der;
fq-=der;
if(fq < mix1_lowest_fq)fq = mix1_lowest_fq;
if(fq > mix1_highest_fq)fq = mix1_highest_fq;
mix1_fq_start[mix1p0+np]=fq;
fq-=der;
np=(np+fftxn_mask)&fftxn_mask;
}
}
int make_afc_signoi(void)
{
int i, k, np,j;
float t1,t2;
// Estimate S/N for the signal we see in mix1_eval data.
// Last time we used ag.fit_points blocks of eval data to evaluate
// the signal frequency. We estimate S/N over a similar time span.
// Each frequency in eval data is obtained from an average over
// afct_avgnum fft1 or fft2 blocks but this value may have changed so there
// may be a few points more or less.
np=(fftx_na-ag.fit_points-afct_avgnum+max_fftxn)&fftxn_mask;
t2=0;
k=0;
i=np;
while(i != fftx_na)
{
if(mix1_eval_sigpwr[mix1p0+i]>0)
{
k++;
t2+=mix1_eval_noise[mix1p0+i];
}
i=(i+1)&fftxn_mask;
}
if(k == 0)return 0;
t2/=k;
// Get the AFC noise floor as the average of noise levels that are
// within 3 times the average of the valid data points.
afc_noise=0;
k=0;
t2*=3;
i=np;
while(i != fftx_na)
{
if(mix1_eval_avgn[mix1p0+i]>0)
{
if(mix1_eval_noise[mix1p0+i]<t2)
{
k++;
afc_noise+=mix1_eval_noise[mix1p0+i];
}
}
i=(i+1)&fftxn_mask;
}
if(k == 0)return 0;
afc_noise/=k;
// Get the amplitude of the signal as the average of
// points with (S+N)/N above 3dB
t1=0;
t2=2*afc_noise;
k=0;
i=np;
while(i != fftx_na)
{
if(mix1_eval_avgn[mix1p0+i]>0)
{
if(mix1_eval_sigpwr[mix1p0+i]>t2)
{
k++;
t1+=mix1_eval_sigpwr[mix1p0+i];
}
}
i=(i+1)&fftxn_mask;
}
if(k == 0)return 0;
t1/=k;
// Get afc_ampl from the points that are above 25% of the average
// or have (S+N(avg))/N(avg) larger than 10dB.
// Exclude points with (S+N)/N < 3dB+3dB/sqrt(avgn)
// the same as: 0.5*(S+N) < (1+sqrt(avgn))*N
afc_maxval=0;
t2=10*afc_noise;
if(t2 > 0.25*t1)t2=0.25*t1;
k=0;
i=np;
j=0;
while(i != fftx_na)
{
j++;
if(mix1_eval_avgn[mix1p0+i]>0)
{
if(mix1_eval_sigpwr[mix1p0+i]>t2)
{
if(0.5*mix1_eval_sigpwr[mix1p0+i] >
(sqrt(mix1_eval_avgn[mix1p0+i])+1)*mix1_eval_noise[mix1p0+i])
{
k++;
afc_maxval+=mix1_eval_sigpwr[mix1p0+i];
}
}
}
i=(i+1)&fftxn_mask;
}
if(k == 0)return 0;
afc_maxval/=k;
return 1;
}
void make_ag_point(int np, int nno)
{
int i, j, k, m, na, pn1, pn2, nn_offset,psref;
int no_of_points;
float noise1[AG_MAKEP], noise2[AG_MAKEP],signal1[AG_MAKEP];
float t1, t2, t3;
float pos;
float *pwra;
float *x;
short int * zxy;
// Get the frequency of a signal that is very close to
// afc_fq
// Store old values extrapolated from old data (we might like to compare)
afc_fq=mix1_fq_mid[mix1p0+np];
if(afc_fq<0)
{
lirerr(889955);
return;
}
na=(np-afct_half_avgnum+max_fftxn)&fftxn_mask;
if(na > fftx_nm)return;
// Get spectrum and noise floor on both sides in no_of_points points.
if(ag.mode_control == 1)
{
no_of_points=nno;
}
else
{
no_of_points=ag.lock_range*fftx_points_per_hz;
}
no_of_points=(no_of_points&0xfffffffe)+1;
if(no_of_points < 9)no_of_points=9;
if(no_of_points > AG_MAKEP)no_of_points=AG_MAKEP;
if(baseband_bw_fftxpts > afcf_search_range)
{
t1=afcf_search_range;
}
else
{
t1=baseband_bw_fftxpts;
}
nn_offset=2.5*t1+no_of_points;
for(i=0; i<no_of_points; i++)
{
noise1[i]=0;
noise2[i]=0;
signal1[i]=0;
}
psref=mix1_fq_mid[mix1p0+np]*fftx_points_per_hz-no_of_points/2;
pn1=psref-nn_offset;
if(pn1 < 0)pn1=psref+2*nn_offset;
pn2=psref+nn_offset;
if(pn2 >= fftx_size)pn2=psref-2*nn_offset;
if(sw_onechan)
{
for(i=0; i<afct_avgnum; i++)
{
pwra=&fftx_pwr[na*fftx_size];
t1=afct_window[i];
for(j=0; j<no_of_points; j++)
{
signal1[j]+=pwra[psref+j]*t1;
noise1[j]+=pwra[pn1+j]*t1;
noise2[j]+=pwra[pn2+j]*t1;
}
na=(na+1)&fftxn_mask;
}
}
else
{
if(swmmx_fft2)
{
for(i=0; i<afct_avgnum; i++)
{
zxy=&fft2_short_int[4*na*fft2_size];
t3=afct_window[i];
for(j=0; j<no_of_points; j++)
{
m=4*(psref+j);
t1=pg.c1*zxy[m ]+pg.c2*zxy[m+2]+pg.c3*zxy[m+3];
t2=pg.c1*zxy[m+1]+pg.c2*zxy[m+3]-pg.c3*zxy[m+2];
signal1[j]+=t3*(t1*t1+t2*t2);
m=4*(pn1+j);
t1=pg.c1*zxy[m ]+pg.c2*zxy[m+2]+pg.c3*zxy[m+3];
t2=pg.c1*zxy[m+1]+pg.c2*zxy[m+3]-pg.c3*zxy[m+2];
noise1[j]+=t3*(t1*t1+t2*t2);
m=4*(pn2+j);
t1=pg.c1*zxy[m ]+pg.c2*zxy[m+2]+pg.c3*zxy[m+3];
t2=pg.c1*zxy[m+1]+pg.c2*zxy[m+3]-pg.c3*zxy[m+2];
noise2[j]+=t3*(t1*t1+t2*t2);
}
na=(na+1)&fftxn_mask;
}
}
else
{
for(i=0; i<afct_avgnum; i++)
{
x=&fftx[na*fftx_size*4];
t3=afct_window[i];
for(j=0; j<no_of_points; j++)
{
m=4*(psref+j);
t1=pg.c1*x[m ]+pg.c2*x[m+2]+pg.c3*x[m+3];
t2=pg.c1*x[m+1]+pg.c2*x[m+3]-pg.c3*x[m+2];
signal1[j]+=t3*(t1*t1+t2*t2);
m=4*(pn1+j);
t1=pg.c1*x[m ]+pg.c2*x[m+2]+pg.c3*x[m+3];
t2=pg.c1*x[m+1]+pg.c2*x[m+3]-pg.c3*x[m+2];
noise1[j]+=t3*(t1*t1+t2*t2);
m=4*(pn2+j);
t1=pg.c1*x[m ]+pg.c2*x[m+2]+pg.c3*x[m+3];
t2=pg.c1*x[m+1]+pg.c2*x[m+3]-pg.c3*x[m+2];
noise2[j]+=t3*(t1*t1+t2*t2);
}
na=(na+1)&fftxn_mask;
}
}
}
// Locate the maximum point.
t1=0;
k=-1;
for(j=0; j<no_of_points; j++)
{
if(t1 < signal1[j])
{
t1=signal1[j];
k=j;
}
}
// If maximum is one of the end points we failed!
// Set negative amplitude for error
if(k <= 0 || k==no_of_points-1)
{
mix1_eval_sigpwr[mix1p0+np]=-1;
}
else
{
parabolic_fit(&t1,&pos,sqrt(signal1[k-1]),sqrt(signal1[k]),sqrt(signal1[k+1]));
mix1_eval_sigpwr[mix1p0+np]=t1*t1;
mix1_eval_fq[mix1p0+np]=(psref+pos+k)/fftx_points_per_hz;
}
// Get the noise floor.
// There may be a signal1 outside the passband.
// Therefore we try to avoid exceptionally large values.
// First get the average on the two sides.
t1=0;
t2=0;
for(j=0; j<no_of_points; j++)
{
t1+=noise1[j];
t2+=noise2[j];
}
if(t1 > t2)
{
t1=t2;
}
t1/=no_of_points;
// Select 8 times the lowest average as a threshold and
// get the noise floor as the average of points below this threshold.
t2=0;
t1*=8;
k=0;
for(j=0; j<no_of_points; j++)
{
if(noise1[j] < t1)
{
t2+=noise1[j];
k++;
}
if(noise2[j] < t1)
{
t2+=noise2[j];
k++;
}
}
if(k > no_of_points/4)
{
mix1_eval_noise[mix1p0+np]=t2/k;
}
else
{
mix1_eval_noise[mix1p0+np]=t1/8;
}
// Signal and noise are averaged over afct_avgnum.
// In case the signal is zero we still get (S+N)/N larger than 1
// because of the statistical variation of the noise.
// Subtract the probable extra noise from mix1_eval_sigpwr
mix1_eval_sigpwr[mix1p0+np]-=mix1_eval_noise[mix1p0+np]*afc_noisefac;
if(mix1_eval_sigpwr[mix1p0+np] <=0 )
{
mix1_eval_sigpwr[mix1p0+np]=0;
mix1_eval_noise[mix1p0+np]=0.000000000001;
}
// Add a little signal to the noise to make sure S/N will not overflow.
mix1_eval_noise[mix1p0+np]+=0.000001*mix1_eval_sigpwr[mix1p0+np];
mix1_eval_avgn[mix1p0+np]=afct_avgnum;
}
void make_afct_window(int n)
{
char s[80];
int i, new_avgn;
float t1,t2,t3;
new_avgn=n;
if(new_avgn < 1)new_avgn=1;
if(new_avgn > 0.75*ag_xpixels)new_avgn=0.75*ag_xpixels;
if(new_avgn>max_afc_fit) new_avgn=max_afc_fit;