-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrate.c
1510 lines (1499 loc) · 48.2 KB
/
calibrate.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 "fft1def.h"
#include "fft2def.h"
#include "screendef.h"
#include "caldef.h"
#include "rusage.h"
#include "thrdef.h"
#include "keyboard_def.h"
#define TOPLINE 0
#define WAITMENU_LINE TOPLINE+4
#define ERRLINE (WAITMENU_LINE+5)
#define MAX_PULSE_ERRORS 5
#define SKIP_BEFORE 0.15
#define SKIP_AFTER 0.30
void final_filtercorr_init(void)
{
int i, j, k, mm, line;
int ja, jb;
int ka, kb;
int siz_n, siz;
float t1, t2, t3, renorm;
double dt1, dt2;
double *dbuf;
double pwrinteg[17];
char s[80];
double dc[4];
if(cal_fft1_size == 0)
{
show_missing_cal_info();
return;
}
mm=twice_rxchan;
// In case the native size of the calibration data is smaller
// than fft1_size, change fft1_size and remember old values in fft2_size.
fft2_size=fft1_size;
fft2_n=fft1_n;
if(cal_fft1_size < fft1_size)
{
fft1_size=cal_fft1_size;
fft1_n=cal_fft1_n;
init_fft1_filtercorr();
}
init_fft(0,fft1_n, fft1_size, fft2_tab, fft2_permute);
// The correction function fft1_filtercorr is present in the same
// number of points as we currently have for fft1.
// Since the Q of the hardware is limited there is no need to use
// too many points in the filter function.
// This routine will reduce the number of points used for the filtercorr
// function while removing erronous points and reducing noise.
// ******************************************************************
// First get the magnitude of the second derivative into cal_buf.
// Large values over narrow frequency regions indicate the presence
// of discontinuities in the calibration function due to narrowband
// signals (spurs)
// Also store the amplitude and the second derivative of the phase
// in cal_buf2.
// We can expect large second derivatives at the passband ends.
// Set ka and kb to point to the interesting region.
ka=5;
kb=fft1_size-6;
while(fft1_desired[ka]<0.5)ka++;
while(fft1_desired[kb]<0.5)kb--;
for(j=0; j<mm; j+=2)
{
// The calibration function consists of small numbers because
// it serves the purpose of giving a suitable output level
// from the first fft to interface to the integer format of
// MMX routines in case they might be used.
// Renormalize the calibration function to become near unity so we
// do not get underflows when computing powers.
t1=0;
for(i=ka; i<kb; i++)
{
t2=(float)(fabs(fft1_filtercorr[mm*i+j ])+
fabs(fft1_filtercorr[mm*i+j+1]));
if(t2 > t1)t1=t2;
}
for(i=0; i<fft1_size; i++)
{
fft1_filtercorr[mm*i+j ]/=t1;
fft1_filtercorr[mm*i+j+1]/=t1;
}
// First derivative into cal_tmp[0,1].
for(i=1; i<fft1_size; i++)
{
cal_tmp[mm*(i-1)+j ]=fft1_filtercorr[mm*i+j ]-
fft1_filtercorr[mm*(i-1)+j ];
cal_tmp[mm*(i-1)+j+1]=fft1_filtercorr[mm*i+j+1]-
fft1_filtercorr[mm*(i-1)+j+1];
}
cal_tmp[mm*(fft1_size-1)+j ]=0;
cal_tmp[mm*(fft1_size-1)+j+1]=0;
// Amplitude into cal_buf2[0] and phase into cal_buf2[1]
for(i=0; i<fft1_size; i++)
{
cal_buf2[mm*i+j ]=(float)sqrt(
fft1_filtercorr[mm*i+j ]*fft1_filtercorr[mm*i+j ]+
fft1_filtercorr[mm*i+j+1]*fft1_filtercorr[mm*i+j+1]);
if(cal_buf2[mm*i+j ] == 0)
{
cal_buf2[mm*i+j+1]=0;
}
else
{
cal_buf2[mm*i+j+1]=(float)atan2(fft1_filtercorr[mm*i+j+1],
fft1_filtercorr[mm*i+j ]);
}
}
}
for(j=0; j<mm; j+=2)
{
// Power of second derivative into cal_buf[0]
for(i=1; i<fft1_size; i++)
{
t1=cal_tmp[mm*i+j ]-cal_tmp[mm*(i-1)+j ];
t2=cal_tmp[mm*i+j+1]-cal_tmp[mm*(i-1)+j+1];
cal_buf[mm*(i-1)+j]=t1*t1+t2*t2;
}
cal_buf[mm*(fft1_size-1)+j ]=0;
// A carrier is about 2 bins wide, but after differentiation twice
// we might expect it to be 4 bins wide. Remove HF noise in the
// second derivative power to make signals stand out better by
// low pass filtering over 5 points.
t1=0;
for(i=0; i<5; i++)t1+=cal_buf[mm*i+j];
for(i=5; i<fft1_size; i++)
{
cal_buf[mm*(i-2)+j+1]=t1;
t1+=cal_buf[mm*i+j]-cal_buf[mm*(i-5)+j];
}
t3=0;
for(i=ka; i<kb; i++)
{
t3+=cal_buf[mm*i+j+1];
}
dc[j]=t3/(float)(kb-ka+1);
}
// make a single channel for the summed relative power of both channels.
for(i=1; i<fft1_size; i++)
{
t1=0;
for(j=0; j<mm; j+=2)
{
t1+=cal_buf[mm*i+j+1]/(float)dc[j];
}
cal_buf[i]=t1;
}
ja=ka;
repl:;
while(ja<kb && cal_buf[ja] < 100)ja++;
if(ja < kb)
{
jb=ja;
while(jb<kb && cal_buf[jb] >20)jb++;
if(jb<kb)
{
while(ja>ka && cal_buf[ja] > 20)ja--;
k=4+(jb-ja)/4;
while(ja>ka+k && cal_buf[ja]/cal_buf[ja-k]>4)ja--;
while(jb<kb-k && cal_buf[jb]/cal_buf[jb+k]>4)jb++;
// Make the amplitude constant across the bad region.
// The amplitude is set to the average amplitude from both sides.
for(j=0; j<mm; j+=2)
{
t1=0;
for(i=0; i<k; i++)
{
t1+=cal_buf2[mm*(ja-i)+j];
t1+=cal_buf2[mm*(jb+i)+j];
}
t1/=(float)(2*k);
for(i=ja; i<jb; i++)
{
cal_buf2[mm*i+j]=t1;
t2+=cal_buf2[mm*i+j];
}
}
ja=jb;
goto repl;
}
}
// Restore the calibration function.
for(j=0; j<mm; j+=2)
{
for(i=1; i<fft1_size; i++)
{
t1=cal_buf2[mm*i+j]*(float)cos(cal_buf2[mm*i+j+1]);
t2=cal_buf2[mm*i+j]*(float)sin(cal_buf2[mm*i+j+1]);
fft1_filtercorr[mm*i+j ]=t1;
fft1_filtercorr[mm*i+j+1]=t2;
}
}
// Get the transform of the current filtercorr function.
begin_final:;
cal_type=CAL_TYPE_REFINE_FILTERCORR;
cal_initscreen();
line=1;
dbuf=(double*)(cal_buf2);
k=mm*fft1_size;
t1=0;
for(i=0; i<k; i++)t1+=(float)fabs(fft1_filtercorr[i]);
renorm=(float)fft1_size/t1;
for(j=0; j<mm; j+=2)
{
for(i=0; i<fft1_size; i++)
{
cal_tmp[2*i ]=fft1_filtercorr[mm*i+j ]*renorm;
cal_tmp[2*i+1]=fft1_filtercorr[mm*i+j+1]*renorm;
}
fftforward(fft1_size, fft1_n, cal_tmp, fft2_tab, fft2_permute, FALSE);
for(i=0; i<fft1_size; i++)
{
cal_buf[mm*i+j ]=cal_tmp[2*i ]/(float)fft1_size;
cal_buf[mm*i+j+1]=cal_tmp[2*i+1]/(float)fft1_size;
}
}
// We now have the pulse response of the hardware filter chain.
// The pulse is centered at point 0.
// Calculate the integrated power of the pulse summed over the channels.
dt1=0;
for(i=0; i<fft1_size; i++)
{
for(j=0; j<mm; j++)
{
dt1+=pow(cal_buf[mm*i+j],2.);
}
dbuf[i]=dt1;
}
dt2=0.5*(dbuf[fft1_size/2-8]+dbuf[fft1_size/2+8]);
// Add an integration constant for the integral to become zero
// in the center region, as far away as possible from the pulse.
for(i=0; i<fft1_size; i++)
{
dbuf[i]-=dt2;
}
// Find out how fast the integral falls when the size is doubled.
i=2;
k=0;
lir_text(10,line,"Part of pulse energy lost outside range.");
line++;
dt1=dbuf[fft1_size-1]-dbuf[0];
while(i<=fft1_size/2)
{
if(i == fft1_size/2)
{
pwrinteg[k]=0;
}
else
{
pwrinteg[k]=fft1_size*(dbuf[fft1_size-i]-dbuf[i-1])/(dt1*(fft1_size-2*i));
}
sprintf(s,"Range=%d",2*i);
lir_text(12,line,s);
sprintf(s,"Lost=%.25f%%",pwrinteg[k]);
lir_text(25,line,s);
i*=2;
k++;
line++;
}
// Step siz_n until the remaining energy content of the integral
// is below 0.0001 (0.01% in power or 1% in voltage).
siz_n=1;
while(siz_n < k && pwrinteg[siz_n] > 0.0001)siz_n++;
if(siz_n == k)siz_n--;
if(k-siz_n < 3 && siz_n > 4)
{
settextcolor(14);
lir_text(5,line,"The calibration function seems noisy.");
line++;
lir_text(5,line,"It could also need all the points.");
line++;
lir_text(5,line,"Make your own judgement!");
line++;
settextcolor(7);
}
line+=2;
siz_n+=2;
siz=1<<siz_n;
sprintf(s,"Suggested new size for calibration function %d (old = %d)",
siz,fft1_size);
lir_text(5,line,s);
line+=2;
save_msg:;
sprintf(s,"Save modified calibration function in %d points? (Y/N,F1)",siz);
lir_text(5,line,s);
kbdinp:;
await_processed_keyboard();
if(kill_all_flag) return;
if(lir_inkey == 'N')
{
lir_text(5,line+3,"Enter size to save calibration. 0 to skip.");
siz=lir_get_integer(49,line+3,5,0,fft1_size);
if(siz == 0) return;
siz_n=make_power_of_two(&siz);
clear_lines(line,line+3);
goto save_msg;
}
if(lir_inkey == F1_KEY || lir_inkey == '!')
{
help_message(310);
goto begin_final;
}
if(lir_inkey != 'Y') goto kbdinp;
if(siz_n < 4)
{
lir_text(5,line+2,"Calibration data seems incorrect");
lir_text(5,line+3,"Nothing changed");
skip:;
lir_text(5,line+5,press_any_key);
await_keyboard();
return;
}
if( siz_n >= fft1_n)
{
lir_text(5,line+2,"Filter response needs current number of data points");
lir_text(5,line+3,"Data will be used without change");
goto skip;
}
resize_filtercorr_td_to_fd( FALSE, fft1_size, cal_buf, siz_n, siz, cal_buf);
write_filcorr(siz);
if(kill_all_flag) return;
sprintf(s,"Filter correction function saved in %d points",siz);
lir_text(5,line+2,s);
sprintf(s,"File size reduced from %d to %d bytes",
(1+twice_rxchan)*fft1_size*(int)sizeof(float)+20*(int)sizeof(int),
(1+twice_rxchan)*siz*(int)sizeof(float)+20*(int)sizeof(int));
lir_text(5,line+3,s);
lir_text(5,line+5,press_any_key);
await_keyboard();
// Restore filtercorr and fft1 desired.
if(fft2_size != fft1_size)
{
fft1_size=fft2_size;
fft1_n=fft2_n;
init_fft1_filtercorr();
}
}
void cal_filtercorr(void)
{
int j,jj,totbytes;
char s[160];
int i,k,m,n,width,old_pb;
int ia,ib,ic,ja,jb,max_pulpos;
int ka, kb;
float collect_noiselevel[MAX_ADCHAN/2];
float collect_powerlevel[MAX_ADCHAN/2];
float collect_powermax[MAX_ADCHAN/2];
float old_trig_power,trig_power,dc[2*MAX_ADCHAN],ampmax;
float avg_power,summed_power;
float t1,t2,t3,r1,r2;
int pulse_error[MAX_PULSE_ERRORS];
int siz128, mm, collected_pulses;
int pulpos;
float summed_timediff;
int no_of_timediff;
int mask;
int show_flag;
double redraw_time;
double sum[4],isum[4];
double dsum1, dsum2;
double dt1,dt2,dt3;
fft2_size=(int)(2*cal_interval*(1-SKIP_BEFORE-SKIP_AFTER));
fft2_n=make_power_of_two(&fft2_size);
init_fft(0,fft2_n, fft2_size, fft2_tab, fft2_permute);
cal_ygain=1;
cal_xgain=1;
cal_xshift=0;
cal_domain=0;
mm=twice_rxchan;
for(j=0;j<mm; j++)
{
sum[j]=0;
isum[j]=0;
}
siz128=fft2_size/128;
// **********************************************************************
// To get the pulses on one format independently of the hardware and
// fft implementation (some could contain filters) we use the standard
// routine for the first fft to get fourier transforms of the input data.
// By back transformation the input data is converted to floating point
// complex format regardless of the input format.
// **********************************************************************
i=0;
lir_sleep(10000);
fft1_px=fft1_pb;
while(i < 2 && thread_command_flag[THREAD_CAL_FILTERCORR]==THRFLAG_ACTIVE)
{
while(fft1_pb==fft1_px)
{
lir_await_event(EVENT_FFT1_READY);
}
fft1_px=fft1_pb;
i++;
}
clr_restart:;
summed_power=0;
summed_timediff=0;
no_of_timediff=0;
for(i=0; i<mm*fft1_size-1; i++)cal_buf5[i]=0;
for(j=0; j<ui.rx_rf_channels; j++)
{
collect_noiselevel[j]=0;
collect_powerlevel[j]=0;
}
totbytes=0;
collected_pulses=0;
old_trig_power=0;
for(i=0; i<MAX_PULSE_ERRORS; i++)pulse_error[i]=0;
redraw_time=current_time();
restart:;
cal_type=CAL_TYPE_COLLECT_PULSE_AVERAGE;
cal_initscreen();
lir_text(0,WAITMENU_LINE,
"Wait until curves are stable. +,-,E,C => Change scale");
lir_text(0,WAITMENU_LINE+1,
"U => Compute new corrections in RAM. Do not save on disk.");
lir_text(0,WAITMENU_LINE+2,"S => Save to disk from RAM");
lir_text(0,WAITMENU_LINE+3,"T => Toggle time/frequency domain");
lir_text(0,WAITMENU_LINE+4,"A => Clear RAM");
lir_refresh_screen();
thread_status_flag[THREAD_CAL_FILTERCORR]=THRFLAG_ACTIVE;
show_flag=TRUE;
// Wait a full second. Some hardware may not settle fast...
lir_sleep(1000000);
old_pb=fft1_pb;
while(thread_command_flag[THREAD_CAL_FILTERCORR] == THRFLAG_ACTIVE)
{
while(old_pb==fft1_pb)
{
lir_await_event(EVENT_FFT1_READY);
if(thread_command_flag[THREAD_CAL_FILTERCORR] != THRFLAG_ACTIVE)
goto check_command_flag;
fft1_px=(fft1_pb-fft1_block+fft1_mask+1)&fft1_mask;
}
// Skip old data in case the cpu is a bit slow.
while( ((fft1_pb-fft1_px+fft1_mask+1)&fft1_mask) > fft1_block)
{
fft1_px=(fft1_px+fft1_block)&fft1_mask;
}
old_pb=fft1_pb;
for(j=0; j<mm; j+=2)
{
for(i=0; i<fft1_size; i++)
{
if(fft1_px+mm*i+j > fft1_mask)
{
lirerr(879456);
goto filcorr_error_exit;
}
cal_tmp[2*i ]=fft1_float[fft1_px+mm*i+j ];
cal_tmp[2*i+1]=fft1_float[fft1_px+mm*i+j+1];
}
if( (ui.rx_input_mode&IQ_DATA) != 0)fft_iqshift(fft1_size, cal_tmp);
fftback(fft1_size, fft1_n, cal_tmp, cal_table, cal_permute,FALSE);
for(i=0; i<fft1_size; i++)
{
cal_buf[mm*i+j ]=cal_tmp[2*i ];
cal_buf[mm*i+j+1]=cal_tmp[2*i+1];
}
}
// The time function we got in cal_buf is the time function after
// multiplication with the sin pow4 window.
// Correct the time function by a division by cal_win - but use only the
// center half so we avoid division by small numbers.
for(j=0; j<mm; j+=2)
{
t1=0;
t2=0;
for(i=fft1_size/4; i<=fft1_size/2; i++)
{
cal_buf[i*mm+j ]/=cal_win[i];
t1+=cal_buf[i*mm+j ];
cal_buf[i*mm+j+1]/=cal_win[i];
t2+=cal_buf[i*mm+j+1];
}
for(i=fft1_size/2+1; i<=3*fft1_size/4; i++)
{
cal_buf[i*mm+j ]/=cal_win[fft1_size-1-i];
t1+=cal_buf[i*mm+j ];
cal_buf[i*mm+j+1]/=cal_win[fft1_size-1-i];
t2+=cal_buf[i*mm+j+1];
}
// Remove any DC component to make pulses more visible.
t1/=(float)fft1_size/2+1;
t2/=(float)fft1_size/2+1;
for(i=fft1_size/4; i<=3*fft1_size/4; i++)
{
cal_buf[i*mm+j ]-=t1;
cal_buf[i*mm+j+1]-=t2;
}
}
// Get the total power summed over all channels and store in cal_buf7
ampmax=0;
for(i=fft1_size/4; i<=3*fft1_size/4; i++)
{
cal_buf7[i]=(float)pow(cal_buf[i*mm ],2.0);
for(j=1; j<mm; j++)
{
cal_buf7[i]+=(float)pow(cal_buf[i*mm+j],2.0);
}
if(ampmax < cal_buf7[i])ampmax=cal_buf7[i];
}
if(old_trig_power == 0)
{
old_trig_power=ampmax;
}
else
{
old_trig_power=(old_trig_power*(INIT_PULSENUM-1)+ampmax)/INIT_PULSENUM;
}
// Make sure we have a reasonable value for trig power by not
// going further until we looked at a few pulses.
totbytes+=fft1_size/2;
if(totbytes > INIT_PULSENUM*cal_interval)
{
totbytes=(int)(INIT_PULSENUM*cal_interval);
// Set trig power at -15dB (30 times)
// Pulses must be separated well enough for the power level to
// have dropped well below -15dB after 70% of the interval time.
trig_power=old_trig_power/30;
// Look for a pulse, at least interval/2 points into our data.
pulpos=(int)(cal_interval+(float)fft1_size/4);
max_pulpos=(int)((float)(3*fft1_size)/4-cal_interval);
while( cal_buf7[pulpos] > trig_power/4 && pulpos<max_pulpos)pulpos++;
if(pulpos >= max_pulpos)
{
lir_text(1,TOPLINE+1,"Noise level too high");
lir_refresh_screen();
}
else
{
sprintf(s,"Search range %d to %d",pulpos,max_pulpos);
lir_text(1,TOPLINE+1,s);
lir_refresh_screen();
while( cal_buf7[pulpos] < trig_power && pulpos<max_pulpos)pulpos++;
find_pulse:;
if(ui.rx_rf_channels == 2)
{
sprintf(s,"Time difference between channels %7.4f samples.",
summed_timediff/no_of_timediff);
lir_text(38,TOPLINE+1,s);
}
lir_sched_yield();
if(thread_command_flag[THREAD_CAL_FILTERCORR]!=THRFLAG_ACTIVE)
{
goto check_command_flag;
}
if(pulpos >= max_pulpos)goto go_get_data;
dt1=current_time();
if(dt1 - redraw_time > 0.1)
{
redraw_time=dt1;
show_flag=TRUE;
sprintf(s,
"Accepted %4d Wide %4d Weak %4d S/N %4d Spur %4d Gain y=%.2f x=%.2f xpos %d",
pulse_error[0],pulse_error[1],pulse_error[2],
pulse_error[3],pulse_error[4],cal_ygain,1/cal_xgain,cal_xshift);
lir_text(0,TOPLINE+2,s);
lir_refresh_screen();
}
ia=pulpos-(int)(cal_interval/2);
ib=pulpos+(int)(cal_interval/2);
if(ib < max_pulpos)
{
ampmax=0;
for(i=ia; i<=ib; i++)
{
if(cal_buf7[i] > 1.1*ampmax)
{
ampmax=cal_buf7[i];
pulpos=i;
}
}
// Copy data into cal_buf2 so the pulse becomes placed at location 0
// Also copy the power function to cal_fft1_sumsq with the pulse at location 0
k=pulpos;
for(i=0; i<fft2_size/2; i++)
{
for(j=0; j<mm; j++)
{
cal_buf2[i*mm+j]=cal_buf[k*mm+j];
}
cal_fft1_sumsq[i]=cal_buf7[k];
k++;
}
k=pulpos-1;
for(i=fft1_size-1; i >= fft1_size-fft2_size/2; i--)
{
for(j=0; j<mm; j++)cal_buf2[i*mm+j]=cal_buf[k*mm+j];
cal_fft1_sumsq[i]=cal_buf7[k];
k--;
}
// Find the width of the current pulse.
// First find the maximum power.
k=pulpos;
pulpos+=(int)cal_interval;
if( ampmax < old_trig_power/20)
{
pulse_error[2]++;
goto find_pulse;
}
// Now step from the peak position until we reach power/8
ampmax/=8;
ja=0;
jb=fft1_size-1;
while( cal_fft1_sumsq[ja] > ampmax && ja<fft1_size)ja++;
while( cal_fft1_sumsq[jb] > ampmax && jb>ja)jb--;
width=ja+fft1_size-jb;
if(width > cal_interval/4)
{
pulse_error[1]++;
goto find_pulse;
}
// Some soundcards like Maya44 have a time shift between the channels.
// compute the center of gravity of the pulse power in the two channels
// and accumulate the difference. We will use it to shift the pulse
// positions by changing the first derivative of the phase.
if(ui.rx_rf_channels == 2)
{
t1=0;
t2=0;
r1=0;
r2=0;
for(i=-width; i<width; i++)
{
t1+=cal_buf[(k+i)*mm ]*cal_buf[(k+i)*mm ]+
cal_buf[(k+i)*mm+1]*cal_buf[(k+i)*mm+1];
t2+=i*(cal_buf[(k+i)*mm ]*cal_buf[(k+i)*mm ]+
cal_buf[(k+i)*mm+1]*cal_buf[(k+i)*mm+1]);
r1+=cal_buf[(k+i)*mm+2]*cal_buf[(k+i)*mm+2]+
cal_buf[(k+i)*mm+3]*cal_buf[(k+i)*mm+3];
r2+=i*(cal_buf[(k+i)*mm+2]*cal_buf[(k+i)*mm+2]+
cal_buf[(k+i)*mm+3]*cal_buf[(k+i)*mm+3]);
}
summed_timediff+=r2/r1-t2/t1;
no_of_timediff++;
if(no_of_timediff < 100)goto find_pulse;
}
// Filter the power function to reduce noise. Place in cal_fft1_slowsum.
width/=2;
if(width < (int)(cal_interval/64))width=(int)(cal_interval/64);
if(width < 4)width=4;
ja=fft1_size-width;
jb=width;
width=2*width+1;
dsum1=0;
for(i=0; i<=jb; i++)dsum1+=cal_fft1_sumsq[i];
for(i=ja; i<fft1_size; i++)dsum1+=cal_fft1_sumsq[i];
for(i=0; i<fft1_size; i++)
{
jb=(jb+1)&(fft1_size-1);
if(jb >= fft1_size)jb-=fft1_size;
cal_fft1_slowsum[i]=(float)dsum1/(float)width;
dsum1+=cal_fft1_sumsq[jb]-cal_fft1_sumsq[ja];
ja=(ja+1)&(fft1_size-1);
}
// Locate the surrounding pulses.
ja=(int)(0.5F*cal_interval);
jb=(int)(1.5F*cal_interval);
t1=0;
for(i=ja; i<jb; i++)
{
if(cal_fft1_slowsum[i] > t1)
{
t1=cal_fft1_slowsum[i];
ib=i;
}
}
ja=fft1_size-(int)(1.5F*cal_interval);
jb=fft1_size-(int)(0.5F*cal_interval);
t1=0;
ic=ja;
for(i=ja; i<jb; i++)
{
if(cal_fft1_slowsum[i] > t1)
{
t1=cal_fft1_slowsum[i];
ic=i;
}
}
// ib and ic point to the peaks of surrounding pulses.
// skip a range after the previous pulse and before the next one.
ib=(int)((1-SKIP_BEFORE)*(float)ib);
ic=fft1_size-(int)((float)(fft1_size-ic)*(1.0F-SKIP_AFTER));
// When we pick the range ic to ib we may create a discontinuity.
// because there may be a DC level.
// Compute the DC level around ib and ic and add a straight line
// that will make the average DC level zero at both ends.
mask=fft1_size-1;
jj=2+(int)0.05*cal_interval;
for(j=0;j<mm; j++)
{
sum[j]=0;
isum[j]=0;
}
for(i=0; i<jj; i++)
{
for(j=0;j<mm; j++)
{
sum[j]+=cal_buf2[(ib+i+1)*mm+j]+cal_buf2[(ib-i)*mm+j];
isum[j]+=cal_buf2[(ic+i)*mm+j]+cal_buf2[(ic-i-1)*mm+j];
}
}
for(j=0;j<mm; j++)
{
sum[j]/=2*jj;
isum[j]/=2*jj;
sum[j]=(sum[j]-isum[j])/( (ib-ic-1)&mask );
}
ja=ic;
while(ja != ib)
{
for(j=0; j<mm; j++)
{
cal_buf2[ja*mm+j]-=(float)isum[j];
isum[j]+=sum[j];
}
ja=(ja+1)&mask;
}
// Attenuate before and after the pulse with a cos squared window.
k=fft1_size-ic;
if(ib < k)k=ib;
k/=2;
ja=ic+k;
jb=ib-k;
t1=0;
for(i=0; i<k; i++)
{
t2=(float)pow(cos(t1),2.0);
for(j=0; j<mm; j++)
{
cal_buf2[ja*mm+j]*=t2;
cal_buf2[jb*mm+j]*=t2;
}
t1+=(float)PI_L/(2*(float)k);
ja--;
jb++;
}
ia=ja;
ib=jb;
for(i=jb; i<=ja; i++)
{
for(j=0; j<mm; j++)
{
cal_buf2[i*mm+j]=0;
}
}
if(show_flag)
{
if(cal_domain == 1)
{
for(j=0; j<mm; j+=2)
{
for(i=0; i<screen_width; i++)
{
k=i-screen_width/3;
k=(int)((float)k*cal_xgain);
k+=cal_xshift;
k&=(fft1_size-1);
lir_setpixel(i, cal_graph[screen_width*j+i], 0);
t2=0.00001F*cal_ygain*cal_buf2[mm*k+j];
if(t2 <-cal_ymax)t2=-cal_ymax;
if(t2 >cal_ymax)t2=cal_ymax;
if(j > 1)t2-=0.32F;
cal_graph[screen_width*j+i]=
(short int)((float)screen_height*(cal_yzer-t2));
lir_setpixel(i, cal_graph[screen_width*j+i], 13);
lir_setpixel(i, cal_graph[screen_width*(j+1)+i], 0);
t2=0.00001F*cal_ygain*cal_buf2[mm*k+j+1];
if(t2 <-cal_ymax)t2=-cal_ymax;
if(t2 >cal_ymax)t2=cal_ymax;
if(j > 1)t2-=0.32F;
cal_graph[screen_width*(j+1)+i]=
(short int)((float)screen_height*(cal_yzer-t2));
lir_setpixel(i, cal_graph[screen_width*(j+1)+i], 10);
}
}
lir_refresh_screen();
show_flag=FALSE;
}
}
for(j=0; j<ui.rx_rf_channels; j++)collect_powermax[j]=0;
// Now that we have a single pulse (hopefully), get the
// fourier transform of it.
for(j=0; j<mm; j+=2)
{
jj=j/2;
for(i=0; i<ib; i++)
{
cal_tmp[2*i ]=cal_buf2[mm*i+j ];
cal_tmp[2*i+1]=cal_buf2[mm*i+j+1];
}
k=fft2_size-1;
for(i=fft1_size-1; i>ia; i--)
{
cal_tmp[2*k ]=cal_buf2[mm*i+j ];
cal_tmp[2*k+1]=cal_buf2[mm*i+j+1];
k--;
}
while(k >= ib)
{
cal_tmp[2*k ]=0;
cal_tmp[2*k+1]=0;
k--;
}
fftforward(fft2_size, fft2_n, cal_tmp, fft2_tab, fft2_permute,fft2_n > 12);
if( (ui.rx_input_mode&IQ_DATA) != 0)fft_iqshift(fft2_size, cal_tmp);
for(i=0; i<fft2_size; i++)
{
cal_buf2[mm*i+j ]=cal_tmp[2*i ];
cal_buf2[mm*i+j+1]=cal_tmp[2*i+1];
}
// The complex transform must not contain high frequencies because the
// filter function must vary slowly with frequency.
// Store a low pass filtered version in cal_buf3 as complex numbers
// with the corresponding filtered powers in cal_buf6
dsum1=dsum2=0;
n=2*siz128;
for(i=2; i<n+2; i++)
{
dsum1+=cal_tmp[2*i];
dsum2+=cal_tmp[2*i+1];
}
for(i=0; i<siz128+2; i++)
{
cal_buf3[mm*i+j ]=(float)(dsum1/n);
cal_buf3[mm*i+j+1]=(float)(dsum2/n);
cal_buf6[mm*i+j ]=(float)(pow(dsum1/n,2.0)+pow(dsum2/n,2.0));
}
ja=fft2_size-siz128-1;
if( (ui.rx_input_mode&IQ_DATA) == 0)
{
for(i=siz128+2; i<ja; i++)
{
cal_buf3[mm*i+j ]=(float)(dsum1/n);
cal_buf3[mm*i+j+1]=(float)(dsum2/n);
cal_buf6[mm*i+j ]=(float)(pow(dsum1/n,2.0)+pow(dsum2/n,2.0));
if(cal_buf6[mm*i+j ]>collect_powermax[jj])
{
collect_powermax[jj]=cal_buf6[mm*i+j ];
}
dsum1+=cal_tmp[2*(i+siz128) ]-cal_tmp[2*(i-siz128) ];
dsum2+=cal_tmp[2*(i+siz128)+1]-cal_tmp[2*(i-siz128)+1];
}
}
// ******************************************************
// In direct conversion mode there may be a discontinuity at fft2_size/2
// because the A/D converters are AC coupled.
else
{
jb=fft2_size/2-siz128-1;
for(i=siz128+2; i<jb; i++)
{
cal_buf3[mm*i+j ]=(float)(dsum1/n);
cal_buf3[mm*i+j+1]=(float)(dsum2/n);
cal_buf6[mm*i+j ]=(float)(pow(dsum1/n,2.0)+pow(dsum2/n,2.0));
if(cal_buf6[mm*i+j ]>collect_powermax[jj])
{
collect_powermax[jj]=cal_buf6[mm*i+j ];
}
dsum1+=cal_tmp[2*(i+siz128)]-cal_tmp[2*(i-siz128)];
dsum2+=cal_tmp[2*(i+siz128)+1]-cal_tmp[2*(i-siz128)+1];
}
for(i=jb; i<fft2_size/2; i++)
{
cal_buf3[mm*i+j ]=(float)(dsum1/n);
cal_buf3[mm*i+j+1]=(float)(dsum2/n);
cal_buf6[mm*i+j ]=(float)(pow(dsum1/n,2.0)+pow(dsum2/n,2.0));
}
dsum1=dsum2=0;
jb=fft2_size/2+2*siz128+3;
for(i=fft2_size/2+3; i<jb; i++)
{
dsum1+=cal_tmp[2*i];
dsum2+=cal_tmp[2*i+1];
}
jb=fft2_size/2+siz128+1;
for(i=fft2_size/2+1; i<jb; i++)
{
cal_buf3[mm*i+j ]=(float)(dsum1/n);
cal_buf3[mm*i+j+1]=(float)(dsum2/n);
cal_buf6[mm*i+j ]=(float)(pow(dsum1/n,2.0)+pow(dsum2/n,2.0));
}
for(i=jb; i<ja; i++)
{
cal_buf3[mm*i+j ]=(float)(dsum1/n);
cal_buf3[mm*i+j+1]=(float)(dsum2/n);
cal_buf6[mm*i+j ]=(float)(pow(dsum1/n,2.0)+pow(dsum2/n,2.0));
if(cal_buf6[mm*i+j ]>collect_powermax[jj])
collect_powermax[jj]=cal_buf6[mm*i+j ];
dsum1+=cal_tmp[2*(i+siz128)]-cal_tmp[2*(i-siz128)];
dsum2+=cal_tmp[2*(i+siz128)+1]-cal_tmp[2*(i-siz128)+1];
}
cal_buf3[ui.rx_rf_channels*fft2_size+j ]=
(cal_buf3[ui.rx_rf_channels*(fft2_size+2)+j ]+
cal_buf3[ui.rx_rf_channels*(fft2_size-2)+j ])/2;
cal_buf3[ui.rx_rf_channels*fft2_size+j+1]=
(cal_buf3[ui.rx_rf_channels*(fft2_size+2)+j+1]+
cal_buf3[ui.rx_rf_channels*(fft2_size-2)+j+1])/2;
cal_buf6[ui.rx_rf_channels*fft2_size+j]=
(cal_buf6[ui.rx_rf_channels*(fft2_size+2)+j]+
cal_buf6[ui.rx_rf_channels*(fft2_size-2)+j])/2;
}
for(i=ja; i<fft2_size; i++)
{
cal_buf3[mm*i+j ]=(float)(dsum1/n);
cal_buf3[mm*i+j+1]=(float)(dsum2/n);
cal_buf6[mm*i+j ]=(float)(pow(dsum1/n,2.0)+pow(dsum2/n,2.0));
}
}
// ******************************************************
//Valid spectra:
//cal_buf5 = accumulated power and d2phase/df2 (not normalized)
//cal_buf4 = accumulated power and phase (normalised)
//cal_buf2 = current pulse: complex amplitude
//cal_buf3 = current pulse: smoothed complex amplitude
//cal_buf6 = current pulse: smoothed power
// ******************************************************
// Collect noise weighted by power spectrum
// Also store the noise in cal_buf6
for(j=0; j<mm; j+=2)
{
dc[j]=0;
dc[j+1]=0;
}
if( (ui.rx_input_mode&IQ_DATA) == 0)
{
ib=fft2_size-siz128;
}
else
{
ib=fft2_size/2-siz128;
}
for(i=siz128; i<ib; i++)
{
for(j=0; j<mm; j+=2)
{
t1=cal_buf6[mm*i+j ];
t2=(float)(pow(cal_buf2[mm*i+j ]-cal_buf3[mm*i+j ],2.0)+
pow(cal_buf2[mm*i+j+1]-cal_buf3[mm*i+j+1],2.0));
cal_buf6[mm*i+j+1]=t2;
dc[j]+=t1*t2;
dc[j+1]+=t1;
}
}
if( (ui.rx_input_mode&IQ_DATA) != 0)
{
ib=fft2_size-siz128;
for(i=fft2_size/2+siz128; i<ib; i++)
{
for(j=0; j<mm; j+=2)
{
t1=cal_buf6[mm*i+j ];
t2=(float)(pow(cal_buf2[mm*i+j ]-cal_buf3[mm*i+j ],2.0)+
pow(cal_buf2[mm*i+j+1]-cal_buf3[mm*i+j+1],2.0));
cal_buf6[mm*i+j+1]=t2;
dc[j]+=t1*t2;
dc[j+1]+=t1;
}
}
}
for(i=0; i<siz128; i++)
{
for(j=0; j<mm; j+=2)
{
cal_buf6[mm*i+j+1]=(float)
(pow(cal_buf2[mm*i+j ]-cal_buf3[mm*i+j ],2.0)+