forked from wb2osz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demod.c
1056 lines (820 loc) · 34.3 KB
/
demod.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
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 John Langner, WB2OSZ
//
// 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, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Module: demod.c
*
* Purpose: Common entry point for multiple types of demodulators.
*
* Input: Audio samples from either a file or the "sound card."
*
* Outputs: Calls hdlc_rec_bit() for each bit demodulated.
*
*---------------------------------------------------------------*/
#include "direwolf.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "audio.h"
#include "demod.h"
#include "tune.h"
#include "fsk_demod_state.h"
#include "fsk_gen_filter.h"
#include "fsk_fast_filter.h"
#include "hdlc_rec.h"
#include "textcolor.h"
#include "demod_9600.h"
#include "demod_afsk.h"
#include "demod_psk.h"
// Properties of the radio channels.
static struct audio_s *save_audio_config_p;
// TODO: temp experiment.
static int upsample = 2; // temp experiment.
static int zerostuff = 1; // temp experiment.
// Current state of all the decoders.
static struct demodulator_state_s demodulator_state[MAX_CHANS][MAX_SUBCHANS];
static int sample_sum[MAX_CHANS][MAX_SUBCHANS];
static int sample_count[MAX_CHANS][MAX_SUBCHANS];
/*------------------------------------------------------------------
*
* Name: demod_init
*
* Purpose: Initialize the demodulator(s) used for reception.
*
* Inputs: pa - Pointer to audio_s structure with
* various parameters for the modem(s).
*
* Returns: 0 for success, -1 for failure.
*
*
* Bugs: This doesn't do much error checking so don't give it
* anything crazy.
*
*----------------------------------------------------------------*/
int demod_init (struct audio_s *pa)
{
//int j;
int chan; /* Loop index over number of radio channels. */
char profile;
/*
* Save audio configuration for later use.
*/
save_audio_config_p = pa;
for (chan = 0; chan < MAX_CHANS; chan++) {
if (save_audio_config_p->achan[chan].valid) {
char *p;
char just_letters[16];
int num_letters;
int have_plus;
/*
* These are derived from config file parameters.
*
* num_subchan is number of demodulators.
* This can be increased by:
* Multiple frequencies.
* Multiple letters (not sure if I will continue this).
* New interleaved decoders.
*
* num_slicers is set to max by the "+" option.
*/
save_audio_config_p->achan[chan].num_subchan = 1;
save_audio_config_p->achan[chan].num_slicers = 1;
switch (save_audio_config_p->achan[chan].modem_type) {
case MODEM_OFF:
break;
case MODEM_AFSK:
/*
* Tear apart the profile and put it back together in a normalized form:
* - At least one letter, supply suitable default if necessary.
* - Upper case only.
* - Any plus will be at the end.
*/
num_letters = 0;
just_letters[num_letters] = '\0';
have_plus = 0;
for (p = save_audio_config_p->achan[chan].profiles; *p != '\0'; p++) {
if (islower(*p)) {
just_letters[num_letters] = toupper(*p);
num_letters++;
just_letters[num_letters] = '\0';
}
else if (isupper(*p)) {
just_letters[num_letters] = *p;
num_letters++;
just_letters[num_letters] = '\0';
}
else if (*p == '+') {
have_plus = 1;
if (p[1] != '\0') {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Channel %d: + option must appear at end of demodulator types \"%s\" \n",
chan, save_audio_config_p->achan[chan].profiles);
}
}
else if (*p == '-') {
have_plus = -1;
if (p[1] != '\0') {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Channel %d: - option must appear at end of demodulator types \"%s\" \n",
chan, save_audio_config_p->achan[chan].profiles);
}
} else {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Channel %d: Demodulator types \"%s\" can contain only letters and + - characters.\n",
chan, save_audio_config_p->achan[chan].profiles);
}
}
assert (num_letters == (int)(strlen(just_letters)));
/*
* Pick a good default demodulator if none specified.
*/
if (num_letters == 0) {
if (save_audio_config_p->achan[chan].baud < 600) {
/* This has been optimized for 300 baud. */
strlcpy (just_letters, "D", sizeof(just_letters));
}
else {
#if __arm__
/* We probably don't have a lot of CPU power available. */
/* Previously we would use F if possible otherwise fall back to A. */
/* In version 1.2, new default is E+ /3. */
strlcpy (just_letters, "E", sizeof(just_letters)); // version 1.2 now E.
if (have_plus != -1) have_plus = 1; // Add as default for version 1.2
// If not explicitly turned off.
if (save_audio_config_p->achan[chan].decimate == 0) {
if (save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec > 40000) {
save_audio_config_p->achan[chan].decimate = 3;
}
}
#else
strlcpy (just_letters, "E", sizeof(just_letters)); // version 1.2 changed C to E.
if (have_plus != -1) have_plus = 1; // Add as default for version 1.2
// If not explicitly turned off.
#endif
}
num_letters = 1;
}
assert (num_letters == (int)(strlen(just_letters)));
/*
* Put it back together again.
*/
/* At this point, have_plus can have 3 values: */
/* 1 = turned on, either explicitly or by applied default */
/* -1 = explicitly turned off. change to 0 here so it is false. */
/* 0 = off by default. */
if (have_plus == -1) have_plus = 0;
strlcpy (save_audio_config_p->achan[chan].profiles, just_letters, sizeof(save_audio_config_p->achan[chan].profiles));
assert (strlen(save_audio_config_p->achan[chan].profiles) >= 1);
if (have_plus) {
strlcat (save_audio_config_p->achan[chan].profiles, "+", sizeof(save_audio_config_p->achan[chan].profiles));
}
/* These can be increased later for the multi-frequency case. */
save_audio_config_p->achan[chan].num_subchan = num_letters;
save_audio_config_p->achan[chan].num_slicers = 1;
/*
* Some error checking - Can use only one of these:
*
* - Multiple letters.
* - New + multi-slicer.
* - Multiple frequencies.
*/
if (have_plus && save_audio_config_p->achan[chan].num_freq > 1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Channel %d: Demodulator + option can't be combined with multiple frequencies.\n", chan);
save_audio_config_p->achan[chan].num_subchan = 1; // Will be set higher later.
save_audio_config_p->achan[chan].num_freq = 1;
}
if (num_letters > 1 && save_audio_config_p->achan[chan].num_freq > 1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Channel %d: Multiple demodulator types can't be combined with multiple frequencies.\n", chan);
save_audio_config_p->achan[chan].profiles[1] = '\0';
num_letters = 1;
}
if (save_audio_config_p->achan[chan].decimate == 0) {
save_audio_config_p->achan[chan].decimate = 1;
if (strchr (just_letters, 'D') != NULL && save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec > 40000) {
save_audio_config_p->achan[chan].decimate = 3;
}
}
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Channel %d: %d baud, AFSK %d & %d Hz, %s, %d sample rate",
chan, save_audio_config_p->achan[chan].baud,
save_audio_config_p->achan[chan].mark_freq, save_audio_config_p->achan[chan].space_freq,
save_audio_config_p->achan[chan].profiles,
save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec);
if (save_audio_config_p->achan[chan].decimate != 1)
dw_printf (" / %d", save_audio_config_p->achan[chan].decimate);
if (save_audio_config_p->achan[chan].dtmf_decode != DTMF_DECODE_OFF)
dw_printf (", DTMF decoder enabled");
dw_printf (".\n");
/*
* Initialize the demodulator(s).
*
* We have 3 cases to consider.
*/
// TODO1.3: revisit this logic now that it is less restrictive.
if (num_letters > 1) {
int d;
/*
* Multiple letters, usually for 1200 baud.
* Each one corresponds to a demodulator and subchannel.
*
* An interesting experiment but probably not too useful.
* Can't have multiple frequency pairs.
* In version 1.3 this can be combined with the + option.
*/
save_audio_config_p->achan[chan].num_subchan = num_letters;
/*
* Quick hack with special case for another experiment.
* Do this in a more general way if it turns out to be useful.
*/
save_audio_config_p->achan[chan].interleave = 1;
if (strcasecmp(save_audio_config_p->achan[chan].profiles, "EE") == 0) {
save_audio_config_p->achan[chan].interleave = 2;
save_audio_config_p->achan[chan].decimate = 1;
}
else if (strcasecmp(save_audio_config_p->achan[chan].profiles, "EEE") == 0) {
save_audio_config_p->achan[chan].interleave = 3;
save_audio_config_p->achan[chan].decimate = 1;
}
else if (strcasecmp(save_audio_config_p->achan[chan].profiles, "EEEE") == 0) {
save_audio_config_p->achan[chan].interleave = 4;
save_audio_config_p->achan[chan].decimate = 1;
}
else if (strcasecmp(save_audio_config_p->achan[chan].profiles, "EEEEE") == 0) {
save_audio_config_p->achan[chan].interleave = 5;
save_audio_config_p->achan[chan].decimate = 1;
}
else if (strcasecmp(save_audio_config_p->achan[chan].profiles, "GG") == 0) {
save_audio_config_p->achan[chan].interleave = 2;
save_audio_config_p->achan[chan].decimate = 1;
}
else if (strcasecmp(save_audio_config_p->achan[chan].profiles, "GGG") == 0) {
save_audio_config_p->achan[chan].interleave = 3;
save_audio_config_p->achan[chan].decimate = 1;
}
else if (strcasecmp(save_audio_config_p->achan[chan].profiles, "GGG+") == 0) {
save_audio_config_p->achan[chan].interleave = 3;
save_audio_config_p->achan[chan].decimate = 1;
}
else if (strcasecmp(save_audio_config_p->achan[chan].profiles, "GGGG") == 0) {
save_audio_config_p->achan[chan].interleave = 4;
save_audio_config_p->achan[chan].decimate = 1;
}
else if (strcasecmp(save_audio_config_p->achan[chan].profiles, "GGGGG") == 0) {
save_audio_config_p->achan[chan].interleave = 5;
save_audio_config_p->achan[chan].decimate = 1;
}
if (save_audio_config_p->achan[chan].num_subchan != num_letters) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("INTERNAL ERROR, %s:%d, chan=%d, num_subchan(%d) != strlen(\"%s\")\n",
__FILE__, __LINE__, chan, save_audio_config_p->achan[chan].num_subchan, save_audio_config_p->achan[chan].profiles);
}
if (save_audio_config_p->achan[chan].num_freq != 1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("INTERNAL ERROR, %s:%d, chan=%d, num_freq(%d) != 1\n",
__FILE__, __LINE__, chan, save_audio_config_p->achan[chan].num_freq);
}
for (d = 0; d < save_audio_config_p->achan[chan].num_subchan; d++) {
int mark, space;
assert (d >= 0 && d < MAX_SUBCHANS);
struct demodulator_state_s *D;
D = &demodulator_state[chan][d];
profile = save_audio_config_p->achan[chan].profiles[d];
mark = save_audio_config_p->achan[chan].mark_freq;
space = save_audio_config_p->achan[chan].space_freq;
if (save_audio_config_p->achan[chan].num_subchan != 1) {
text_color_set(DW_COLOR_DEBUG);
dw_printf (" %d.%d: %c %d & %d\n", chan, d, profile, mark, space);
}
demod_afsk_init (save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec / (save_audio_config_p->achan[chan].decimate * save_audio_config_p->achan[chan].interleave),
save_audio_config_p->achan[chan].baud,
mark,
space,
profile,
D);
if (have_plus) {
/* I'm not happy about putting this hack here. */
/* should pass in as a parameter rather than adding on later. */
save_audio_config_p->achan[chan].num_slicers = MAX_SLICERS;
D->num_slicers = MAX_SLICERS;
}
/* For signal level reporting, we want a longer term view. */
// TODO: Should probably move this into the init functions.
D->quick_attack = D->agc_fast_attack * 0.2f;
D->sluggish_decay = D->agc_slow_decay * 0.2f;
}
}
else if (have_plus) {
/*
* PLUS - which (formerly) implies we have only one letter and one frequency pair.
*
* One demodulator feeds multiple slicers, each a subchannel.
*/
if (num_letters != 1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("INTERNAL ERROR, %s:%d, chan=%d, strlen(\"%s\") != 1\n",
__FILE__, __LINE__, chan, just_letters);
}
if (save_audio_config_p->achan[chan].num_freq != 1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("INTERNAL ERROR, %s:%d, chan=%d, num_freq(%d) != 1\n",
__FILE__, __LINE__, chan, save_audio_config_p->achan[chan].num_freq);
}
if (save_audio_config_p->achan[chan].num_freq != save_audio_config_p->achan[chan].num_subchan) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("INTERNAL ERROR, %s:%d, chan=%d, num_freq(%d) != num_subchan(%d)\n",
__FILE__, __LINE__, chan, save_audio_config_p->achan[chan].num_freq, save_audio_config_p->achan[chan].num_subchan);
}
struct demodulator_state_s *D;
D = &demodulator_state[chan][0];
/* I'm not happy about putting this hack here. */
/* This belongs in demod_afsk_init but it doesn't have access to the audio config. */
save_audio_config_p->achan[chan].num_slicers = MAX_SLICERS;
demod_afsk_init (save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec / save_audio_config_p->achan[chan].decimate,
save_audio_config_p->achan[chan].baud,
save_audio_config_p->achan[chan].mark_freq,
save_audio_config_p->achan[chan].space_freq,
save_audio_config_p->achan[chan].profiles[0],
D);
if (have_plus) {
/* I'm not happy about putting this hack here. */
/* should pass in as a parameter rather than adding on later. */
save_audio_config_p->achan[chan].num_slicers = MAX_SLICERS;
D->num_slicers = MAX_SLICERS;
}
/* For signal level reporting, we want a longer term view. */
D->quick_attack = D->agc_fast_attack * 0.2f;
D->sluggish_decay = D->agc_slow_decay * 0.2f;
}
else {
int d;
/*
* One letter.
* Can be combined with multiple frequencies.
*/
if (num_letters != 1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("INTERNAL ERROR, %s:%d, chan=%d, strlen(\"%s\") != 1\n",
__FILE__, __LINE__, chan, save_audio_config_p->achan[chan].profiles);
}
save_audio_config_p->achan[chan].num_subchan = save_audio_config_p->achan[chan].num_freq;
for (d = 0; d < save_audio_config_p->achan[chan].num_freq; d++) {
int mark, space, k;
assert (d >= 0 && d < MAX_SUBCHANS);
struct demodulator_state_s *D;
D = &demodulator_state[chan][d];
profile = save_audio_config_p->achan[chan].profiles[0];
k = d * save_audio_config_p->achan[chan].offset - ((save_audio_config_p->achan[chan].num_freq - 1) * save_audio_config_p->achan[chan].offset) / 2;
mark = save_audio_config_p->achan[chan].mark_freq + k;
space = save_audio_config_p->achan[chan].space_freq + k;
if (save_audio_config_p->achan[chan].num_freq != 1) {
text_color_set(DW_COLOR_DEBUG);
dw_printf (" %d.%d: %c %d & %d\n", chan, d, profile, mark, space);
}
demod_afsk_init (save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec / save_audio_config_p->achan[chan].decimate,
save_audio_config_p->achan[chan].baud,
mark, space,
profile,
D);
if (have_plus) {
/* I'm not happy about putting this hack here. */
/* should pass in as a parameter rather than adding on later. */
save_audio_config_p->achan[chan].num_slicers = MAX_SLICERS;
D->num_slicers = MAX_SLICERS;
}
/* For signal level reporting, we want a longer term view. */
D->quick_attack = D->agc_fast_attack * 0.2f;
D->sluggish_decay = D->agc_slow_decay * 0.2f;
} /* for each freq pair */
}
break;
case MODEM_QPSK: // New for 1.4
// TODO: See how much CPU this takes on ARM and decide if we should have different defaults.
if (strlen(save_audio_config_p->achan[chan].profiles) == 0) {
//#if __arm__
// strlcpy (save_audio_config_p->achan[chan].profiles, "R", sizeof(save_audio_config_p->achan[chan].profiles));
//#else
strlcpy (save_audio_config_p->achan[chan].profiles, "PQRS", sizeof(save_audio_config_p->achan[chan].profiles));
//#endif
}
save_audio_config_p->achan[chan].num_subchan = strlen(save_audio_config_p->achan[chan].profiles);
save_audio_config_p->achan[chan].decimate = 1; // think about this later.
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Channel %d: %d bps, QPSK, %s, %d sample rate",
chan, save_audio_config_p->achan[chan].baud,
save_audio_config_p->achan[chan].profiles,
save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec);
if (save_audio_config_p->achan[chan].decimate != 1)
dw_printf (" / %d", save_audio_config_p->achan[chan].decimate);
if (save_audio_config_p->achan[chan].dtmf_decode != DTMF_DECODE_OFF)
dw_printf (", DTMF decoder enabled");
dw_printf (".\n");
int d;
for (d = 0; d < save_audio_config_p->achan[chan].num_subchan; d++) {
assert (d >= 0 && d < MAX_SUBCHANS);
struct demodulator_state_s *D;
D = &demodulator_state[chan][d];
profile = save_audio_config_p->achan[chan].profiles[d];
//text_color_set(DW_COLOR_DEBUG);
//dw_printf ("About to call demod_psk_init for Q-PSK case, modem_type=%d, profile='%c'\n",
// save_audio_config_p->achan[chan].modem_type, profile);
demod_psk_init (save_audio_config_p->achan[chan].modem_type,
save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec / save_audio_config_p->achan[chan].decimate,
save_audio_config_p->achan[chan].baud,
profile,
D);
//text_color_set(DW_COLOR_DEBUG);
//dw_printf ("Returned from demod_psk_init\n");
/* For signal level reporting, we want a longer term view. */
/* Guesses based on 9600. Maybe revisit someday. */
D->quick_attack = 0.080 * 0.2;
D->sluggish_decay = 0.00012 * 0.2;
}
break;
case MODEM_8PSK: // New for 1.4
// TODO: See how much CPU this takes on ARM and decide if we should have different defaults.
if (strlen(save_audio_config_p->achan[chan].profiles) == 0) {
//#if __arm__
// strlcpy (save_audio_config_p->achan[chan].profiles, "V", sizeof(save_audio_config_p->achan[chan].profiles));
//#else
strlcpy (save_audio_config_p->achan[chan].profiles, "TUVW", sizeof(save_audio_config_p->achan[chan].profiles));
//#endif
}
save_audio_config_p->achan[chan].num_subchan = strlen(save_audio_config_p->achan[chan].profiles);
save_audio_config_p->achan[chan].decimate = 1; // think about this later
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Channel %d: %d bps, 8PSK, %s, %d sample rate",
chan, save_audio_config_p->achan[chan].baud,
save_audio_config_p->achan[chan].profiles,
save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec);
if (save_audio_config_p->achan[chan].decimate != 1)
dw_printf (" / %d", save_audio_config_p->achan[chan].decimate);
if (save_audio_config_p->achan[chan].dtmf_decode != DTMF_DECODE_OFF)
dw_printf (", DTMF decoder enabled");
dw_printf (".\n");
//int d;
for (d = 0; d < save_audio_config_p->achan[chan].num_subchan; d++) {
assert (d >= 0 && d < MAX_SUBCHANS);
struct demodulator_state_s *D;
D = &demodulator_state[chan][d];
profile = save_audio_config_p->achan[chan].profiles[d];
//text_color_set(DW_COLOR_DEBUG);
//dw_printf ("About to call demod_psk_init for 8-PSK case, modem_type=%d, profile='%c'\n",
// save_audio_config_p->achan[chan].modem_type, profile);
demod_psk_init (save_audio_config_p->achan[chan].modem_type,
save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec / save_audio_config_p->achan[chan].decimate,
save_audio_config_p->achan[chan].baud,
profile,
D);
//text_color_set(DW_COLOR_DEBUG);
//dw_printf ("Returned from demod_psk_init\n");
/* For signal level reporting, we want a longer term view. */
/* Guesses based on 9600. Maybe revisit someday. */
D->quick_attack = 0.080 * 0.2;
D->sluggish_decay = 0.00012 * 0.2;
}
break;
//TODO: how about MODEM_OFF case?
case MODEM_BASEBAND:
case MODEM_SCRAMBLE:
default: /* Not AFSK */
{
if (strcmp(save_audio_config_p->achan[chan].profiles, "") == 0) {
/* Apply default if not set earlier. */
/* Not sure if it should be on for ARM too. */
/* Need to take a look at CPU usage and performance difference. */
/* Version 1.5: Remove special case for ARM. */
/* We want higher performance to be the default. */
/* "MODEM 9600 -" can be used on very slow CPU if necessary. */
//#ifndef __arm__
strlcpy (save_audio_config_p->achan[chan].profiles, "+", sizeof(save_audio_config_p->achan[chan].profiles));
//#endif
}
#ifdef TUNE_UPSAMPLE
upsample = TUNE_UPSAMPLE;
#endif
#ifdef TUNE_ZEROSTUFF
zerostuff = TUNE_ZEROSTUFF;
#endif
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Channel %d: %d baud, K9NG/G3RUH, %s, %d sample rate x %d",
chan, save_audio_config_p->achan[chan].baud,
save_audio_config_p->achan[chan].profiles,
save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec, upsample);
if (save_audio_config_p->achan[chan].dtmf_decode != DTMF_DECODE_OFF)
dw_printf (", DTMF decoder enabled");
dw_printf (".\n");
struct demodulator_state_s *D;
D = &demodulator_state[chan][0]; // first subchannel
save_audio_config_p->achan[chan].num_subchan = 1;
save_audio_config_p->achan[chan].num_slicers = 1;
if (strchr(save_audio_config_p->achan[chan].profiles, '+') != NULL) {
/* I'm not happy about putting this hack here. */
/* This belongs in demod_9600_init but it doesn't have access to the audio config. */
save_audio_config_p->achan[chan].num_slicers = MAX_SLICERS;
}
/* We need a minimum number of audio samples per bit time for good performance. */
/* Easier to check here because demod_9600_init might have an adjusted sample rate. */
float ratio = (float)(save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec)
/ (float)(save_audio_config_p->achan[chan].baud);
text_color_set(DW_COLOR_INFO);
dw_printf ("The ratio of audio samples per sec (%d) to data rate in baud (%d) is %.1f\n",
save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec,
save_audio_config_p->achan[chan].baud,
(double)ratio);
if (ratio < 3) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("There is little hope of success with such a low ratio. Use a higher sample rate.\n");
}
else if (ratio < 5) {
dw_printf ("This is on the low side for best performance. Can you use a higher sample rate?\n");
}
else if (ratio < 6) {
dw_printf ("Increasing the sample rate should improve decoder performance.\n");
}
else if (ratio > 15) {
dw_printf ("Sample rate is more than adequate. You might lower it if CPU load is a concern.\n");
}
else {
dw_printf ("This is a suitable ratio for good performance.\n");
}
demod_9600_init (upsample * save_audio_config_p->adev[ACHAN2ADEV(chan)].samples_per_sec, save_audio_config_p->achan[chan].baud, D);
if (strchr(save_audio_config_p->achan[chan].profiles, '+') != NULL) {
/* I'm not happy about putting this hack here. */
/* should pass in as a parameter rather than adding on later. */
save_audio_config_p->achan[chan].num_slicers = MAX_SLICERS;
D->num_slicers = MAX_SLICERS;
}
/* For signal level reporting, we want a longer term view. */
D->quick_attack = D->agc_fast_attack * 0.2f;
D->sluggish_decay = D->agc_slow_decay * 0.2f;
}
break;
} /* switch on modulation type. */
} /* if channel number is valid */
} /* for chan ... */
return (0);
} /* end demod_init */
/*------------------------------------------------------------------
*
* Name: demod_get_sample
*
* Purpose: Get one audio sample fromt the specified sound input source.
*
* Inputs: a - Index for audio device. 0 = first.
*
* Returns: -32768 .. 32767 for a valid audio sample.
* 256*256 for end of file or other error.
*
* Global In: save_audio_config_p->adev[ACHAN2ADEV(chan)].bits_per_sample - So we know whether to
* read 1 or 2 bytes from audio stream.
*
* Description: Grab 1 or two btyes depending on data source.
*
* When processing stereo, the caller will call this
* at twice the normal rate to obtain alternating left
* and right samples.
*
*----------------------------------------------------------------*/
#define FSK_READ_ERR (256*256)
__attribute__((hot))
int demod_get_sample (int a)
{
int x1, x2;
signed short sam; /* short to force sign extention. */
assert (save_audio_config_p->adev[a].bits_per_sample == 8 || save_audio_config_p->adev[a].bits_per_sample == 16);
if (save_audio_config_p->adev[a].bits_per_sample == 8) {
x1 = audio_get(a);
if (x1 < 0) return(FSK_READ_ERR);
assert (x1 >= 0 && x1 <= 255);
/* Scale 0..255 into -32k..+32k */
sam = (x1 - 128) * 256;
}
else {
x1 = audio_get(a); /* lower byte first */
if (x1 < 0) return(FSK_READ_ERR);
x2 = audio_get(a);
if (x2 < 0) return(FSK_READ_ERR);
assert (x1 >= 0 && x1 <= 255);
assert (x2 >= 0 && x2 <= 255);
sam = ( x2 << 8 ) | x1;
}
return (sam);
}
/*-------------------------------------------------------------------
*
* Name: demod_process_sample
*
* Purpose: (1) Demodulate the AFSK signal.
* (2) Recover clock and data.
*
* Inputs: chan - Audio channel. 0 for left, 1 for right.
* subchan - modem of the channel.
* sam - One sample of audio.
* Should be in range of -32768 .. 32767.
*
* Returns: None
*
* Descripion: We start off with two bandpass filters tuned to
* the given frequencies. In the case of VHF packet
* radio, this would be 1200 and 2200 Hz.
*
* The bandpass filter amplitudes are compared to
* obtain the demodulated signal.
*
* We also have a digital phase locked loop (PLL)
* to recover the clock and pick out data bits at
* the proper rate.
*
* For each recovered data bit, we call:
*
* hdlc_rec (channel, demodulated_bit);
*
* to decode HDLC frames from the stream of bits.
*
* Future: This could be generalized by passing in the name
* of the function to be called for each bit recovered
* from the demodulator. For now, it's simply hard-coded.
*
*--------------------------------------------------------------------*/
__attribute__((hot))
void demod_process_sample (int chan, int subchan, int sam)
{
float fsam;
int k;
struct demodulator_state_s *D;
assert (chan >= 0 && chan < MAX_CHANS);
assert (subchan >= 0 && subchan < MAX_SUBCHANS);
D = &demodulator_state[chan][subchan];
/* Scale to nice number, actually -2.0 to +2.0 for extra headroom */
fsam = sam / 16384.0f;
/*
* Accumulate measure of the input signal level.
*/
/*
* Version 1.2: Try new approach to capturing the amplitude.
* This is same as the later AGC without the normalization step.
* We want decay to be substantially slower to get a longer
* range idea of the received audio.
*/
if (fsam >= D->alevel_rec_peak) {
D->alevel_rec_peak = fsam * D->quick_attack + D->alevel_rec_peak * (1.0f - D->quick_attack);
}
else {
D->alevel_rec_peak = fsam * D->sluggish_decay + D->alevel_rec_peak * (1.0f - D->sluggish_decay);
}
if (fsam <= D->alevel_rec_valley) {
D->alevel_rec_valley = fsam * D->quick_attack + D->alevel_rec_valley * (1.0f - D->quick_attack);
}
else {
D->alevel_rec_valley = fsam * D->sluggish_decay + D->alevel_rec_valley * (1.0f - D->sluggish_decay);
}
/*
* Select decoder based on modulation type.
*/
switch (save_audio_config_p->achan[chan].modem_type) {
case MODEM_OFF:
// Might have channel only listening to DTMF for APRStt gateway.
// Don't waste CPU time running a demodulator here.
break;
case MODEM_AFSK:
if (save_audio_config_p->achan[chan].decimate > 1) {
sample_sum[chan][subchan] += sam;
sample_count[chan][subchan]++;
if (sample_count[chan][subchan] >= save_audio_config_p->achan[chan].decimate) {
demod_afsk_process_sample (chan, subchan, sample_sum[chan][subchan] / save_audio_config_p->achan[chan].decimate, D);
sample_sum[chan][subchan] = 0;
sample_count[chan][subchan] = 0;
}
}
else {
demod_afsk_process_sample (chan, subchan, sam, D);
}
break;
case MODEM_QPSK:
case MODEM_8PSK:
if (save_audio_config_p->achan[chan].decimate > 1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Invalid combination of options. Exiting.\n");
// Would probably work but haven't thought about it or tested yet.
exit (1);
}
else {
demod_psk_process_sample (chan, subchan, sam, D);
}
break;
case MODEM_BASEBAND:
case MODEM_SCRAMBLE:
default:
if (zerostuff) {
/* Literature says this is better if followed */
/* by appropriate low pass filter. */
/* So far, both are same in tests with different */
/* optimal low pass filter parameters. */
for (k=1; k<upsample; k++) {
demod_9600_process_sample (chan, 0, D);
}
demod_9600_process_sample (chan, sam * upsample, D);
}
else {
/* Linear interpolation. */
static int prev_sam;
switch (upsample) {
case 1:
demod_9600_process_sample (chan, sam, D);
break;
case 2:
demod_9600_process_sample (chan, (prev_sam + sam) / 2, D);
demod_9600_process_sample (chan, sam, D);
break;
case 3:
demod_9600_process_sample (chan, (2 * prev_sam + sam) / 3, D);
demod_9600_process_sample (chan, (prev_sam + 2 * sam) / 3, D);
demod_9600_process_sample (chan, sam, D);
break;
case 4:
demod_9600_process_sample (chan, (3 * prev_sam + sam) / 4, D);
demod_9600_process_sample (chan, (prev_sam + sam) / 2, D);
demod_9600_process_sample (chan, (prev_sam + 3 * sam) / 4, D);
demod_9600_process_sample (chan, sam, D);
break;
default:
assert (0);
break;
}
prev_sam = sam;
}
break;
} /* switch modem_type */
return;
} /* end demod_process_sample */
/* Doesn't seem right. Need to revisit this. */
/* Resulting scale is 0 to almost 100. */
/* Cranking up the input level produces no more than 97 or 98. */
/* We currently produce a message when this goes over 90. */
alevel_t demod_get_audio_level (int chan, int subchan)
{
struct demodulator_state_s *D;
alevel_t alevel;