forked from pd0mz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demod_afsk.c
1143 lines (880 loc) · 30.8 KB
/
demod_afsk.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 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/>.
//
// #define DEBUG1 1 /* display debugging info */
// #define DEBUG3 1 /* print carrier detect changes. */
// #define DEBUG4 1 /* capture AFSK demodulator output to log files */
// #define DEBUG5 1 /* capture 9600 output to log files */
/*------------------------------------------------------------------
*
* Module: demod_afsk.c
*
* Purpose: Demodulator for Audio Frequency Shift Keying (AFSK).
*
* Input: Audio samples from either a file or the "sound card."
*
* Outputs: Calls hdlc_rec_bit() for each bit demodulated.
*
*---------------------------------------------------------------*/
#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 "direwolf.h"
#include "audio.h"
#include "tune.h"
#include "fsk_demod_state.h"
#include "fsk_gen_filter.h"
#include "hdlc_rec.h"
#include "textcolor.h"
#include "demod_afsk.h"
#include "dsp.h"
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
/* Quick approximation to sqrt(x*x+y*y) */
/* No benefit for regular PC. */
/* Should help with microcomputer platform. */
__attribute__((hot))
static inline float z (float x, float y)
{
x = fabsf(x);
y = fabsf(y);
if (x > y) {
return (x * .941246f + y * .41f);
}
else {
return (y * .941246f + x * .41f);
}
}
/* Add sample to buffer and shift the rest down. */
__attribute__((hot))
static inline void push_sample (float val, float *buff, int size)
{
memmove(buff+1,buff,(size-1)*sizeof(float));
buff[0] = val;
}
/* FIR filter kernel. */
__attribute__((hot))
static inline float convolve (const float *__restrict__ data, const float *__restrict__ filter, int filter_size)
{
float sum = 0.0f;
int j;
#if 0
// As suggested here, http://locklessinc.com/articles/vectorize/
// Unfortunately, older compilers don't recognize it.
// Get more information by using -ftree-vectorizer-verbose=5
float *d = __builtin_assume_aligned(data, 16);
float *f = __builtin_assume_aligned(filter, 16);
for (j=0; j<filter_size; j++) {
sum += f[j] * d[j];
}
#else
for (j=0; j<filter_size; j++) {
sum += filter[j] * data[j];
}
#endif
return (sum);
}
/* Automatic gain control. */
/* Result should settle down to 1 unit peak to peak. i.e. -0.5 to +0.5 */
__attribute__((hot))
static inline float agc (float in, float fast_attack, float slow_decay, float *ppeak, float *pvalley)
{
if (in >= *ppeak) {
*ppeak = in * fast_attack + *ppeak * (1.0f - fast_attack);
}
else {
*ppeak = in * slow_decay + *ppeak * (1.0f - slow_decay);
}
if (in <= *pvalley) {
*pvalley = in * fast_attack + *pvalley * (1.0f - fast_attack);
}
else {
*pvalley = in * slow_decay + *pvalley * (1.0f - slow_decay);
}
if (*ppeak > *pvalley) {
return ((in - 0.5f * (*ppeak + *pvalley)) / (*ppeak - *pvalley));
}
return (0.0f);
}
/*
* for multi-slicer experiment.
*/
#define MIN_G 0.5f
#define MAX_G 4.0f
/* TODO: static */ float space_gain[MAX_SUBCHANS];
/*------------------------------------------------------------------
*
* Name: demod_afsk_init
*
* Purpose: Initialization for an AFSK demodulator.
* Select appropriate parameters and set up filters.
*
* Inputs: samples_per_sec
* baud
* mark_freq
* space_freq
*
* D - Pointer to demodulator state for given channel.
*
* Outputs: D->ms_filter_size
* D->m_sin_table[]
* D->m_cos_table[]
* D->s_sin_table[]
* D->s_cos_table[]
*
* Returns: None.
*
* Bugs: This doesn't do much error checking so don't give it
* anything crazy.
*
*----------------------------------------------------------------*/
void demod_afsk_init (int samples_per_sec, int baud, int mark_freq,
int space_freq, char profile, struct demodulator_state_s *D)
{
int j;
memset (D, 0, sizeof(struct demodulator_state_s));
#if DEBUG1
dw_printf ("demod_afsk_init (rate=%d, baud=%d, mark=%d, space=%d, profile=%c\n",
samples_per_sec, baud, mark_freq, space_freq, profile);
#endif
#ifdef TUNE_PROFILE
profile = TUNE_PROFILE;
#endif
if (profile == 'F') {
if (baud != DEFAULT_BAUD ||
mark_freq != DEFAULT_MARK_FREQ ||
space_freq!= DEFAULT_SPACE_FREQ ||
samples_per_sec != DEFAULT_SAMPLES_PER_SEC) {
text_color_set(DW_COLOR_INFO);
dw_printf ("Note: Decoder 'F' works only for %d baud, %d/%d tones, %d samples/sec.\n",
DEFAULT_BAUD, DEFAULT_MARK_FREQ, DEFAULT_SPACE_FREQ, DEFAULT_SAMPLES_PER_SEC);
dw_printf ("Using Decoder 'A' instead.\n");
profile = 'A';
}
}
D->profile = profile; // so we know whether to take fast path later.
switch (profile) {
case 'A':
case 'F':
/* Original. 52 taps, truncated bandpass, IIR lowpass */
/* 'F' is the fast version for low end processors. */
/* It is a special case that works only for a particular */
/* baud rate, tone pair, and sampling rate. */
D->use_prefilter = 0;
D->ms_filter_len_bits = 1.415; /* 52 @ 44100, 1200 */
D->ms_window = BP_WINDOW_TRUNCATED;
//D->bp_window = BP_WINDOW_TRUNCATED;
D->lpf_use_fir = 0;
D->lpf_iir = 0.195;
D->agc_fast_attack = 0.250;
D->agc_slow_decay = 0.00012;
D->hysteresis = 0.005;
D->pll_locked_inertia = 0.700;
D->pll_searching_inertia = 0.580;
break;
case 'B':
/* Original bandpass. Use FIR lowpass instead. */
D->use_prefilter = 0;
D->ms_filter_len_bits = 1.415; /* 52 @ 44100, 1200 */
D->ms_window = BP_WINDOW_TRUNCATED;
//D->bp_window = BP_WINDOW_TRUNCATED;
D->lpf_use_fir = 1;
D->lpf_baud = 1.09;
D->lp_filter_len_bits = D->ms_filter_len_bits;
D->lp_window = BP_WINDOW_TRUNCATED;
D->agc_fast_attack = 0.370;
D->agc_slow_decay = 0.00014;
D->hysteresis = 0.003;
D->pll_locked_inertia = 0.620;
D->pll_searching_inertia = 0.350;
break;
case 'C':
/* Cosine window, 76 taps for bandpass, FIR lowpass. */
D->use_prefilter = 0;
D->ms_filter_len_bits = 2.068; /* 76 @ 44100, 1200 */
D->ms_window = BP_WINDOW_COSINE;
//D->bp_window = BP_WINDOW_COSINE;
D->lpf_use_fir = 1;
D->lpf_baud = 1.09;
D->lp_filter_len_bits = D->ms_filter_len_bits;
D->lp_window = BP_WINDOW_TRUNCATED;
D->agc_fast_attack = 0.495;
D->agc_slow_decay = 0.00022;
D->hysteresis = 0.005;
D->pll_locked_inertia = 0.620;
D->pll_searching_inertia = 0.350;
break;
case 'D':
/* Prefilter, Cosine window, FIR lowpass. Tweeked for 300 baud. */
D->use_prefilter = 1; /* first, a bandpass filter. */
D->prefilter_baud = 0.87;
D->pre_filter_len_bits = 1.857;
D->pre_window = BP_WINDOW_COSINE;
D->ms_filter_len_bits = 1.857; /* 91 @ 44100/3, 300 */
D->ms_window = BP_WINDOW_COSINE;
//D->bp_window = BP_WINDOW_COSINE;
D->lpf_use_fir = 1;
D->lpf_baud = 1.10;
D->lp_filter_len_bits = D->ms_filter_len_bits;
D->lp_window = BP_WINDOW_TRUNCATED;
D->agc_fast_attack = 0.495;
D->agc_slow_decay = 0.00022;
D->hysteresis = 0.027;
D->pll_locked_inertia = 0.620;
D->pll_searching_inertia = 0.350;
break;
case 'E':
/* 1200 baud - Started out similar to C but add prefilter. */
/* Version 1.2 - EXPERIMENTAL - Needs more fine tuning. */
/* Enhancements: */
/* + Add prefilter. Previously used for 300 baud D, but not 1200. */
/* + Prefilter length now independent of M/S filters. */
/* + Lowpass filter length now independent of M/S filters. */
/* + Allow mixed window types. */
//D->bp_window = BP_WINDOW_COSINE; /* The name says BP but it is used for all of them. */
D->use_prefilter = 1; /* first, a bandpass filter. */
D->prefilter_baud = 0.23;
D->pre_filter_len_bits = 156 * 1200. / 44100.;
D->pre_window = BP_WINDOW_TRUNCATED;
D->ms_filter_len_bits = 74 * 1200. / 44100.;
D->ms_window = BP_WINDOW_COSINE;
D->lpf_use_fir = 1;
D->lpf_baud = 1.18;
D->lp_filter_len_bits = 63 * 1200. / 44100.;
D->lp_window = BP_WINDOW_TRUNCATED;
//D->agc_fast_attack = 0.300;
//D->agc_slow_decay = 0.000185;
D->agc_fast_attack = 0.820;
D->agc_slow_decay = 0.000214;
D->hysteresis = 0.01;
//D->pll_locked_inertia = 0.57;
//D->pll_searching_inertia = 0.33;
D->pll_locked_inertia = 0.74;
D->pll_searching_inertia = 0.50;
break;
default:
text_color_set(DW_COLOR_ERROR);
dw_printf ("Invalid filter profile = %c\n", profile);
exit (1);
}
#ifdef TUNE_PRE_WINDOW
D->pre_window = TUNE_PRE_WINDOW;
#endif
#ifdef TUNE_MS_WINDOW
D->ms_window = TUNE_MS_WINDOW;
#endif
#ifdef TUNE_LP_WINDOW
D->lp_window = TUNE_LP_WINDOW;
#endif
#if defined(TUNE_AGC_FAST) && defined(TUNE_AGC_SLOW)
D->agc_fast_attack = TUNE_AGC_FAST;
D->agc_slow_decay = TUNE_AGC_SLOW;
#endif
#ifdef TUNE_HYST
D->hysteresis = TUNE_HYST;
#endif
#if defined(TUNE_PLL_LOCKED) && defined(TUNE_PLL_SEARCHING)
D->pll_locked_inertia = TUNE_PLL_LOCKED;
D->pll_searching_inertia = TUNE_PLL_SEARCHING;
#endif
#ifdef TUNE_LPF_BAUD
D->lpf_baud = TUNE_LPF_BAUD;
#endif
#ifdef TUNE_PRE_BAUD
D->prefilter_baud = TUNE_PRE_BAUD;
#endif
/*
* Calculate constants used for timing.
* The audio sample rate must be at least a few times the data rate.
*/
D->pll_step_per_sample = (int) round((TICKS_PER_PLL_CYCLE * (double)baud) / ((double)samples_per_sec));
/*
* Convert number of bit times to number of taps.
*/
D->pre_filter_size = (int) round( D->pre_filter_len_bits * (float)samples_per_sec / (float)baud );
D->ms_filter_size = (int) round( D->ms_filter_len_bits * (float)samples_per_sec / (float)baud );
D->lp_filter_size = (int) round( D->lp_filter_len_bits * (float)samples_per_sec / (float)baud );
/* Experiment with other sizes. */
#ifdef TUNE_PRE_FILTER_SIZE
D->pre_filter_size = TUNE_PRE_FILTER_SIZE;
#endif
#ifdef TUNE_MS_FILTER_SIZE
D->ms_filter_size = TUNE_MS_FILTER_SIZE;
#endif
#ifdef TUNE_LP_FILTER_SIZE
D->lp_filter_size = TUNE_LP_FILTER_SIZE;
#endif
//assert (D->pre_filter_size >= 4);
assert (D->ms_filter_size >= 4);
//assert (D->lp_filter_size >= 4);
if (D->pre_filter_size > MAX_FILTER_SIZE)
{
text_color_set (DW_COLOR_ERROR);
dw_printf ("Calculated filter size of %d is too large.\n", D->pre_filter_size);
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
exit (1);
}
if (D->ms_filter_size > MAX_FILTER_SIZE)
{
text_color_set (DW_COLOR_ERROR);
dw_printf ("Calculated filter size of %d is too large.\n", D->ms_filter_size);
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
exit (1);
}
if (D->lp_filter_size > MAX_FILTER_SIZE)
{
text_color_set (DW_COLOR_ERROR);
dw_printf ("Calculated filter size of %d is too large.\n", D->pre_filter_size);
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
exit (1);
}
/*
* Optionally apply a bandpass ("pre") filter to attenuate
* frequencies outside the range of interest.
* This was first used for the "D" profile for 300 baud
* which uses narrow shift. We expect it to have significant
* benefit for a narrow shift.
* In version 1.2, we will also try it with 1200 baud "E" as
* an experiment to see how much it actually helps.
*/
if (D->use_prefilter) {
float f1, f2;
f1 = MIN(mark_freq,space_freq) - D->prefilter_baud * baud;
f2 = MAX(mark_freq,space_freq) + D->prefilter_baud * baud;
#if 0
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Generating prefilter %.0f to %.0f Hz.\n", f1, f2);
#endif
f1 = f1 / (float)samples_per_sec;
f2 = f2 / (float)samples_per_sec;
//gen_bandpass (f1, f2, D->pre_filter, D->pre_filter_size, BP_WINDOW_HAMMING);
//gen_bandpass (f1, f2, D->pre_filter, D->pre_filter_size, BP_WINDOW_BLACKMAN);
//gen_bandpass (f1, f2, D->pre_filter, D->pre_filter_size, BP_WINDOW_COSINE);
//gen_bandpass (f1, f2, D->pre_filter, D->pre_filter_size, D->bp_window);
gen_bandpass (f1, f2, D->pre_filter, D->pre_filter_size, D->pre_window);
}
/*
* Filters for detecting mark and space tones.
*/
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("%s: \n", __FILE__);
dw_printf ("%d baud, %d samples_per_sec\n", baud, samples_per_sec);
dw_printf ("AFSK %d & %d Hz\n", mark_freq, space_freq);
dw_printf ("spll_step_per_sample = %d = 0x%08x\n", D->pll_step_per_sample, D->pll_step_per_sample);
dw_printf ("D->ms_filter_size = %d = 0x%08x\n", D->ms_filter_size, D->ms_filter_size);
dw_printf ("\n");
dw_printf ("Mark\n");
dw_printf (" j shape M sin M cos \n");
#endif
float Gs = 0, Gc = 0;
for (j=0; j<D->ms_filter_size; j++) {
float am;
float center;
float shape = 1; /* Shape is an attempt to smooth out the */
/* abrupt edges in hopes of reducing */
/* overshoot and ringing. */
/* My first thought was to use a cosine shape. */
/* Should investigate Hamming and Blackman */
/* windows mentioned in the literature. */
/* http://en.wikipedia.org/wiki/Window_function */
center = 0.5 * (D->ms_filter_size - 1);
am = ((float)(j - center) / (float)samples_per_sec) * ((float)mark_freq) * (2 * M_PI);
shape = window (D->ms_window, D->ms_filter_size, j);
D->m_sin_table[j] = sin(am) * shape;
D->m_cos_table[j] = cos(am) * shape;
Gs += D->m_sin_table[j] * sin(am);
Gc += D->m_cos_table[j] * cos(am);
#if DEBUG1
dw_printf ("%6d %6.2f %6.2f %6.2f\n", j, shape, D->m_sin_table[j], D->m_cos_table[j]) ;
#endif
}
/* Normalize for unity gain */
#if DEBUG1
dw_printf ("Before normalizing, Gs = %.2f, Gc = %.2f\n", Gs, Gc) ;
#endif
for (j=0; j<D->ms_filter_size; j++) {
D->m_sin_table[j] = D->m_sin_table[j] / Gs;
D->m_cos_table[j] = D->m_cos_table[j] / Gc;
}
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Space\n");
dw_printf (" j shape S sin S cos\n");
#endif
Gs = 0;
Gc = 0;
for (j=0; j<D->ms_filter_size; j++) {
float as;
float center;
float shape = 1;
center = 0.5 * (D->ms_filter_size - 1);
as = ((float)(j - center) / (float)samples_per_sec) * ((float)space_freq) * (2 * M_PI);
shape = window (D->ms_window, D->ms_filter_size, j);
D->s_sin_table[j] = sin(as) * shape;
D->s_cos_table[j] = cos(as) * shape;
Gs += D->s_sin_table[j] * sin(as);
Gc += D->s_cos_table[j] * cos(as);
#if DEBUG1
dw_printf ("%6d %6.2f %6.2f %6.2f\n", j, shape, D->s_sin_table[j], D->s_cos_table[j] ) ;
#endif
}
/* Normalize for unity gain */
#if DEBUG1
dw_printf ("Before normalizing, Gs = %.2f, Gc = %.2f\n", Gs, Gc) ;
#endif
for (j=0; j<D->ms_filter_size; j++) {
D->s_sin_table[j] = D->s_sin_table[j] / Gs;
D->s_cos_table[j] = D->s_cos_table[j] / Gc;
}
/*
* Now the lowpass filter.
* I thought we'd want a cutoff of about 0.5 the baud rate
* but it turns out about 1.1x is better. Still investigating...
*/
if (D->lpf_use_fir) {
float fc;
fc = baud * D->lpf_baud / (float)samples_per_sec;
gen_lowpass (fc, D->lp_filter, D->lp_filter_size, D->lp_window);
}
/*
* A non-whole number of cycles results in a DC bias.
* Let's see if it helps to take it out.
* Actually makes things worse: 20 fewer decoded.
* Might want to try again after EXPERIMENTC.
*/
#if 0
#ifndef AVOID_FLOATING_POINT
failed experiment
dc_bias = 0;
for (j=0; j<D->ms_filter_size; j++) {
dc_bias += D->m_sin_table[j];
}
for (j=0; j<D->ms_filter_size; j++) {
D->m_sin_table[j] -= dc_bias / D->ms_filter_size;
}
dc_bias = 0;
for (j=0; j<D->ms_filter_size; j++) {
dc_bias += D->m_cos_table[j];
}
for (j=0; j<D->ms_filter_size; j++) {
D->m_cos_table[j] -= dc_bias / D->ms_filter_size;
}
dc_bias = 0;
for (j=0; j<D->ms_filter_size; j++) {
dc_bias += D->s_sin_table[j];
}
for (j=0; j<D->ms_filter_size; j++) {
D->s_sin_table[j] -= dc_bias / D->ms_filter_size;
}
dc_bias = 0;
for (j=0; j<D->ms_filter_size; j++) {
dc_bias += D->s_cos_table[j];
}
for (j=0; j<D->ms_filter_size; j++) {
D->s_cos_table[j] -= dc_bias / D->ms_filter_size;
}
#endif
#endif
/*
* In version 1.2 we try another experiment.
* Try using multiple slicing points instead of the traditional AGC.
*/
space_gain[0] = MIN_G;
float step = powf(10.0, log10f(MAX_G/MIN_G) / (MAX_SUBCHANS-1));
for (j=1; j<MAX_SUBCHANS; j++) {
space_gain[j] = space_gain[j-1] * step;
}
#ifndef GEN_FFF
#if 0
text_color_set(DW_COLOR_DEBUG);
for (j=0; j<MAX_SUBCHANS; j++) {
float db = 20.0 * log10f(space_gain[j]);
dw_printf ("G = %.3f, %+.1f dB\n", space_gain[j], db);
}
#endif
#endif
} /* fsk_gen_filter */
#if GEN_FFF
// Properties of the radio channels.
static struct audio_s modem;
// Filters will be stored here.
static struct demodulator_state_s ds;
#define SPARSE 3
static void emit_macro (char *name, int size, float *coeff)
{
int i;
dw_printf ("#define %s(x) \\\n", name);
for (i=SPARSE/2; i<size; i+=SPARSE) {
dw_printf ("\t%c (%.6ff * x[%d]) \\\n", (i==0 ? ' ' : '+'), coeff[i], i);
}
dw_printf ("\n");
}
int main ()
{
//int n;
char fff_profile;
fff_profile = 'F';
memset (&modem, 0, sizeof(modem));
memset (&ds, 0, sizeof(ds));
modem.adev[0].num_channels = 1;
modem.adev[0].samples_per_sec = DEFAULT_SAMPLES_PER_SEC;
modem.achan[0].mark_freq = DEFAULT_MARK_FREQ;
modem.achan[0].space_freq = DEFAULT_SPACE_FREQ;
modem.achan[0].baud = DEFAULT_BAUD;
modem.achan[0].num_demod = 1;
modem.achan[0].num_subchan = 1;
demod_afsk_init (modem.adev[0].samples_per_sec, modem.achan[0].baud,
modem.achan[0].mark_freq, modem.achan[0].space_freq, fff_profile, &ds);
printf ("/* This is an automatically generated file. Do not edit. */\n");
printf ("\n");
printf ("#define FFF_SAMPLES_PER_SEC %d\n", modem.adev[0].samples_per_sec);
printf ("#define FFF_BAUD %d\n", modem.achan[0].baud);
printf ("#define FFF_MARK_FREQ %d\n", modem.achan[0].mark_freq);
printf ("#define FFF_SPACE_FREQ %d\n", modem.achan[0].space_freq);
printf ("#define FFF_PROFILE '%c'\n", fff_profile);
printf ("\n");
emit_macro ("CALC_M_SUM1", ds.ms_filter_size, ds.m_sin_table);
emit_macro ("CALC_M_SUM2", ds.ms_filter_size, ds.m_cos_table);
emit_macro ("CALC_S_SUM1", ds.ms_filter_size, ds.s_sin_table);
emit_macro ("CALC_S_SUM2", ds.ms_filter_size, ds.s_cos_table);
exit(0);
}
#endif
#ifndef GEN_FFF
/* Optimization for slow processors. */
#include "fsk_fast_filter.h"
/*-------------------------------------------------------------------
*
* Name: demod_afsk_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.
*
*--------------------------------------------------------------------*/
static void nudge_pll (int chan, int subchan, int demod_data, struct demodulator_state_s *D);
__attribute__((hot))
void demod_afsk_process_sample (int chan, int subchan, int sam, struct demodulator_state_s *D)
{
float fsam, abs_fsam;
float m_sum1, m_sum2, s_sum1, s_sum2;
float m_amp, s_amp;
float m_norm, s_norm;
float demod_out;
#if DEBUG4
static FILE *demod_log_fp = NULL;
static int seq = 0; /* for log file name */
#endif
int j;
int demod_data;
assert (chan >= 0 && chan < MAX_CHANS);
assert (subchan >= 0 && subchan < MAX_SUBCHANS);
/*
* Filters use last 'filter_size' samples.
*
* First push the older samples down.
*
* Finally, put the most recent at the beginning.
*
* Future project? Can we do better than shifting each time?
*/
/* Scale to nice number, TODO: range -1.0 to +1.0, not 2. */
fsam = sam / 16384.0f;
abs_fsam = fsam >= 0.0f ? fsam : -fsam;
/*
* Optional bandpass filter before the mark/space discriminator.
*/
if (D->use_prefilter) {
float cleaner;
push_sample (fsam, D->raw_cb, D->pre_filter_size);
cleaner = convolve (D->raw_cb, D->pre_filter, D->pre_filter_size);
push_sample (cleaner, D->ms_in_cb, D->ms_filter_size);
}
else {
push_sample (fsam, D->ms_in_cb, D->ms_filter_size);
}
/*
* Next we have bandpass filters for the mark and space tones.
*
* This takes a lot of computation.
* It's not a problem on a typical (Intel x86 based) PC.
* Dire Wolf takes only about 2 or 3% of the CPU time.
*
* It might be too much for a little microcomputer to handle.
*
* Here we have an optimized case for the default values.
*/
// TODO1.2: is this right or do we need to store profile in the modulator info?
if (D->profile == toupper(FFF_PROFILE)) {
/* ========== Faster for default values on slower processors. ========== */
m_sum1 = CALC_M_SUM1(D->ms_in_cb);
m_sum2 = CALC_M_SUM2(D->ms_in_cb);
m_amp = z(m_sum1,m_sum2);
s_sum1 = CALC_S_SUM1(D->ms_in_cb);
s_sum2 = CALC_S_SUM2(D->ms_in_cb);
s_amp = z(s_sum1,s_sum2);
}
else {
/* ========== General case to handle all situations. ========== */
/*
* find amplitude of "Mark" tone.
*/
m_sum1 = convolve (D->ms_in_cb, D->m_sin_table, D->ms_filter_size);
m_sum2 = convolve (D->ms_in_cb, D->m_cos_table, D->ms_filter_size);
m_amp = sqrtf(m_sum1 * m_sum1 + m_sum2 * m_sum2);
/*
* Find amplitude of "Space" tone.
*/
s_sum1 = convolve (D->ms_in_cb, D->s_sin_table, D->ms_filter_size);
s_sum2 = convolve (D->ms_in_cb, D->s_cos_table, D->ms_filter_size);
s_amp = sqrtf(s_sum1 * s_sum1 + s_sum2 * s_sum2);
/* ========== End of general case. ========== */
}
/*
* Apply some low pass filtering BEFORE the AGC to remove
* overshoot, ringing, and other bad stuff.
*
* A simple IIR filter is faster but FIR produces better results.
*
* It is a balancing act between removing high frequency components
* from the tone dectection while letting the data thru.
*/
if (D->lpf_use_fir) {
push_sample (m_amp, D->m_amp_cb, D->lp_filter_size);
m_amp = convolve (D->m_amp_cb, D->lp_filter, D->lp_filter_size);
push_sample (s_amp, D->s_amp_cb, D->lp_filter_size);
s_amp = convolve (D->s_amp_cb, D->lp_filter, D->lp_filter_size);
}
else {
/* Original, but faster, IIR. */
m_amp = D->lpf_iir * m_amp + (1.0f - D->lpf_iir) * D->m_amp_prev;
D->m_amp_prev = m_amp;
s_amp = D->lpf_iir * s_amp + (1.0f - D->lpf_iir) * D->s_amp_prev;
D->s_amp_prev = s_amp;
}
/*
* Version 1.2: Try new approach to capturing the amplitude for display.
* This is same as the AGC above without the normalization step.
* We want decay to be substantially slower to get a longer
* range idea of the received audio.
*/
if (m_amp >= D->alevel_mark_peak) {
D->alevel_mark_peak = m_amp * D->quick_attack + D->alevel_mark_peak * (1.0f - D->quick_attack);
}
else {
D->alevel_mark_peak = m_amp * D->sluggish_decay + D->alevel_mark_peak * (1.0f - D->sluggish_decay);
}
if (s_amp >= D->alevel_space_peak) {
D->alevel_space_peak = s_amp * D->quick_attack + D->alevel_space_peak * (1.0f - D->quick_attack);
}
else {
D->alevel_space_peak = s_amp * D->sluggish_decay + D->alevel_space_peak * (1.0f - D->sluggish_decay);
}
/*
* Which tone is stronger?
*
* In an ideal world, simply compare. In my first naive attempt, that
* worked perfectly with perfect signals. In the real world, we don't
* have too many perfect signals.
*
* Here is an excellent explanation:
* http://www.febo.com/packet/layer-one/transmit.html
*
* Under real conditions, we find that the higher tone has a
* considerably smaller amplitude due to the passband characteristics
* of the transmitter and receiver. To make matters worse, it
* varies considerably from one station to another.
*
* The two filters also have different amounts of DC bias.
*
* My solution was to apply automatic gain control (AGC) to the mark and space
* levels. This works by looking at the minimum and maximum outputs
* for each filter and scaling the results to be roughly in the -0.5 to +0.5 range.
* Results were excellent after tweaking the attack and decay times.
*
* 4X6IZ took a different approach. See QEX Jul-Aug 2012.
*
* He ran two different demodulators in parallel. One of them boosted the higher
* frequency tone by 6 dB. Any duplicates were removed. This produced similar results.
* He also used a bandpass filter before the mark/space filters.
* I haven't tried this combination yet for 1200 baud.
*
* First, let's take a look at Track 1 of the TNC test CD. Here the receiver
* has a flat response. We find the mark/space strength ratios very from 0.53 to 1.38
* with a median of 0.81. This in in line with expections because most
* transmitters add pre-emphasis to boost the higher audio frequencies.
* Track 2 should more closely resemble what comes out of the speaker on a typical
* transceiver. Here we see a ratio from 1.73 to 3.81 with a median of 2.48.
*
* This is similar to my observations of local signals, from the speaker.
* The amplitude ratio varies from 1.48 to 3.41 with a median of 2.70.
*
* Rather than only two filters, let's try slicing the data in more places.
*/
/* Fast attack and slow decay. */
/* Numbers were obtained by trial and error from actual */
/* recorded less-than-optimal signals. */
/* See fsk_demod_agc.h for more information. */
m_norm = agc (m_amp, D->agc_fast_attack, D->agc_slow_decay, &(D->m_peak), &(D->m_valley));