forked from Tsukihime/OpenExpertSDR
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sdr.c
1172 lines (981 loc) · 34.7 KB
/
sdr.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
/* sdr.c
This file is part of a program that implements a Software-Defined Radio.
Copyright (C) 2004, 2005, 2006 by Frank Brickle, AB2KT and Bob McGwier, N4HY.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
The authors can be reached by email at
or
or by paper mail at
The DTTS Microwave Society
6 Kathleen Place
Bridgewater, NJ 08807
*/
#include <common.h>
//========================================================================
/* initialization and termination */
void
reset_meters(void)
{
if(uni.meter.flag)
{ // reset metering completely
int i, k;
for (i = 0; i < RXMETERPTS; i++) for (k = 0; k < MAXRX; k++) uni.meter.rx.val[k][i] = 0.0;
for (i = 0; i < TXMETERPTS; i++) uni.meter.tx.val[i] = 0.0;
}
}
void
reset_spectrum(void)
{
if(uni.spec.flag) reinit_spectrum(&uni.spec);
}
void
reset_counters(void)
{
int k;
for (k = 0; k < uni.multirx.nrx; k++) rx[k].tick = 0;
tx.tick = 0;
}
//========================================================================
/* global and general info,
not specifically attached to
tx, rx, or scheduling */
PRIVATE void
setup_all(REAL rate,
int buflen,
SDRMODE mode, char *wisdom, int specsize, int numrecv, int cpdsize)
{
uni.samplerate = rate;
uni.buflen = buflen;
uni.mode.sdr = mode;
uni.mode.trx = RX;
uni.wisdom.path = wisdom;
uni.wisdom.bits = FFTW_ESTIMATE;
{
FILE *f = fopen(uni.wisdom.path, "r");
if(f)
{
char wisdomstring[32768];
size_t nread = fread(wisdomstring, 1, 32768, f);
if(fftwf_import_wisdom_from_string(wisdomstring) != 0) uni.wisdom.bits = FFTW_MEASURE;
fclose(f);
}
}
if(uni.meter.flag)
{
reset_meters();
}
uni.spec.rxk = 0;
uni.spec.buflen = uni.buflen;
uni.spec.scale = SPEC_PWR;
uni.spec.type = SPEC_POST_FILT;
uni.spec.size = specsize;
uni.spec.planbits = uni.wisdom.bits;
init_spectrum(&uni.spec);
// set which receiver is listening to commands
uni.multirx.lis = 0;
uni.multirx.nrx = numrecv;
// set mixing of input from aux ports
uni.mix.rx.flag = uni.mix.tx.flag = FALSE;
uni.mix.rx.gain = uni.mix.tx.gain = 1.0;
uni.cpdlen = cpdsize;
uni.tick = 0;
}
/* purely rx */
PRIVATE void
setup_rx(int k)
{
/* conditioning */
rx[k].iqfix = newCorrectIQ(0.0, 1.0);
rx[k].filt.coef = newFIR_Bandpass_COMPLEX(-4800.0,
4800.0,
uni.samplerate, uni.buflen + 1);
rx[k].filt.ovsv =
newFiltOvSv(FIRcoef(rx[k].filt.coef), FIRsize(rx[k].filt.coef),
uni.wisdom.bits);
normalize_vec_COMPLEX(rx[k].filt.ovsv->zfvec, rx[k].filt.ovsv->fftlen);
rx[k].output_gain = 1.0f;
// hack for EQ
rx[k].filt.save =
newvec_COMPLEX(rx[k].filt.ovsv->fftlen, "RX filter cache");
memcpy((char *)rx[k].filt.save, (char *)rx[k].filt.ovsv->zfvec,
rx[k].filt.ovsv->fftlen * sizeof(COMPLEX));
/* buffers */
/* note we overload the internal filter buffers
we just created */
rx[k].buf.i = newCXB(FiltOvSv_fetchsize(rx[k].filt.ovsv),
FiltOvSv_fetchpoint(rx[k].filt.ovsv),
"init rx[k].buf.i");
rx[k].buf.o = newCXB(FiltOvSv_storesize(rx[k].filt.ovsv),
FiltOvSv_storepoint(rx[k].filt.ovsv),
"init rx[k].buf.o");
/* conversion */
rx[k].osc.freq = -11025.0;
rx[k].osc.phase = 0.0;
rx[k].osc.gen = newOSC(uni.buflen,
ComplexTone,
rx[k].osc.freq,
rx[k].osc.phase,
uni.samplerate, "SDR RX Oscillator");
rx[k].dttspagc.gen = newDttSPAgc(1, // mode kept around for control reasons alone
CXBbase(rx[k].buf.o), // input buffer
CXBsize(rx[k].buf.o), // output buffer
1.0f, // Target output
2.0f, // Attack time constant in ms
500, // Decay time constant in ms
1.0, // Slope
500, //Hangtime in ms
uni.samplerate, // Sample rate
31622.8f, // Maximum gain as a multipler, linear not dB
0.00001f, // Minimum gain as a multipler, linear not dB
1.0, // Set the current gain
"AGC" // Set a tag for an error message if the memory allocation fails
);
rx[k].dttspagc.flag = TRUE;
rx[k].grapheq.gen = new_EQ(rx[k].buf.o, uni.samplerate, uni.wisdom.bits);
rx[k].grapheq.flag = FALSE;
/* demods */
rx[k].am.gen = newAMD(uni.samplerate, // REAL samprate
0.0, // REAL f_initial
-2000.0, // REAL f_lobound,
2000.0, // REAL f_hibound,
200.0, // REAL f_bandwid,
CXBsize(rx[k].buf.o), // int size,
CXBbase(rx[k].buf.o), // COMPLEX *ivec,
CXBbase(rx[k].buf.o), // COMPLEX *ovec,
AMdet, // AM Mode AMdet == rectifier,
// SAMdet == synchronous detector
"AM detector blew"); // char *tag
rx[k].fm.gen = newFMD(uni.samplerate, // REAL samprate
0.0, // REAL f_initial
-6000.0, // REAL f_lobound
6000.0, // REAL f_hibound
5000.0, // REAL f_bandwid
CXBsize(rx[k].buf.o), // int size
CXBbase(rx[k].buf.o), // COMPLEX *ivec
CXBbase(rx[k].buf.o), // COMPLEX *ovec
"New FM Demod structure"); // char *error message;
/* noise reduction */
rx[k].anf.gen = new_lmsr(rx[k].buf.o, // CXB signal,
64, // int delay,
0.01f, // REAL adaptation_rate,
0.00001f, // REAL leakage,
45, // int adaptive_filter_size,
LMADF_INTERFERENCE);
rx[k].anf.flag = FALSE;
rx[k].banf.gen =
new_blms(rx[k].buf.o, 0.00001f, 0.005f, LMADF_INTERFERENCE,
uni.wisdom.bits);
rx[k].banf.flag = FALSE;
rx[k].anr.gen = new_lmsr(rx[k].buf.o, // CXB signal,
64, // int delay,
0.01f, // REAL adaptation_rate,
0.00001f, // REAL leakage,
45, // int adaptive_filter_size,
LMADF_NOISE);
rx[k].anr.flag = FALSE;
rx[k].banr.gen =
new_blms(rx[k].buf.o, 0.00001f, 0.005f, LMADF_NOISE, uni.wisdom.bits);
rx[k].banr.flag = FALSE;
rx[k].nb.thresh = 3.3f;
rx[k].nb.gen = new_noiseblanker(rx[k].buf.i, rx[k].nb.thresh);
rx[k].nb.flag = FALSE;
rx[k].nb_sdrom.thresh = 2.5f;
rx[k].nb_sdrom.gen = new_noiseblanker(rx[k].buf.i, rx[k].nb_sdrom.thresh);
rx[k].nb_sdrom.flag = FALSE;
rx[k].spot.gen = newSpotToneGen(-12.0, // gain
700.0, // freq
5.0, // ms rise
5.0, // ms fall
uni.buflen, // length of spot tone buffer
uni.samplerate // sample rate
);
memset((char *)&rx[k].squelch, 0, sizeof(rx[k].squelch));
rx[k].squelch.thresh = -150.0;
rx[k].squelch.power = 0.0;
rx[k].squelch.flag = rx[k].squelch.running = rx[k].squelch.set = FALSE;
rx[k].squelch.num = uni.buflen - 48;
rx[k].cpd.gen = newWSCompander(uni.cpdlen, 0.0, rx[k].buf.o);
rx[k].cpd.flag = FALSE;
rx[k].mode = uni.mode.sdr;
rx[k].bin.flag = FALSE;
{
REAL pos = 0.5, // 0 <= pos <= 1, left->right
theta = (REAL)((1.0 - pos) * M_PI / 2.0);
rx[k].azim = Cmplx((REAL)cos(theta), (IMAG)sin(theta));
}
rx[k].tick = 0;
}
/* purely tx */
PRIVATE void
setup_tx(void)
{
/* conditioning */
tx.iqfix = newCorrectIQ(0.0, 1.0);
tx.filt.coef = newFIR_Bandpass_COMPLEX(300.0,
3000.0,
uni.samplerate, uni.buflen + 1);
tx.filt.ovsv = newFiltOvSv(FIRcoef(tx.filt.coef),
FIRsize(tx.filt.coef), uni.wisdom.bits);
normalize_vec_COMPLEX(tx.filt.ovsv->zfvec, tx.filt.ovsv->fftlen);
// hack for EQ
tx.filt.save = newvec_COMPLEX(tx.filt.ovsv->fftlen, "TX filter cache");
memcpy((char *)tx.filt.save,
(char *)tx.filt.ovsv->zfvec,
tx.filt.ovsv->fftlen * sizeof(COMPLEX));
/* buffers */
tx.buf.i = newCXB(FiltOvSv_fetchsize(tx.filt.ovsv),
FiltOvSv_fetchpoint(tx.filt.ovsv), "init tx.buf.i");
tx.buf.o = newCXB(FiltOvSv_storesize(tx.filt.ovsv),
FiltOvSv_storepoint(tx.filt.ovsv), "init tx.buf.o");
tx.dcb.flag = FALSE;
tx.dcb.gen = newDCBlocker(DCB_MED, tx.buf.i);
/* conversion */
tx.osc.freq = 0.0;
tx.osc.phase = 0.0;
tx.osc.gen = newOSC(uni.buflen,
ComplexTone,
tx.osc.freq,
tx.osc.phase, uni.samplerate, "SDR TX Oscillator");
tx.am.carrier_level = 0.5f;
tx.fm.cvtmod2freq = (REAL)(3000.0 * TWOPI / uni.samplerate); //3 kHz deviation
tx.leveler.gen = newDttSPAgc(1, // mode kept around for control reasons
CXBbase(tx.buf.i), // input buffer
CXBsize(tx.buf.i), // output buffer
1.1f, // Target output
2, // Attack time constant in ms
500, // Decay time constant in ms
1, // Slope
500, //Hangtime in ms
uni.samplerate, // Sample rate
1.778f, // Maximum gain as a multipler, linear not dB
1.0, // Minimum gain as a multipler, linear not dB
1.0, // Set the current gain
"LVL" // Set a tag for an error message if the memory allocation fails
);
tx.leveler.flag = TRUE;
tx.grapheq.gen = new_EQ(tx.buf.i, uni.samplerate, uni.wisdom.bits);
tx.grapheq.flag = FALSE;
memset((char *)&tx.squelch, 0, sizeof(tx.squelch));
tx.squelch.thresh = -40.0;
tx.squelch.power = 0.0;
tx.squelch.flag = FALSE;
tx.squelch.running = tx.squelch.set = FALSE;
tx.squelch.num = uni.buflen - 48;
tx.alc.gen = newDttSPAgc(1, // mode kept around for control reasons alone
CXBbase(tx.buf.i), // input buffer
CXBsize(tx.buf.i), // output buffer
1.08f, // Target output
2, // Attack time constant in ms
10, // Decay time constant in ms
1, // Slope
500, //Hangtime in ms
uni.samplerate, 1.0, // Maximum gain as a multipler, linear not dB
.000001f, // Minimum gain as a multipler, linear not dB
1.0, // Set the current gain
"ALC" // Set a tag for an error message if the memory allocation fails
);
tx.alc.flag = TRUE;
tx.spr.gen =
newSpeechProc(0.4f, 3.0, CXBbase(tx.buf.i), CXBsize(tx.buf.o));
tx.spr.flag = FALSE;
tx.cpd.gen = newWSCompander(uni.cpdlen, -3.0, tx.buf.i);
tx.cpd.flag = FALSE;
//tx.scl.dc = cxzero;
tx.mode = uni.mode.sdr;
tx.tick = 0;
/* not much else to do for TX */
}
/* how the outside world sees it */
void
setup_workspace(REAL rate,
int buflen,
SDRMODE mode,
char *wisdom, int specsize, int numrecv, int cpdsize)
{
int k;
setup_all(rate, buflen, mode, wisdom, specsize, numrecv, cpdsize);
for (k = 0; k < uni.multirx.nrx; k++)
{
setup_rx(k);
uni.multirx.act[k] = FALSE;
}
uni.multirx.act[0] = TRUE;
uni.multirx.nac = 1;
setup_tx();
}
void
destroy_workspace(void)
{
int k;
/* TX */
delWSCompander(tx.cpd.gen);
delSpeechProc(tx.spr.gen);
delDttSPAgc(tx.leveler.gen);
delDttSPAgc(tx.alc.gen);
delOSC(tx.osc.gen);
delDCBlocker(tx.dcb.gen);
delvec_COMPLEX(tx.filt.save);
delFiltOvSv(tx.filt.ovsv);
delFIR_Bandpass_COMPLEX(tx.filt.coef);
delCorrectIQ(tx.iqfix);
delCXB(tx.buf.o);
delCXB(tx.buf.i);
/* RX */
for (k = 0; k < uni.multirx.nrx; k++)
{
delWSCompander(rx[k].cpd.gen);
delSpotToneGen(rx[k].spot.gen);
delDttSPAgc(rx[k].dttspagc.gen);
del_nb(rx[k].nb_sdrom.gen);
del_nb(rx[k].nb.gen);
del_lmsr(rx[k].anf.gen);
del_lmsr(rx[k].anr.gen);
del_blms(rx[k].banf.gen);
del_blms(rx[k].banr.gen);
delAMD(rx[k].am.gen);
delFMD(rx[k].fm.gen);
delOSC(rx[k].osc.gen);
delvec_COMPLEX(rx[k].filt.save);
delFiltOvSv(rx[k].filt.ovsv);
delFIR_Bandpass_COMPLEX(rx[k].filt.coef);
delCorrectIQ(rx[k].iqfix);
delCXB(rx[k].buf.o);
delCXB(rx[k].buf.i);
}
/* all */
finish_spectrum(&uni.spec);
}
//////////////////////////////////////////////////////////////////////////
// execution
//////////////////////////////////////////////////////////////////////////
//========================================================================
// util
PRIVATE void
CXBscl(CXB buff, REAL scl)
{
int i;
for (i = 0; i < CXBhave(buff); i++) CXBdata(buff, i) = Cscl(CXBdata(buff, i), scl);
}
PRIVATE REAL
CXBnorm(CXB buff)
{
int i;
REAL sum = 0.0;
for (i = 0; i < CXBhave(buff); i++) sum += Csqrmag(CXBdata(buff, i));
return ((REAL)sqrt(sum));
}
PRIVATE REAL
CXBnormsqr(CXB buff)
{
int i;
REAL sum = 0.0;
for (i = 0; i < CXBhave(buff); i++) sum += Csqrmag(CXBdata(buff, i));
return ((REAL)(sum));
}
PRIVATE REAL
CXBpeak(CXB buff)
{
int i;
REAL maxsam = 0.0;
for (i = 0; i < CXBhave(buff); i++) maxsam = max(Cmag(CXBdata(buff, i)), maxsam);
return (maxsam);
}
PRIVATE REAL
CXBpeakpwr(CXB buff)
{
int i;
REAL maxpwr = 0.0;
for (i = 0; i < CXBhave(buff); i++) maxpwr = max(Csqrmag(CXBdata(buff, i)), maxpwr);
return (maxpwr);
}
//========================================================================
/* all */
// unfortunate duplication here, due to
// multirx vs monotx
PRIVATE void
do_rx_meter(int k, CXB buf, int tap)
{
COMPLEX *vec = CXBbase(buf);
int i, len = CXBhave(buf);
switch(tap)
{
case RXMETER_PRE_CONV:
for (i = 0; i < len; i++) uni.meter.rx.val[k][ADC_REAL] =
(REAL)max(fabs(vec[i].re), uni.meter.rx.val[k][ADC_REAL]);
uni.meter.rx.val[k][ADC_REAL] = (REAL)(20.0 * log10(uni.meter.rx.val[k][ADC_REAL] + 1e-10));
for (i = 0; i < len; i++) uni.meter.rx.val[k][ADC_IMAG] =
(REAL)max(fabs(vec[i].im), uni.meter.rx.val[k][ADC_IMAG]);
uni.meter.rx.val[k][ADC_IMAG] = (REAL)(20.0 * log10(uni.meter.rx.val[k][ADC_IMAG] + 1e-10));
break;
case RXMETER_POST_FILT:
uni.meter.rx.val[k][SIGNAL_STRENGTH] = 0;
for (i = 0; i < len; i++) uni.meter.rx.val[k][SIGNAL_STRENGTH] += Csqrmag(vec[i]);
rx[k].norm = uni.meter.rx.val[k][SIGNAL_STRENGTH] / (REAL)len;
uni.meter.rx.val[k][SIGNAL_STRENGTH] =
(REAL)(10.0 * log10(uni.meter.rx.val[k][SIGNAL_STRENGTH] + 1e-20));
if(uni.meter.rx.mode[k] == SIGNAL_STRENGTH) uni.meter.rx.val[k][AVG_SIGNAL_STRENGTH] = uni.meter.rx.val[k][SIGNAL_STRENGTH];
uni.meter.rx.val[k][AVG_SIGNAL_STRENGTH] =
(REAL)(0.95 * uni.meter.rx.val[k][AVG_SIGNAL_STRENGTH] +
0.05 * uni.meter.rx.val[k][SIGNAL_STRENGTH]);
break;
case RXMETER_POST_AGC:
uni.meter.rx.val[k][AGC_GAIN] =
(REAL)(20.0 * log10(rx[k].dttspagc.gen->gain.now + 1e-10));
break;
default:
break;
}
}
PRIVATE void
do_rx_spectrum(int k, CXB buf, int type)
{
if(uni.spec.flag && k == uni.spec.rxk && type == uni.spec.type)
{
if((uni.spec.type == SPEC_POST_DET) && (!rx[k].bin.flag))
{
int i;
for (i = 0; i < CXBhave(rx[k].buf.o); i++) CXBdata(uni.spec.accum, uni.spec.fill + i) = Cmplx(CXBreal(rx[k].buf.o, i) * 1.414f, 0.0);
} else
{
memcpy((char *)&CXBdata(uni.spec.accum, uni.spec.fill),
(char *)CXBbase(buf), CXBsize(buf) * sizeof(COMPLEX));
}
uni.spec.fill = (uni.spec.fill + CXBsize(buf)) & uni.spec.mask;
}
}
PRIVATE void
do_tx_spectrum(CXB buf)
{
if(uni.spec.type == SPEC_PREMOD)
{
int i;
for (i = 0; i < CXBhave(tx.buf.i); i++) CXBdata(uni.spec.accum, uni.spec.fill + i) = Cmplx(CXBreal(tx.buf.i, i), 0.0);
} else
{
memcpy((char *)&CXBdata(uni.spec.accum, uni.spec.fill),
(char *)CXBbase(buf), CXBsize(buf) * sizeof(COMPLEX));
}
uni.spec.fill = (uni.spec.fill + CXBsize(buf)) & uni.spec.mask;
}
//========================================================================
/* RX processing */
PRIVATE void
should_do_rx_squelch(int k)
{
if(rx[k].squelch.flag)
{
int i, n = CXBhave(rx[k].buf.o);
rx[k].squelch.power = 0.0;
for (i = 0; i < n; i++) rx[k].squelch.power += Csqrmag(CXBdata(rx[k].buf.o, i));
if(10.0 * log10(rx[k].squelch.power + 1e-17) < rx[k].squelch.thresh) rx[k].squelch.set = TRUE;
else rx[k].squelch.set = FALSE;
} else
{
rx[k].squelch.set = FALSE;
}
}
PRIVATE void
should_do_tx_squelch(void)
{
if(tx.squelch.flag)
{
int i, n = CXBsize(tx.buf.i);
tx.squelch.power = 0.0;
for (i = 0; i < n; i++) tx.squelch.power += Csqrmag(CXBdata(tx.buf.i, i));
if((-30 + 10.0 * log10(tx.squelch.power + 1e-17)) < tx.squelch.thresh) tx.squelch.set = TRUE;
else tx.squelch.set = FALSE;
} else
{
tx.squelch.set = FALSE;
}
}
// apply squelch
// slew into silence first time
PRIVATE void
do_squelch(int k)
{
if(!rx[k].squelch.running)
{
int i, m = rx[k].squelch.num, n = CXBhave(rx[k].buf.o) - m;
for (i = 0; i < m; i++)
{
CXBdata(rx[k].buf.o, i) =
Cscl(CXBdata(rx[k].buf.o, i), (REAL)(1.0 - (REAL)i / m));
}
memset((void *)(CXBbase(rx[k].buf.o) + m), 0, n * sizeof(COMPLEX));
rx[k].squelch.running = TRUE;
} else
{
memset((void *)CXBbase(rx[k].buf.o),
0, CXBhave(rx[k].buf.o) * sizeof(COMPLEX));
}
}
PRIVATE void
do_tx_squelch(void)
{
if(!tx.squelch.running)
{
int i, m = tx.squelch.num, n = CXBhave(tx.buf.i) - m;
for (i = 0; i < m; i++)
{
CXBdata(tx.buf.i, i) =
Cscl(CXBdata(tx.buf.i, i), (REAL)(1.0 - (REAL)i / m));
}
memset((void *)(CXBbase(tx.buf.i) + m), 0, n * sizeof(COMPLEX));
tx.squelch.running = TRUE;
} else
{
memset((void *)CXBbase(tx.buf.i),
0, CXBhave(tx.buf.i) * sizeof(COMPLEX));
}
}
// lift squelch
// slew out from silence to full scale
PRIVATE void
no_squelch(int k)
{
if(rx[k].squelch.running)
{
int i, m = rx[k].squelch.num;
for (i = 0; i < m; i++)
{
CXBdata(rx[k].buf.o, i) =
Cscl(CXBdata(rx[k].buf.o, i), (REAL)i / m);
}
rx[k].squelch.running = FALSE;
}
}
PRIVATE void
no_tx_squelch()
{
if(tx.squelch.running)
{
int i, m = tx.squelch.num;
for (i = 0; i < m; i++)
{
CXBdata(tx.buf.i, i) =
Cscl(CXBdata(tx.buf.i, i), (REAL)i / m);
}
tx.squelch.running = FALSE;
}
}
/* pre-condition for (nearly) all RX modes */
PRIVATE void
do_rx_pre(int k)
{
int i, n = min(CXBhave(rx[k].buf.i), uni.buflen);
if(rx[k].nb.flag) noiseblanker(rx[k].nb.gen);
if(rx[k].nb_sdrom.flag) SDROMnoiseblanker(rx[k].nb_sdrom.gen);
// metering for uncorrected values here
do_rx_meter(k, rx[k].buf.i, RXMETER_PRE_CONV);
correctIQ(rx[k].buf.i, rx[k].iqfix);
/* 2nd IF conversion happens here */
if(rx[k].osc.gen->Frequency != 0.0)
{
ComplexOSC(rx[k].osc.gen);
for (i = 0; i < n; i++) CXBdata(rx[k].buf.i, i) = Cmul(CXBdata(rx[k].buf.i, i), OSCCdata(rx[k].osc.gen, i));
}
/* filtering, metering, spectrum, squelch, & AGC */
//do_rx_meter (k, rx[k].buf.i, RXMETER_PRE_FILT);
do_rx_spectrum(k, rx[k].buf.i, SPEC_PRE_FILT);
if(rx[k].mode != SPEC)
{
if(rx[k].tick == 0) reset_OvSv(rx[k].filt.ovsv);
filter_OvSv(rx[k].filt.ovsv);
} else
{
memcpy(CXBbase(rx[k].buf.o), CXBbase(rx[k].buf.i),
sizeof(COMPLEX) * CXBhave(rx[k].buf.i));
}
CXBhave(rx[k].buf.o) = CXBhave(rx[k].buf.i);
do_rx_meter(k, rx[k].buf.o, RXMETER_POST_FILT);
do_rx_spectrum(k, rx[k].buf.o, SPEC_POST_FILT);
if(rx[k].cpd.flag) WSCompand(rx[k].cpd.gen);
should_do_rx_squelch(k);
DttSPAgc(rx[k].dttspagc.gen, rx[k].tick);
do_rx_meter(k, rx[k].buf.o, RXMETER_POST_AGC);
do_rx_spectrum(k, rx[k].buf.o, SPEC_POST_AGC);
}
PRIVATE void
do_rx_post(int k)
{
int i, n = CXBhave(rx[k].buf.o);
if(rx[k].squelch.set)
{
do_squelch(k);
} else // if (!rx[k].squelch.set)
{
no_squelch(k);
// spotting tone
if(rx[k].spot.flag)
{
// remember whether it's turned itself off during this pass
rx[k].spot.flag = SpotTone(rx[k].spot.gen);
for (i = 0; i < n; i++) CXBdata(rx[k].buf.o, i) = Cadd(CXBdata(rx[k].buf.o, i),
CXBdata(rx[k].spot.gen->buf,
i));
}
}
if(rx[k].grapheq.flag && rx[k].mode != DRM) graphiceq(rx[k].grapheq.gen);
do_rx_spectrum(k, rx[k].buf.o, SPEC_POST_DET);
// not binaural?
// position in stereo field
if(!rx[k].bin.flag)
{
if(uni.multirx.nac == 1)
{
for (i = 0; i < n; i++) CXBdata(rx[k].buf.o, i) = Cscl(rx[k].azim, 1.41421356f * CXBreal(rx[k].buf.o, i));
} else
{
for (i = 0; i < n; i++) CXBdata(rx[k].buf.o, i) = Cscl(rx[k].azim, CXBreal(rx[k].buf.o, i));
}
}
if(rx[k].output_gain != 1.0) for (i = 0; i < n; i++) CXBdata(rx[k].buf.o, i) = Cscl(CXBdata(rx[k].buf.o, i), rx[k].output_gain);
}
/* demod processing */
PRIVATE void
do_rx_SBCW(int k)
{
if(rx[k].bin.flag)
{
if((rx[k].banr.flag) && (rx[k].anr.flag)) blms_adapt(rx[k].banr.gen);
if((rx[k].banf.flag) && (rx[k].anf.flag)) blms_adapt(rx[k].banf.gen);
} else
{
int i;
if(rx[k].anr.flag) if(rx[k].banr.flag) blms_adapt(rx[k].banr.gen);
else lmsr_adapt(rx[k].anr.gen);
if(rx[k].anf.flag)
{
if(rx[k].banf.flag) blms_adapt(rx[k].banf.gen);
else lmsr_adapt(rx[k].anf.gen);
}
for (i = 0; i < CXBhave(rx[k].buf.o); i++) CXBimag(rx[k].buf.o, i) = CXBreal(rx[k].buf.o, i);
}
}
PRIVATE void
do_rx_AM(int k)
{
AMDemod(rx[k].am.gen);
if(rx[k].anf.flag)
{
if(!rx[k].banf.flag) lmsr_adapt(rx[k].anf.gen);
else blms_adapt(rx[k].banf.gen);
}
}
PRIVATE void
do_rx_FM(int k)
{
FMDemod(rx[k].fm.gen);
}
PRIVATE void
do_rx_DRM(int k)
{
}
PRIVATE void
do_rx_SPEC(int k)
{
}
PRIVATE void
do_rx_NIL(int k)
{
int i, n = min(CXBhave(rx[k].buf.i), uni.buflen);
for (i = 0; i < n; i++) CXBdata(rx[k].buf.o, i) = cxzero;
}
/* overall dispatch for RX processing */
PRIVATE void
do_rx(int k)
{
do_rx_pre(k);
switch(rx[k].mode)
{
case DIGU:
case DIGL:
case USB:
case LSB:
case CWU:
case CWL:
case DSB:
do_rx_SBCW(k);
break;
case AM:
case SAM:
do_rx_AM(k);
break;
case FMN:
do_rx_FM(k);
break;
case DRM:
do_rx_DRM(k);
break;
case SPEC:
default:
do_rx_SPEC(k);
break;
}
do_rx_post(k);
}
//==============================================================
/* TX processing */
PRIVATE REAL micsave = 0.0f, alcsave = 0.0f, pwrsave =
0.0f, levelersave = 0.0f, eqtapsave = 0.0f, compsave =
0.0f, cpdrsave = 0.0f;
/* pre-condition for (nearly) all TX modes */
PRIVATE REAL peaksmooth = 0.0;
PRIVATE void
do_tx_meter(CXB buf, TXMETERTYPE mt)
{
COMPLEX *vec = CXBbase(buf);
int i, len = CXBhave(buf);
switch(mt)
{
case TX_MIC:
for (i = 0; i < CXBhave(tx.buf.i); i++) micsave = (REAL)(0.9995 * micsave +
0.0005 * Csqrmag(CXBdata(tx.buf.i, i)));
uni.meter.tx.val[TX_MIC] = (REAL)(-10.0 * log10(micsave + 1e-16));
break;
case TX_PWR:
for (i = 0, uni.meter.tx.val[TX_PWR] = 0.0000001f;
i < CXBhave(tx.buf.o); i++) uni.meter.tx.val[TX_PWR] += Csqrmag(CXBdata(tx.buf.o, i));
uni.meter.tx.val[TX_PWR] /= (REAL)len;
break;
case TX_ALC:
for (i = 0; i < CXBhave(tx.buf.i); i++) alcsave = (REAL)(0.9995 * alcsave +
0.0005 * Csqrmag(CXBdata(tx.buf.i, i)));
uni.meter.tx.val[TX_ALC] = (REAL)(-10.0 * log10(alcsave + 1e-16));
uni.meter.tx.val[TX_ALC_G] = (REAL)(20.0 * log10(tx.alc.gen->gain.now + 1e-16));
break;
case TX_EQtap:
for (i = 0; i < CXBhave(tx.buf.i); i++) eqtapsave =
(REAL)(0.9995 * eqtapsave +
0.0005 * Csqrmag(CXBdata(tx.buf.i, i)));
uni.meter.tx.val[TX_EQtap] =
(REAL)(-10.0 * log10(eqtapsave + 1e-16));
break;
case TX_LEVELER:
for (i = 0; i < CXBhave(tx.buf.i); i++) levelersave =
(REAL)(0.9995 * levelersave +
0.0005 * Csqrmag(CXBdata(tx.buf.i, i)));
uni.meter.tx.val[TX_LEVELER] = (REAL)(-10.0 * log10(levelersave + 1e-16));
uni.meter.tx.val[TX_LVL_G] = (REAL)(20.0 * log10(tx.leveler.gen->gain.now + 1e-16));
break;
case TX_COMP:
for (i = 0; i < CXBhave(tx.buf.i); i++) compsave =
(REAL)(0.9995 * compsave +
0.0005 * Csqrmag(CXBdata(tx.buf.i, i)));
uni.meter.tx.val[TX_COMP] = (REAL)(-10.0 * log10(compsave + 1e-16));
break;
case TX_CPDR:
for (i = 0; i < CXBhave(tx.buf.i); i++) cpdrsave =
(REAL)(0.9995 * cpdrsave +
0.0005 * Csqrmag(CXBdata(tx.buf.i, i)));
uni.meter.tx.val[TX_CPDR] = (REAL)(-10.0 * log10(cpdrsave + 1e-16));
break;
default:
break;
}
}
PRIVATE void
do_tx_pre(void)
{
int i, n = CXBhave(tx.buf.i);
for (i = 0; i < n; i++) CXBdata(tx.buf.i, i) = Cmplx(CXBimag(tx.buf.i, i), 0.0);
//fprintf(stderr,"Peak value = %f\n",CXBpeakpwr(tx.buf.i));
if(tx.dcb.flag) DCBlock(tx.dcb.gen);
do_tx_meter(tx.buf.i, TX_MIC);
should_do_tx_squelch();
if(tx.squelch.set) do_tx_squelch();
else //if (!tx.squelch.set)
no_tx_squelch();
if((tx.mode != DIGU) && (tx.mode != DIGL))
{
if(tx.grapheq.flag) graphiceq(tx.grapheq.gen);
do_tx_meter(tx.buf.i, TX_EQtap);
if(tx.leveler.flag) DttSPAgc(tx.leveler.gen, tx.tick);
do_tx_meter(tx.buf.i, TX_LEVELER);
if(tx.spr.flag) SpeechProcessor(tx.spr.gen);
do_tx_meter(tx.buf.i, TX_COMP);
if(tx.cpd.flag) WSCompand(tx.cpd.gen);
do_tx_meter(tx.buf.i, TX_CPDR);
} else
{
do_tx_meter(tx.buf.i, TX_EQtap);
do_tx_meter(tx.buf.i, TX_LEVELER);
do_tx_meter(tx.buf.i, TX_LVL_G);
do_tx_meter(tx.buf.i, TX_COMP);
do_tx_meter(tx.buf.i, TX_CPDR);
}
}
PRIVATE void
do_tx_post(void)
{
CXBhave(tx.buf.o) = CXBhave(tx.buf.i);
if(tx.tick == 0) reset_OvSv(tx.filt.ovsv);
filter_OvSv(tx.filt.ovsv);
if(uni.spec.flag) do_tx_spectrum(tx.buf.o);
// meter modulated signal
if(tx.osc.gen->Frequency != 0.0)
{
int i;
ComplexOSC(tx.osc.gen);
for (i = 0; i < CXBhave(tx.buf.o); i++)
{
CXBdata(tx.buf.o, i) =