-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseb_graph.c
3217 lines (3083 loc) · 106 KB
/
baseb_graph.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.
#define YWF 4
#define YBO 4
#define DA_GAIN_RANGE 10.
#define DA_GAIN_REF 1.
#define BG_MINYCHAR 8
#define BG_MIN_WIDTH (36*text_width)
#define BG_BFO 1
#define BG_BFO10 2
#define BG_BFO100 3
#define BG_FLAT 4
#define BG_CURV 5
#define BG_VOLUME 6
#include <string.h>
#include "globdef.h"
#include "uidef.h"
#include "fft3def.h"
#include "fft2def.h"
#include "fft1def.h"
#include "sigdef.h"
#include "seldef.h"
#include "vernr.h"
#include "screendef.h"
#include "graphcal.h"
#include "thrdef.h"
#include "options.h"
#if OSNUM == OSNUM_WINDOWS
#include "wscreen.h"
#endif
#if OSNUM == OSNUM_LINUX
#include "lscreen.h"
#endif
int binshape_points;
int binshape_total;
int reinit_baseb;
int baseband_graph_scro;
int bg_old_x1;
int bg_old_x2;
int bg_old_y1;
int bg_old_y2;
int new_bg_agc_flag;
int new_bg_fm_mode;
int new_bg_fm_subtract;
int new_bg_ch2_phase;
int new_daout_channels;
int new_daout_bytes;
int new_bg_delay;
int new_bg_coherent;
int new_bg_twopol;
float make_interleave_ratio(int nn);
void prepare_mixer(MIXER_VARIABLES *m, int nn);
static int ch2_signs[3]= {1,-1,0};
float iir5_c4(float fq)
{
double f, dt1;
f=fq;
f+=(double)1.09997;
dt1=(double)-8180.447687211407;
dt1+=(double)7606.001654061646/f;
dt1+=(double)4911.238063416248*f;
dt1+=(double)-3722.590923768796/(f*f);
dt1+=(double)-1563.849845278449*f*f;
dt1+=(double)752.161694907701/(f*f*f);
dt1+=(double)204.513728752513*f*f*f;
return (float)dt1;
}
float iir5_c3(float fq)
{
double f, dt1;
f=fq;
f+=(double).486;
dt1=(double)1216.739279441632;
dt1+=(double)-498.037837421718/f;
dt1+=(double)-1605.884219112067*f;
dt1+=(double)98.522399169193/(f*f);
dt1+=(double)1163.334654719262*f*f;
dt1+=(double)-8.037391363636/(f*f*f);
dt1+=(double)-378.207481959566*f*f*f;
dt1+=(double)0.460060792302*f*f*f*f;
return (float)dt1;
}
float iir5_c2(float fq)
{
double f, dt1;
f=fq;
f+=(double).373;
dt1=(double)-1367.817993435964;
dt1+=(double)321.426710716263/f;
dt1+=(double)3143.873042678646*f;
dt1+=(double)-33.921164217429/(f*f);
dt1+=(double)-4207.054014954479*f*f;
dt1+=(double)1.474943544810/(f*f*f);
dt1+=(double)3173.210883130213*f*f*f;
dt1+=(double)-1061.231815380881*f*f*f*f;
return (float)dt1;
}
float iir5_c1(float fq)
{
double f, dt1;
f=fq;
f+=(double).13607;
dt1=(double)44.536544420484;
dt1+=(double)-6.546900527778/f;
dt1+=(double)-135.019183000212*f;
dt1+=(double)0.298668662095/(f*f);
dt1+=(double)185.556866049127*f*f;
dt1+=(double)-0.006124743273/(f*f*f);
dt1+=(double)-66.083966540342*f*f*f;
dt1+=(double)-56.694842234911*f*f*f*f;
return (float)dt1;
}
float iir5_c0(float fq)
{
double f, dt1;
f=fq;
f=f+(double)0.25;
f=pow(f,(double)1.02);
dt1= (double)36.927768658067;
dt1+=(double)-70.589472628117*f;
dt1+=(double)72.191321694731*f*f;
dt1+=(double)-31.339811219801*f*f*f;
dt1+=(double)-10.505632906345/f;
dt1+=(double)1.399038105205/(f*f);
dt1+=(double)-0.043679124720/(f*f*f);
return (float)dt1;
}
float iir5_gain(float fq)
{
double f, dt1;
// fq is the corner frequency divided by the sampling frequency
f=fq;
dt1=(double)0.147800765172*pow(f,-3);
dt1+=(double)0.03169658430080*pow(f,-4);
dt1+=(double)0.0032849456565132*pow(f,-5);
dt1+=(double)-0.1237864623*pow(f,-2);
return (float)dt1;
}
void make_iir5(float fq, IIR5_PARMS *iir5)
{
iir5[0].gain=iir5_gain(fq);
iir5[0].c0=iir5_c0(fq);
iir5[0].c1=iir5_c1(fq);
iir5[0].c2=iir5_c2(fq);
iir5[0].c3=iir5_c3(fq);
iir5[0].c4=iir5_c4(fq);
}
void halt_rx_output(void)
{
int i;
i=0;
await_semclear:
;
if(all_threads_started &&
thread_status_flag[THREAD_RX_OUTPUT]!=THRFLAG_SEMCLEAR) {
if(new_baseb_flag != 0) {
i++;
lir_sleep(20000);
if(i<200)goto await_semclear;
lirerr(564292);
return;
}
// The output is running. Stop it now!
pause_thread(THREAD_RX_OUTPUT);
thread_command_flag[THREAD_RX_OUTPUT]=THRFLAG_SEMCLEAR;
lir_sched_yield();
while(thread_status_flag[THREAD_RX_OUTPUT] != THRFLAG_SEMCLEAR) {
lir_sleep(1000);
}
baseb_reset_counter++;
}
}
void clear_baseb_arrays(int nn,int k)
{
memset(&baseb_raw[2*nn],0,2*k*sizeof(float));
memset(&baseb_raw_orthog[2*nn],0,2*k*sizeof(float));
memset(&baseb[2*nn],0,2*k*sizeof(float));
memset(&baseb_carrier[2*nn],0,2*k*sizeof(float));
memset(&baseb_carrier_ampl[nn],0,k*sizeof(float));
memset(&baseb_totpwr[nn],0,k*sizeof(float));
if(bg.agc_flag != 0) {
memset(&baseb_agc_level[nn],0,k*sizeof(float));
memset(&baseb_agc_det[nn],0,k*sizeof(float));
memset(&baseb_threshold[bg.agc_flag*nn],0,bg.agc_flag*k*sizeof(float));
memset(&baseb_upthreshold[bg.agc_flag*nn],0,bg.agc_flag*k*sizeof(float));
}
lir_sched_yield();
if(genparm[CW_DECODE_ENABLE] != 0) {
if(bg.agc_flag == 0) {
memset(&baseb_threshold[nn],0,k*sizeof(float));
memset(&baseb_upthreshold[nn],0,k*sizeof(float));
}
memset(&baseb_ramp[nn],-cg_code_unit,k*sizeof(short int));
memset(&baseb_wb_raw[2*nn],0,2*k*sizeof(float));
memset(&baseb_fit[2*nn],0,2*k*sizeof(float));
memset(&baseb_tmp[2*nn],0,2*k*sizeof(float));
memset(&baseb_sho1[2*nn],0,2*k*sizeof(float));
memset(&baseb_sho2[2*nn],0,2*k*sizeof(float));
memset(&baseb_envelope[2*nn],0,2*k*sizeof(float));
}
lir_sched_yield();
}
int filcur_pixel(int points)
{
float t1;
t1=(float)(2*points)/fft3_size;
t1=sqrt(t1);
return bg_first_xpixel+t1*bg_xpixels;
//return bg_first_xpixel+(bg_xpixels*2*points)/fft3_size;
}
int filcur_points(void)
{
float t1;
t1=(float)(mouse_x-bg_first_xpixel+1)/bg_xpixels;
t1=t1*t1;
return 0.5*fft3_size*t1;
}
void make_bg_waterf_cfac(void)
{
bg_waterf_cfac=bg.waterfall_gain*0.1;
bg_waterf_czer=10*bg.waterfall_zero;
}
void make_bg_yfac(void)
{
if(genparm[SECOND_FFT_ENABLE]==0) {
bg.yfac_power=FFT1_BASEBAND_FACTOR;
bg.yfac_power*=ui.rx_rf_channels*ui.rx_rf_channels;
} else {
bg.yfac_power=FFT2_BASEBAND_FACTOR*
(float)(1<<genparm[FIRST_BCKFFT_ATT_N])/fft2_size;
bg.yfac_power*=bg.yfac_power;
bg.yfac_power*=(float)(1<<genparm[MIX1_BANDWIDTH_REDUCTION_N]);
}
bg.yfac_power*=(1 + 1/(0.5+genparm[FIRST_FFT_SINPOW]))/(float)(fft1_size);
bg.yfac_power/=fft3_size;
baseband_pwrfac=10*bg.yfac_power/fft3_size;
bg_waterf_yfac=20*baseband_pwrfac/bg.waterfall_avgnum;
bg.yfac_power/=bg.yzero*bg.yzero;
bg.yfac_log=10/bg.db_per_pixel;
}
void make_daout_gain(void)
{
float t1;
if(daout_gain_y>bg_y0) {
daout_gain_y=bg_y0;
daout_gain=0;
return;
}
if(daout_gain_y<bg_ymax)daout_gain_y=bg_ymax;
t1=(float)((bg_y0+bg_ymax)/2-daout_gain_y)/(bg_y0-bg_ymax);
bg.output_gain=pow(10.,t1*DA_GAIN_RANGE)/DA_GAIN_REF;
if(rx_mode == MODE_FM && bg_coherent < 2) {
daout_gain=5*bg.output_gain;
} else {
daout_gain=0.5*bg.output_gain/fft3_size;
if(genparm[SECOND_FFT_ENABLE]!=0) {
daout_gain/=fft2_size;
daout_gain*=1<<genparm[FIRST_BCKFFT_ATT_N];
}
daout_gain*=sqrt((1 + 1/(0.5+genparm[FIRST_FFT_SINPOW]))/
(float)(fft1_size));
}
if(rx_daout_bytes == 2) {
daout_gain*=256;
}
}
void make_daout_gainy(void)
{
int old;
float t1;
old=daout_gain_y;
t1=log10(bg.output_gain*DA_GAIN_REF)/DA_GAIN_RANGE;
daout_gain_y=(bg_y0+bg_ymax)/2-t1*(bg_y0-bg_ymax);
make_daout_gain();
update_bar(bg_vol_x1,bg_vol_x2,bg_y0,daout_gain_y,old,
BG_GAIN_COLOR,bg_volbuf);
}
void make_new_daout_upsamp(void)
{
int k, new;
float t1;
t1=da_resample_ratio;
new=1;
daout_upsamp_n=0;
{
k=genparm[DA_OUTPUT_SPEED];
while(t1 > 5 && k > 16000 && new < 32) {
daout_upsamp_n++;
k/=2;
t1/=2;
new*=2;
}
}
if(new != daout_upsamp) {
halt_rx_output();
daout_upsamp=new;
}
}
void make_bfo(void)
{
float bforef, bfooff, t1;
if(rx_mode == MODE_SSB)sc[SC_FREQ_READOUT]++;
if(use_bfo == 0) {
rx_daout_cos=1;
rx_daout_sin=0;
rx_daout_phstep_sin=0;
rx_daout_phstep_cos=1;
bfo_xpixel=-1;
bfo10_xpixel=-1;
bfo100_xpixel=-1;
return;
}
if(bfo_xpixel > 0) {
if(bg_filterfunc_y[bfo_xpixel] > 0)
lir_setpixel(bfo_xpixel,bg_filterfunc_y[bfo_xpixel],14);
if(bg_filterfunc_y[bfo10_xpixel] > 0)
lir_setpixel(bfo10_xpixel,bg_filterfunc_y[bfo10_xpixel],14);
if(bg_filterfunc_y[bfo100_xpixel] > 0)
lir_setpixel(bfo100_xpixel,bg_filterfunc_y[bfo100_xpixel],14);
if(bg_carrfilter_y[bfo_xpixel] > 0)
lir_setpixel(bfo_xpixel,bg_carrfilter_y[bfo_xpixel],14);
if(bg_carrfilter_y[bfo10_xpixel] > 0)
lir_setpixel(bfo10_xpixel,bg_carrfilter_y[bfo10_xpixel],14);
if(bg_carrfilter_y[bfo100_xpixel] > 0)
lir_setpixel(bfo100_xpixel,bg_carrfilter_y[bfo100_xpixel],14);
}
// When we arrive here only bg.bfo_freq is defined.
// Set up the other variables we need that depend on it.
make_new_daout_upsamp();
t1=(2*PI_L*bg.bfo_freq)/genparm[DA_OUTPUT_SPEED];
rx_daout_phstep_cos=cos(t1);
rx_daout_phstep_sin=sin(t1);
bforef=bg_first_xpixel+0.5+bg.pixels_per_point*(fft3_size/2-bg_first_xpoint);
bfooff=bg.pixels_per_point*bg.bfo_freq/bg_hz_per_pixel;
bfo_xpixel=bforef+bfooff;
if(bfo_xpixel < bg_first_xpixel)bfo_xpixel=bg_first_xpixel;
if(bfo_xpixel > bg_last_xpixel)bfo_xpixel=bg_last_xpixel;
bfo10_xpixel=bforef+0.1*bfooff;
if(bfo10_xpixel < bg_first_xpixel)bfo10_xpixel=bg_first_xpixel;
if(bfo10_xpixel > bg_last_xpixel)bfo10_xpixel=bg_last_xpixel;
bfo100_xpixel=bforef+0.01*bfooff;
if(bfo100_xpixel < bg_first_xpixel)bfo100_xpixel=bg_first_xpixel;
if(bfo100_xpixel > bg_last_xpixel)bfo100_xpixel=bg_last_xpixel;
lir_line(bfo100_xpixel, bg_y0,bfo100_xpixel,bg_y1,12);
if(kill_all_flag) return;
lir_line(bfo10_xpixel, bg_y1-1,bfo10_xpixel,bg_y2,12);
if(kill_all_flag) return;
lir_line(bfo_xpixel, bg_y2-1,bfo_xpixel,bg_y3,12);
}
void chk_bg_avgnum(void)
{
if(fft3_blocktime*bg.fft_avgnum > genparm[BASEBAND_STORAGE_TIME])
bg.fft_avgnum=genparm[BASEBAND_STORAGE_TIME]/fft3_blocktime;
if(bg.fft_avgnum >9999)bg.fft_avgnum=9999;
if(bg.fft_avgnum <1)bg.fft_avgnum=1;
}
void clear_agc(void)
{
// The AGC attack is operated from two series connected low pass filters.
// followed by a peak detector.
// The AGC is in mix2.c
rx_agc_factor1=1;
rx_agc_factor2=1;
rx_agc_sumpow1=0;
rx_agc_sumpow2=0;
rx_agc_sumpow3=0;
rx_agc_sumpow4=0;
rx_agc_fastsum1=0;
rx_agc_fastsum2=0;
rx_agc_fast_factor1=pow(0.5,2000./baseband_sampling_speed);
rx_agc_fast_factor2=1-rx_agc_fast_factor1;
agc_attack_factor1=pow(0.5,1000./(baseband_sampling_speed*(1<<bg.agc_attack)));
agc_release_factor=pow(0.5,500./(baseband_sampling_speed*(1<<bg.agc_release)));
agc_attack_factor2=1-agc_attack_factor1;
bg_agc_hang_pts=0.01*baseband_sampling_speed*pow(1.7,(float)(bg.agc_hang));
if(bg_agc_hang_pts > baseband_size/2)bg_agc_hang_pts=baseband_size/2;
}
/*void construct_fir(int win, int siz, int n, int *points, int ct,
float *fir, float small)
*/
void construct_fir(int win, int siz, int n, int *points, int ct,
float *fir, float zsmall)
{
int i,j,k,ib,ic,ja,jb,cutoff;
float t1,t2;
float *firbuf;
float *fir_window;
unsigned short int *fir_permute;
COSIN_TABLE *fir_tab;
cutoff=ct;
if(cutoff < 1)cutoff=1;
if(cutoff>=siz/2)cutoff=siz/2-1;
// Construct a FIR filter of size siz with a cut-off frequency cutoff.
// The array of FIR filter coefficients is the pulse response of a FIR
// filter. Get it by taking the FFT.
// We use only the real half of a complex FFT.
firbuf=malloc(siz*(3*sizeof(float)+sizeof(short int)+sizeof(COSIN_TABLE)/2));
if(firbuf == NULL) {
lirerr(493367);
return;
}
fir_window=&firbuf[siz*2];
fir_permute=(void*)(&fir_window[siz]);
fir_tab=(void*)(&fir_permute[siz]);
init_fft(1,n, siz, fir_tab, fir_permute);
make_window(1,siz, win, fir_window);
firbuf[0]=1;
firbuf[1]=0;
jb=siz-1;
for(ja=1; ja<cutoff; ja++) {
firbuf[2*ja ]=1;
firbuf[2*jb ]=1;
firbuf[2*ja+1]=0;
firbuf[2*jb+1]=0;
jb--;
}
for(ja=cutoff; ja<siz/2; ja++) {
firbuf[2*ja ]=0;
firbuf[2*jb ]=0;
firbuf[2*ja+1]=0;
firbuf[2*jb+1]=0;
jb--;
}
firbuf[2*jb ]=0;
firbuf[2*jb+1]=0;
for( i=0; i<siz/2; i++) {
t1=firbuf[2*i ];
t2=firbuf[siz+2*i ];
firbuf[2*i ]=t1+t2;
firbuf[siz+2*i ]=fir_tab[i].cos*(t1-t2);
firbuf[siz+2*i+1]=fir_tab[i].sin*(t1-t2);
}
bulk_of_dif(siz, n, firbuf, fir_tab, yieldflag_ndsp_fft3);
for(i=0; i < siz; i+=2) {
ib=fir_permute[i];
ic=fir_permute[i+1];
fir[ib]=firbuf[2*i ]+firbuf[2*i+2];
fir[ic]=firbuf[2*i ]-firbuf[2*i+2];
}
// Now take the effects of our window into account.
// Note the order in which fir_window is stored.
for(i=0; i<siz/2; i++) {
fir[i]*=fir_window[2*i];
fir[siz/2+i]*=fir_window[2*i+1];
}
free(firbuf);
// The FIR filter must be symmetric. Actually forcing symmetry
// might reduce rounding errors slightly.
for(i=1; i<siz/2; i++) {
fir[i]=0.5*(fir[i]+fir[siz-i]);
fir[siz-i]=fir[i];
}
// There is no reason to make the FIR filter extend outside
// the range where the coefficients are below zsmall (supplied parameter).
t1=zsmall*fir[siz/2];
k=0;
while(fabs(fir[k]) < t1)k++;
j=k;
points[0]=0;
while(k<=siz-j-1) {
fir[points[0]]=fir[k];
k++;
points[0]++;
}
k=points[0];
if(k<siz)memset(&fir[k],0,siz-k);
// Normalize the FIR filter.
t1=0;
for(i=0; i<points[0]; i++) {
t1+=fir[i];
}
t1=1/t1;
for(i=0; i<points[0]; i++) {
fir[i]*=t1;
}
}
void init_basebmem(void)
{
float t1, t2;
int i, k, clr_size;
// Sampling rate for baseband has changed.
// Set flag to flush old data and reinit output.
reinit_baseb=FALSE;
lir_sched_yield();
if( (cg.oscill_on&6) != 0) {
clear_cg_traces();
cg.oscill_on=1;
cg_osc_ptr=0;
}
halt_rx_output();
if(kill_all_flag)return;
if(baseband_handle != NULL) {
baseband_handle=chk_free(baseband_handle);
}
// Make the daout buffer big enough to hold data for at least 6
// times longer than the rate at which the wideband thread posts
// to the narrowband thread.
t1=(6*fftx_size)/ui.rx_ad_speed;
// Make sure minimum is 0.5 seconds
if(t1<0.5)t1=0.5;
t1*=genparm[DA_OUTPUT_SPEED];
// and make sure we can hold 8192 samples
if(t1 < 8192)t1=8192;
daout_size=t1;
if(daout_size < 8*snd[RXDA].block_frames)daout_size=8*snd[RXDA].block_frames;
// Do not use snd[RXDA].framesize since bytes or channels may have changed.
// Do not multiply by channels here, allocate 8 times this size
make_power_of_two(&daout_size);
t1=3*(float)(daout_size)/genparm[DA_OUTPUT_SPEED];
if(t1<genparm[BASEBAND_STORAGE_TIME])t1=genparm[BASEBAND_STORAGE_TIME];
// Allocate memory for transformation from fft3 to the baseband.
// We are already in the baseband but the filter in use may allow
// a reduction of the sampling speed.
baseband_size=t1*baseband_sampling_speed;
make_power_of_two(&baseband_size);
daout_bufmask=daout_size-1;
cg_size=2+(COH_SIDE*text_width)/3;
cg_size&=0xfffffffe;
k=2+bg_flatpoints+2*bg_curvpoints;
if(genparm[THIRD_FFT_SINPOW] > 3)k+=2;
if(genparm[THIRD_FFT_SINPOW] == 8)k+=2;
cg_code_unit=0.5*(float)(mix2.size)/k;
cg_decay_factor=pow(0.5,0.2/cg_code_unit);
cw_waveform_max=14*cg_code_unit;
if(cw_waveform_max >= (int)(fftn_tmp_bytes/(2*sizeof(float)))) {
cw_waveform_max=fftn_tmp_bytes/(2*sizeof(float))-1;
}
cg_osc_offset=mix2.size+50*cg_code_unit;
while(baseband_size < 4*cg_osc_offset)baseband_size*=2;
if(baseband_size < 8*(int)mix2.size)baseband_size=8*mix2.size;
reduce:
;
if(baseband_size < 4*cg_osc_offset) {
cg_osc_offset=baseband_size/4;
}
cg_osc_offset_inc=cg_osc_offset/2;
max_cwdat=MAX_CW_PARTS*baseband_size/cg_code_unit;
baseband_mask=baseband_size-1;
baseband_neg=(7*baseband_size)>>3;
baseband_sizhalf=baseband_size>>1;
// The time stored in the baseband buffers is
// baseband_size/baseband_sampling_speed, the same time is contained in
// the basblock buffers in mix2.size/2 fewer points.
// Find out the number of basblock points that correspond to
// 5 seconds, the time constant for dB meter peak hold.
basblock_size=2*baseband_size/mix2.size;
basblock_mask=basblock_size-1;
basblock_hold_points=5*basblock_size*baseband_sampling_speed/baseband_size;
if(basblock_hold_points<3)basblock_hold_points=3;
if(basblock_hold_points>basblock_mask)basblock_hold_points=basblock_mask;
// When listening to wideband signals, the time between basblock
// points may become short.
// Make sure we do not update the coherent graph too often,
// graphics may be rather slow.
cg_update_interval=basblock_hold_points/20;
cg_update_count=0;
clear_agc();
fft3_interleave_ratio=make_interleave_ratio(THIRD_FFT_SINPOW);
// We get signals from back transformation of fft3.
// We must use an interleave ratio that makes the interleave points
// go even up in mix2.size.
mix2.interleave_points=fft3_interleave_ratio*mix2.size;
mix2.interleave_points&=0xfffffffe;
mix2.new_points=mix2.size-mix2.interleave_points;
fft3_interleave_points=mix2.interleave_points*(fft3_size/mix2.size);
fft3_interleave_ratio=(float)(fft3_interleave_points)/fft1_size;
fft3_new_points=fft3_size-fft3_interleave_points;
fft3_blocktime=(float)(fft3_new_points)/timf3_sampling_speed;
if( rx_mode == MODE_FM ) {
// ****************** fm_audiofil ***************************
// We need a low pass filter with cut-off as specified by the user.
// Make the cross-over region 250 Hz, but degrade the steepness
// in case the baseband sampling speed is very high.
fm_audiofil_size=baseband_sampling_speed/250;
if(fm_audiofil_size > 512)fm_audiofil_size=512;
fm_audiofil_n=make_power_of_two(&fm_audiofil_size);
}
if( rx_mode == MODE_FM && baseband_sampling_speed > 105000) {
// ****************** fmfil70 ************************
// We need a low pass filter with cut-off at about 70 kHz.
// Construct a FIR filter that will allow the cross-over region
// to be about 5 kHz.
// This filter will attenuate by about 100 dB above 80 kHz so we
// can resample the output if the baseband sampling rate is above 160kHz.
// For FM processing we have no reason to keep data in memory for
// a long time. Make the buffer one second.
fmfil70_size=baseband_sampling_speed/5000;
fmfil70_n=make_power_of_two(&fmfil70_size);
fm1_resample_ratio=baseband_sampling_speed/160000;
fm1_sampling_speed=baseband_sampling_speed/fm1_resample_ratio;
fm1_size=fm1_sampling_speed;
make_power_of_two(&fm1_size);
fm1_mask=fm1_size-1;
// ****************** fmfil55 ************************
// We need a low pass filter with cut-off at 55 kHz.
// Construct a FIR filter that will allow the cross-over region
// to be about 2 kHz.
fmfil55_size=baseband_sampling_speed/2000;
fmfil55_n=make_power_of_two(&fmfil55_size);
// ****************** fmfil ***************************
// We need a low pass filter with cut-off at about 17 kHz
// for wideband FM.
fmfil_size=baseband_sampling_speed/2000;
fmfil_n=make_power_of_two(&fmfil_size);
// ******************************************************
// ****************** fmfil_rds ***************************
// We need a low pass filter with cut-off at 1.8 kHz for RDS.
// Construct a FIR filter that will allow the cross-over region
// to be about 1 kHz.
fmfil_rds_size=baseband_sampling_speed/1000;
fmfil_rds_n=make_power_of_two(&fmfil_rds_size);
// ******************************************************
// For wideband FM we look for a pilot tone at 19 kHz.
// Make a filter with a bandwidth of about 100 Hz
fm_pilot_size=0.01*baseband_sampling_speed;
} else {
fm_pilot_size=0;
}
// ********************************************************
init_memalloc(basebmem, MAX_BASEB_ARRAYS);
mem(1,&baseb_out,baseband_size*2*baseb_channels*sizeof(float),0);
mem(2,&baseb_carrier,baseband_size*2*sizeof(float),0);
mem(3,&baseb_raw,baseband_size*2*sizeof(float),0);
mem(4,&baseb_raw_orthog,baseband_size*2*sizeof(float),0);
mem(5,&baseb,baseband_size*2*sizeof(float),0);
mem(6,&baseb_totpwr,baseband_size*sizeof(float),0);
mem(7,&baseb_carrier_ampl,baseband_size*sizeof(float),0);
mem(8,&mix2.permute,mix2.size*sizeof(short int),0);
mem(9,&mix2.table,mix2.size*sizeof(COSIN_TABLE)/2,0);
mem(10,&daout,8*daout_size,0);
mem(11,&cg_map,cg_size*cg_size*sizeof(float),0);
mem(13,&cg_traces,CG_MAXTRACE*MAX_CG_OSCW*sizeof(short int),0);
mem(16,&basblock_maxpower,basblock_size*sizeof(float),0);
mem(17,&basblock_avgpower,basblock_size*sizeof(float),0);
mem(171,&mix2.window,(mix2.size/2+1)*sizeof(float),0);
mem(172,&mix2.cos2win,mix2.new_points*sizeof(float),0);
mem(173,&mix2.sin2win,mix2.new_points*sizeof(float),0);
if(bg.agc_flag != 0) {
mem(18,&baseb_agc_level,bg.agc_flag*baseband_size*sizeof(float),0);
mem(1892,&baseb_agc_det,bg.agc_flag*baseband_size*sizeof(float),0);
mem(14,&baseb_upthreshold,bg.agc_flag*baseband_size*sizeof(float),0);
mem(15,&baseb_threshold,bg.agc_flag*baseband_size*sizeof(float),0);
}
if(genparm[CW_DECODE_ENABLE] != 0) {
if(bg.agc_flag == 0) {
mem(14,&baseb_upthreshold,baseband_size*sizeof(float),0);
mem(15,&baseb_threshold,baseband_size*sizeof(float),0);
}
mem(27,&baseb_ramp,baseband_size*sizeof(short int),0);
keying_spectrum_size=mix2.size/cg_code_unit;
if(keying_spectrum_size > (int)mix2.size/2)keying_spectrum_size=mix2.size/2;
mem(19,&baseb_envelope,baseband_size*2*sizeof(float),0);
mem(21,&dash_waveform,cw_waveform_max*2*sizeof(float),0);
mem(215,&dash_wb_waveform,cw_waveform_max*2*sizeof(float),0);
mem(225,&dot_wb_waveform,cw_waveform_max*sizeof(float),0);
mem(23,&baseb_fit,baseband_size*2*sizeof(float),0);
mem(24,&cw_carrier_window,mix2.size*sizeof(float),0);
mem(25,&mix2_tmp,2*mix2.size*sizeof(float),0);
mem(26,&mix2_pwr,mix2.size*sizeof(float),0);
mem(28,&baseb_tmp,baseband_size*2*sizeof(float),0);
mem(281,&baseb_sho1,baseband_size*2*sizeof(float),0);
mem(282,&baseb_sho2,baseband_size*2*sizeof(float),0);
mem(283,&baseb_wb_raw,baseband_size*2*sizeof(float),0);
mem(30,&cw,max_cwdat*sizeof(MORSE_DECODE_DATA),0);
mem(31,&keying_spectrum,keying_spectrum_size*sizeof(float),0);
mem(285,&baseb_clock,baseband_size*sizeof(float),0);
}
if(rx_mode == MODE_FM) {
mem(311,&baseb_fm_demod,baseband_size*2*baseb_channels*sizeof(float),0);
mem(1311,&fm_audiofil_fir,fm_audiofil_size*sizeof(float),0);
mem(314,&baseb_fm_phase,baseband_size*baseb_channels*sizeof(float),0);
if(fm_pilot_size > 0) {
if(baseb_channels != 1) {
lirerr(873105);
return;
}
mem(312,&baseb_fm_demod_low,baseband_size*baseb_channels*sizeof(float),0);
mem(313,&baseb_fm_demod_high,baseband_size*baseb_channels*sizeof(float),0);
mem(320,&fm_pilot_tone,2*fm_pilot_size*sizeof(float),0);
mem(321,&baseb_fm_pil2,baseband_size*baseb_channels*sizeof(float),0);
mem(322,&baseb_fm_pil3,baseband_size*2*baseb_channels*sizeof(float),0);
mem(324,&fmfil55_fir,fmfil55_size*sizeof(float),0);
mem(325,&baseb_fm_pil2det,baseband_size*baseb_channels*sizeof(float),0);
mem(326,&baseb_fm_pil3det,baseband_size*2*baseb_channels*sizeof(float),0);
mem(327,&baseb_fm_sumchan,baseband_size*baseb_channels*sizeof(float),0);
mem(328,&baseb_fm_diffchan,baseband_size*baseb_channels*sizeof(float),0);
mem(329,&baseb_fm_rdsraw,2*baseband_size*baseb_channels*sizeof(float),0);
mem(330,&fmfil_fir,fmfil_size*sizeof(float),0);
mem(331,&fmfil_rds_fir,fmfil_rds_size*sizeof(float),0);
mem(332,&baseb_fm_composite,baseband_size*baseb_channels*sizeof(float),0);
mem(333,&fmfil70_fir,fmfil70_size*sizeof(float),0);
mem(334,&fm1_all,fm1_size*sizeof(float),0);
}
}
baseband_totmem=memalloc(&baseband_handle,"baseband");
if(baseband_totmem == 0) {
lir_status=LIR_OK;
baseband_size/=2;
lir_pixwrite(bg.xleft+text_width,bg.ybottom-4*text_height,"BUFFERS REDUCED");
goto reduce;
}
k=fft3_size/mix2.size;
mix2.n=fft3_n;
while(k>1) {
mix2.n--;
k/=2;
}
if(mix2.n > 12) {
yieldflag_ndsp_mix2=TRUE;
} else {
yieldflag_ndsp_mix2=FALSE;
}
if(genparm[SECOND_FFT_ENABLE] == 0) {
if(ui.max_blocked_cpus > 1)yieldflag_ndsp_mix2=FALSE;
} else {
if(ui.max_blocked_cpus > 3)yieldflag_ndsp_mix2=FALSE;
}
prepare_mixer(&mix2, THIRD_FFT_SINPOW);
if(genparm[CW_DECODE_ENABLE] != 0) {
make_window(4,mix2.size, genparm[THIRD_FFT_SINPOW], cw_carrier_window);
}
memset(basblock_maxpower,0,basblock_size*sizeof(float));
memset(basblock_avgpower,0,basblock_size*sizeof(float));
clr_size=4*mix2.size;
if(clr_size > baseband_size)clr_size=baseband_size;
lir_sched_yield();
clear_baseb_arrays(0,clr_size);
if(genparm[CW_DECODE_ENABLE] != 0) {
for(i=0; i<max_cwdat; i++) {
cw[i].tmp=-1;
cw[i].unkn=-1;
}
lir_sched_yield();
memset(keying_spectrum,0,keying_spectrum_size*sizeof(float));
}
memset(baseb_out,0,clr_size*2*baseb_channels*sizeof(float));
baseb_pa=0;
baseb_py=0;
baseb_wts=0;
baseb_fx=0;
baseb_pf=0;
fm1_pa=0;
fm1_px=0;
rx_daout_cos=1;
rx_daout_sin=0;
if(kill_all_flag)return;
am_dclevel1=0;
am_dclevel2=0;
am_dclevel_factor1=pow(0.5,2000./(baseband_sampling_speed*(1<<bg.agc_release)));
am_dclevel_factor2=1-am_dclevel_factor1;
baseb_indicator_block=(baseband_mask+1)/INDICATOR_SIZE;
daout_indicator_block=(daout_bufmask+1)/INDICATOR_SIZE;
// *********************** pilot tone filter *****************************
if(fm_pilot_size > 0) {
t1=0;
t2=2*PI_L*19000./baseband_sampling_speed;
for(i=0; i<fm_pilot_size; i++) {
fm_pilot_tone[2*i ]=cos(t1);
fm_pilot_tone[2*i+1]=sin(t1);
t1+=t2;
}
// *********************** fmfil70 *********************************
i=fmfil70_size*68000/baseband_sampling_speed;
construct_fir(7,fmfil70_size, fmfil70_n,
&fmfil70_points, i, fmfil70_fir, 0.00001);
// *********************** fmfil55 *********************************
i=fmfil55_size*53000/baseband_sampling_speed;
construct_fir(7,fmfil55_size, fmfil55_n,
&fmfil55_points, i, fmfil55_fir, 0.00001);
// *********************** fmfil *********************************
t1=15000;
if(genparm[DA_OUTPUT_SPEED] < 40000) {
t1*=genparm[DA_OUTPUT_SPEED]/40000.;
}
i=fmfil_size*t1/baseband_sampling_speed;
construct_fir(7,fmfil_size, fmfil_n,
&fmfil_points, i, fmfil_fir, 0.00001);
// *********************** fmfil_rds ********************************
i=fmfil_rds_size*4000/baseband_sampling_speed;
construct_fir(7,fmfil_rds_size, fmfil_rds_n,
&fmfil_rds_points, i, fmfil_rds_fir, 0.0001);
// *****************************************************************
// collect the RDS phase in a leaky integrator of about 0.01 second.
rds_f1=pow(0.5,100/baseband_sampling_speed);
rds_f2=1-rds_f1;
rds_phase=0;
rds_power=0;
}
DEB"\nbaseband_sampling_speed=%f",baseband_sampling_speed);
}
void update_squelch_buttons(void)
{
int colour;
char s[40];
if(bg_filter_points > 5 && 2*bg_filter_points < bg_xpoints) {
colour=BG_ACTIVE_BUTTON_COLOR;
} else {
colour=BG_INACTIVE_BUTTON_COLOR;
}
settextcolor(colour);
sprintf(s,"%2d",bg.squelch_level);
show_button(&bgbutt[BG_SQUELCH_LEVEL],s);
if(bg.squelch_level == 0)colour=3;
settextcolor(colour);
sprintf(s,"%d",bg.squelch_time);
show_button(&bgbutt[BG_SQUELCH_TIME],s);
sprintf(s,"%2d",bg.squelch_point);
show_button(&bgbutt[BG_SQUELCH_POINT],s);
settextcolor(7);
}
void make_bg_filter(void)
{
int i,k,max,mm;
int j, iy;
int ja, jb, m;
float t1,t2,t3,t4;
int ib,ic;
// Set up the filter function in the baseband for
// the main signal and show it on the screen.
// bg.filter_flat is the size of the flat region
// of the filter in Hz (divided by 2)
// bg.filter_curv is the curvature expressed as ten times the distance
// in Hz to the 6dB point from the end of the flat region.
// The filter response is a flat center region with parabolic fall off.
//
// The filter is applied to the fft3 transforms with a symmetric
// function which is flat over 2*bg_flatpoints and falls off over
// bg_curvpoints at each end.
if(flat_xpixel > 0) {
for(i=bg_ymax; i<=bg_y4; i++)lir_setpixel(flat_xpixel, i,bg_background[i]);
}
if(curv_xpixel > 0) {
for(i=bg_y4; i<bg_y3; i++)lir_setpixel(curv_xpixel, i,bg_background[i]);
}
if(rx_mode != MODE_FM) {
// We should not generate frequencies above the Nyquist frequency
// of our output.
max=(float)(fft3_size)*genparm[DA_OUTPUT_SPEED]/(2*timf3_sampling_speed);
// We must make bg_filter_points smaller than fft3_size/2.
if(max > MAX_BASEBFILTER_WIDTH*fft3_size/2-2)max=MAX_BASEBFILTER_WIDTH*fft3_size/2-2;
} else {
max=MAX_BASEBFILTER_WIDTH*fft3_size/2-2;
}
bg_flatpoints=bg.filter_flat/bg_hz_per_pixel;
if(bg_flatpoints < 1)bg_flatpoints=1;
bg_curvpoints=0.1*bg.filter_curv/bg_hz_per_pixel;
if( genparm[CW_DECODE_ENABLE] != 0) {
k=bg_xpixels/(2*bg.pixels_per_point)-NOISE_SEP-
NOISE_FILTERS*(NOISE_POINTS+NOISE_SEP)-1;
if(max>k)max=k;
k=bg_flatpoints+2*bg_curvpoints-max;
if(k>0) {
k/=2;
bg_flatpoints-=k;
if(bg_flatpoints<1)bg_flatpoints=1;
bg_curvpoints=(max-bg_flatpoints-1)/2;
if(bg_curvpoints < 0) {
k=1+2*bg_curvpoints;
bg_flatpoints+=k;
bg_curvpoints=0;
}
}
} else {
k=bg_flatpoints+bg_curvpoints-max;
if(k>0) {
k=(k+1)/2;
bg_flatpoints-=k;
if(bg_flatpoints<1)bg_flatpoints=1;
bg_curvpoints=max-bg_flatpoints-1;
if(bg_curvpoints < 0) {
bg_flatpoints+=bg_curvpoints;
bg_curvpoints=0;
}
}
}
bg.filter_flat=bg_hz_per_pixel*bg_flatpoints;
bg.filter_curv=10*bg_hz_per_pixel*bg_curvpoints;
bg_curvpoints=0.1*bg.filter_curv/bg_hz_per_pixel;
bg_flatpoints=bg.filter_flat/bg_hz_per_pixel;
if(bg_flatpoints < 1)bg_flatpoints=1;
bgfil_weight=1;
bg_filterfunc[fft3_size/2]=1;
for(i=1; i<bg_flatpoints; i++) {
bg_filterfunc[fft3_size/2+i]=1;
bg_filterfunc[fft3_size/2-i]=1;
bgfil_weight+=2;
}
bg_filter_points=bg_flatpoints;
if(bg_curvpoints > 0) {
t1=.5/bg_curvpoints;
t2=1;
t3=t1;
t2=1-t3*t3;
t3+=t1;
while(t2 > 0 && bg_filter_points<fft3_size/2) {
bg_filterfunc[fft3_size/2+bg_filter_points]=t2;
bg_filterfunc[fft3_size/2-bg_filter_points]=t2;
bgfil_weight+=2*t2*t2;
t2=1-t3*t3;
t3+=t1;
bg_filter_points++;
}
}
for(i=bg_filter_points; i<fft3_size/2; i++) {
bg_filterfunc[fft3_size/2+i]=0;
bg_filterfunc[fft3_size/2-i]=0;
}
bg_filterfunc[0]=0;
// The filter we just specified determines the bandwidth of
// the signal we recover when backtransforming from fft3.
// Find out by what factor we should reduce the sampling speed
// for further processing
k=bg_filter_points+binshape_points;
k=(0.4*(fft3_size+0.5*k))/k;
make_power_of_two(&k);
if(genparm[CW_DECODE_ENABLE] != 0)k/=2;
if(k > 2) {
baseband_sampling_speed=2*timf3_sampling_speed/k;
if((int)mix2.size != 2*fft3_size/k ||
(reinit_baseb && mouse_active_flag==0)) {
mix2.size=2*fft3_size/k;
init_basebmem();
baseb_reset_counter++;
}
} else {
baseband_sampling_speed=timf3_sampling_speed;
if((int)mix2.size != fft3_size ||
(reinit_baseb && mouse_active_flag==0)) {
mix2.size=fft3_size;
init_basebmem();
baseb_reset_counter++;
}
}
timf1_to_baseband_speed_ratio=rint(timf1_sampling_speed/baseband_sampling_speed);
timf2_blockpower_block=4*ui.rx_rf_channels*timf1_to_baseband_speed_ratio;
if((int)mix2.size>fft3_size) {
lirerr(88888);
return;
}
carrfil_weight=1;
bg_carrfilter[fft3_size/2]=1;
mm=1;