-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundInput.c
5789 lines (4419 loc) · 183 KB
/
SoundInput.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
// ARDOP Modem Decode Sound Samples
#include "ARDOPC.h"
#pragma warning(disable : 4244) // Code does lots of float to int
#ifndef TEENSY
#define MEMORYARQ
#endif
#undef PLOTWATERFALL
#ifdef PLOTWATERFALL
#define WHITE 0xffff
#define Tomato 0xffff
#define Orange 0xffff
#define Khaki 0xffff
#define Cyan 0xffff
#define DeepSkyBlue 0
#define RoyalBlue 0
#define Navy 0
#define Black 0
#endif
#ifdef TEENSY
#define PKTLED LED3 // flash when packet received
extern unsigned int PKTLEDTimer;
#endif
//#define max(x, y) ((x) > (y) ? (x) : (y))
//#define min(x, y) ((x) < (y) ? (x) : (y))
void SendFrametoHost(unsigned char *data, unsigned dlen);
void CheckandAdjustRXLevel(int maxlevel, int minlevel, BOOL Force);
void clearDisplay();
void updateDisplay();
VOID L2Routine(UCHAR * Packet, int Length, int FrameQuality, int totalRSErrors, int NumCar, int pktRXMode);
void DrawAxes(int Qual, const char * FrameType, char * Mode);
extern int lastmax, lastmin; // Sample Levels
char strRcvFrameTag[32];
BOOL blnLeaderFound = FALSE;
int intLeaderRcvdMs = 1000; // Leader length??
extern int intLastRcvdFrameQuality;
extern int intReceivedLeaderLen;
extern UCHAR bytLastReceivedDataFrameType;
extern int NErrors;
extern BOOL blnBREAKCmd;
extern UCHAR bytLastACKedDataFrameType;
extern int intARQDefaultDlyMs;
unsigned int tmrFinalID;
extern BOOL PKTCONNECTED;
void DrawDecode(char * Decode);
extern int pktRXMode;
short intPriorMixedSamples[120]; // a buffer of 120 samples to hold the prior samples used in the filter
int intPriorMixedSamplesLength = 120; // size of Prior sample buffer
// While searching for leader we must save unprocessed samples
// We may have up to 720 left, so need 1920
short rawSamples[2400]; // Get Frame Type need 2400 and we may add 1200
int rawSamplesLength = 0;
short intFilteredMixedSamples[5000]; // Get Frame Type need 2400 and we may add 1200
int intFilteredMixedSamplesLength = 0;
int intFrameType; // Type we are decoding
int LastDataFrameType; // Last data frame processed (for Memory ARQ, etc)
char strDecodeCapture[1024];
// Frame type parameters
int intCenterFreq = 1500;
float intCarFreq; //(was int) // Are these the same ??
int intNumCar;
int intBaud;
int intDataLen;
int intRSLen;
int intSampleLen;
int DataRate = 0; // For SCS Reporting
int intDataPtr;
int intSampPerSym;
int intDataBytesPerCar;
BOOL blnOdd;
char strType[18] = "";
char strMod[16] = "";
UCHAR bytMinQualThresh;
int intPSKMode;
#define MAX_RAW_LENGTH 256 // I think! Max length of an RS block
#define MAX_DATA_LENGTH 8 * 128 // I think! 16QAM.2000.100
// intToneMags should be an array with one row per carrier.
// and 16 * max bytes data (2 bits per symbol, 4 samples per symbol in 4FSK.
// but as 600 Baud frames are very long (750 bytes), but only one carrier
// may be better to store as scalar and calculate offsets into it for each carrier
// treat 600 as 3 * 200, but scalar still may be better
// Needs 64K if ints + another 64 for MEM ARQ. (maybe able to store as shorts)
// 48K would do if we use a scalar (600 baud, 750 bytes)
// Max is 4 carrier, 83 bytes or 1 carrier 762 (or treat as 3 * 253)
// Could just about do this on Teensy 3.6 or Nucleo F7
// looks like we have 4 samples for each 2 bits, which means 16 samples per byte.
int intToneMags[4][16 * MAX_RAW_LENGTH] = {0}; // Need one per carrier
int intToneMagsIndex[4];
// Same here
int intSumCounts[8]; // number in above arrays
int intToneMagsLength;
unsigned char goodCarriers = 0; // Carriers we have already decoded
// We always collect all phases for PSK and QAM so we can do phase correction
short intPhases[8][652] = {0}; // We will decode as soon as we have 4 or 8 depending on mode
// (but need one set per carrier)
// 652 is 163 * 4 (4PSK 167 Baud)
// We only use Mags for QAM, and max is 2 carriers 195 bytes
// ??????????????????
//short intMags[2][195 * 2] = {0};
short intMags[8][652] = {0};
#ifdef MEMORYARQ
// Enough RAM for memory ARQ so keep all samples for FSK and a copy of tones or phase/amplitude
int intToneMagsAvg[4][332]; //???? FSK Tone averages
short intCarPhaseAvg[8][652]; // array to accumulate phases for averaging (Memory ARQ)
short intCarMagAvg[8][652]; // array to accumulate mags for averaging (Memory ARQ)
#endif
//219 /3 * 8= 73 * 8 = 584
//163 * 4 = 652
// If we do Mem ARQ we will need a fair amount of RAM
int intPhasesLen;
// Received Frame
UCHAR bytData[MAX_DATA_LENGTH];
int frameLen;
int totalRSErrors;
// We need one raw buffer per carrier
// This can be optimized quite a bit to save space
// We can probably overlay on bytData
UCHAR bytFrameData1[760]; // Received chars
UCHAR bytFrameData2[MAX_RAW_LENGTH]; // Received chars
UCHAR bytFrameData3[MAX_RAW_LENGTH]; // Received chars
UCHAR bytFrameData4[MAX_RAW_LENGTH]; // Received chars
UCHAR bytFrameData5[MAX_RAW_LENGTH]; // Received chars
UCHAR bytFrameData6[MAX_RAW_LENGTH]; // Received chars
UCHAR bytFrameData7[MAX_RAW_LENGTH]; // Received chars
UCHAR bytFrameData8[MAX_RAW_LENGTH]; // Received chars
UCHAR * bytFrameData[8] = {bytFrameData1, bytFrameData2,
bytFrameData3, bytFrameData4, bytFrameData5,
bytFrameData6, bytFrameData7, bytFrameData8};
char CarrierOk[8]; // RS OK Flags per carrier
int charIndex = 0; // Index into received chars
int SymbolsLeft; // number still to decode
int DummyCarrier = 0; // pseudo carrier used for long 600 baud frames
UCHAR * Decode600Buffer = bytFrameData1;
BOOL PSKInitDone = FALSE;
BOOL blnSymbolSyncFound, blnFrameSyncFound;
extern UCHAR bytLastARQSessionID;
extern UCHAR bytCurrentFrameType;
extern int intShiftUpDn;
extern const char ARQSubStates[10][11];
extern int intLastARQDataFrameToHost;
// dont think I need it short intRcvdSamples[12000]; // 1 second. May need to optimise
float dblOffsetLastGoodDecode = 0;
int dttLastGoodFrameTypeDecode = -20000;
float dblOffsetHz = 0;;
int dttLastLeaderDetect;
extern int intRmtLeaderMeasure;
extern BOOL blnARQConnected;
extern BOOL blnPending;
extern UCHAR bytPendingSessionID;
extern UCHAR bytSessionID;
int dttLastGoodFrameTypeDecod;
int dttStartRmtLeaderMeasure;
char lastGoodID[11] = "";
int GotBitSyncTicks;
int intARQRTmeasuredMs;
float dbl2Pi = 2 * M_PI;
float dblSNdBPwr;
float dblNCOFreq = 3000; // nominal NC) frequency
float dblNCOPhase = 0;
float dblNCOPhaseInc = 2 * M_PI * 3000 / 12000; // was dblNCOFreq
int intMFSReadPtr = 30; // reset the MFSReadPtr offset 30 to accomodate the filter delay
int RcvdSamplesLen = 0; // Samples in RX buffer
BOOL Acquire2ToneLeaderSymbolFraming();
BOOL SearchFor2ToneLeader3(short * intNewSamples, int Length, float * dblOffsetHz, int * intSN);
BOOL AcquireFrameSyncRSB();
int Acquire4FSKFrameType();
void DemodulateFrame(int intFrameType);
void Demod1Car4FSKChar(int Start, UCHAR * Decoded, int Carrier);
VOID Track1Car4FSK(short * intSamples, int * intPtr, int intSampPerSymbol, float intSearchFreq, int intBaud, UCHAR * bytSymHistory);
VOID Decode1CarPSK(UCHAR * Decoded, int Carrier);
int EnvelopeCorrelator();
BOOL DecodeFrame(int intFrameType, UCHAR * bytData);
void Update4FSKConstellation(int * intToneMags, int * intQuality);
void Update16FSKConstellation(int * intToneMags, int * intQuality);
void Update8FSKConstellation(int * intToneMags, int * intQuality);
void ProcessPingFrame(char * bytData);
int Compute4FSKSN();
void DemodPSK();
BOOL DemodQAM();
/*
const int SamplesToComplete[256] = {
// lookup the number of samples (@12 KHz) needed to complete the frame after the Frame ID is detected
// Value is increased by factor of 1.005 (5000 ppm) to accomodate sample rate offsets in Transmitter and Receiver
// Also used to validate frame type (len != -1)
// Note these samples DO NOT include the PSK reference symbols which are accomodated in DemodPSK
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00 - 0F ACK and NAK
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 10 - 1F
-1,-1,-1,0,-1,-1,0,-1,-1,0,-1,-1,0,0,0,-1, // BREAK=23, IDLE=26, DISC=29, END=2C, ConRejBusy=2D, ConRejBW=2E
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30 - 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30 - 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30 - 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30 - 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30 - 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30 - 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30 - 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30 - 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * (6 + 6 + 2) * 4 * 240), // 30, 38 ID Frame Call sign + Grid Square, Connect request frames
(int)(1.005 * 240 * 3 * 4), // 39 - 3c Con ACK with timing data
(int)(1.005 * 240 * 3 * 4), // 39 - 3c Con ACK with timing data
(int)(1.005 * 240 * 3 * 4), // 39 - 3c Con ACK with timing data
(int)(1.005 * 240 * 3 * 4), // 39 - 3c Con ACK with timing data
-1,-1,-1, // 3d - 3f
(int)(1.005 * (120 + (1 + 64 + 2 + 32) * 4 * 120)), // 40, 41 1 carrier 100 baud 4PSK
(int)(1.005 * (120 + (1 + 64 + 2 + 32) * 4 * 120)), // 40, 41 1 carrier 100 baud 4PSK
(int)(1.005 * (120 + (1 + 16 + 2 + 8) * 4 * 120)), // 42, 43 1 carrier 100 baud 4PSK Short
(int)(1.005 * (120 + (1 + 16 + 2 + 8) * 4 * 120)), // 42, 43 1 carrier 100 baud 4PSK Short
(int)(1.005 * (120 + (120 * (8 * (1 + 108 + 2 + 36)) / 3))), // 44, 45 1 carrier 100 baud 8PSK
(int)(1.005 * (120 + (120 * (8 * (1 + 108 + 2 + 36)) / 3))), // 44, 45 1 carrier 100 baud 8PSK
(int)(1.005 * (240 * 4 * (1 + 32 + 2 + 8))), // 46, 47 1 carrier 50 baud 4FSK
(int)(1.005 * (240 * 4 * (1 + 32 + 2 + 8))), // 46, 47 1 carrier 50 baud 4FSK
(int)(1.005 * (240 * 4 * (1 + 16 + 2 + 4))), // 48, 49 ' 1 carrier 50 baud 4FSK short
(int)(1.005 * (240 * 4 * (1 + 16 + 2 + 4))), // 48, 49 ' 1 carrier 50 baud 4FSK short
(int)(1.005 * (120 * 4 * (1 + 64 + 2 + 16))), // 4A, 4B ' 1 carrier 100 baud 4FSK
(int)(1.005 * (120 * 4 * (1 + 64 + 2 + 16))), // 4A, 4B ' 1 carrier 100 baud 4FSK
(int)(1.005 * (120 * 4 * (1 + 32 + 2 + 8))), // 4C, 4d ' 1 carrier 100 baud 4FSK short
(int)(1.005 * (120 * 4 * (1 + 32 + 2 + 8))), // 4C, 4d ' 1 carrier 100 baud 4FSK short
(int)(1.005 * (480 * (8 * (1 + 24 + 2 + 6)) / 3)), // 4E, 4F ' 1 carrier 25 baud 8FSK
(int)(1.005 * (480 * (8 * (1 + 24 + 2 + 6)) / 3)), // 4E, 4F ' 1 carrier 25 baud 8FSK
(int)(1.005 * (120 + (1 + 64 + 2 + 32) * 4 * 120)), // 50, 51 ' 2 carrier 100 baud 4PSK
(int)(1.005 * (120 + (1 + 64 + 2 + 32) * 4 * 120)), // 50, 51 ' 2 carrier 100 baud 4PSK
(int)(1.005 * (120 + (120 * (8 * (1 + 108 + 2 + 36)) / 3))), // 52, 53 ' 2 carrier 100 baud 8PSK
(int)(1.005 * (120 + (120 * (8 * (1 + 108 + 2 + 36)) / 3))), // 52, 53 ' 2 carrier 100 baud 8PSK
(int)(1.005 * (72 + (1 + 120 + 2 + 40) * 4 * 72)), // 54, 55 ' 2 carrier 167 baud 4PSK
(int)(1.005 * (72 + (1 + 120 + 2 + 40) * 4 * 72)), // 54, 55 ' 2 carrier 167 baud 4PSK
(int)(1.005 * (72 + (72 * (8 * (1 + 159 + 2 + 60)) / 3))), // 56, 57 ' 2 carrier 167 baud 8PSK
(int)(1.005 * (72 + (72 * (8 * (1 + 159 + 2 + 60)) / 3))), // 56, 57 ' 2 carrier 167 baud 8PSK
(int)(1.005 * (480 * 2 * (1 + 32 + 2 + 8))), // 58, 59 ' 1 carrier 25 baud 16FSK (in testing)
(int)(1.005 * (480 * 2 * (1 + 32 + 2 + 8))), // 58, 59 ' 1 carrier 25 baud 16FSK (in testing)
(int)(1.005 * (480 * 2 * (1 + 16 + 2 + 4))), // 5A, 5B ' 1 carrier 25 baud 16FSK Short (in testing)
(int)(1.005 * (480 * 2 * (1 + 16 + 2 + 4))), // 5A, 5B ' 1 carrier 25 baud 16FSK Short (in testing)
-1,-1,-1,-1, // 5C -5F
(int)(1.005 * (120 + (1 + 64 + 2 + 32) * 4 * 120)), // 60, 61 ' 4 carrier 100 baud 4PSK
(int)(1.005 * (120 + (1 + 64 + 2 + 32) * 4 * 120)), // 60, 61 ' 4 carrier 100 baud 4PSK
(int)(1.005 * (120 + (120 * (8 * (1 + 108 + 2 + 36)) / 3))), //62, 63 ' 4 carrier 100 baud 8PSK
(int)(1.005 * (120 + (120 * (8 * (1 + 108 + 2 + 36)) / 3))), //62, 63 ' 4 carrier 100 baud 8PSK
(int)(1.005 * (72 + (1 + 120 + 2 + 40) * 4 * 72)), // 64, 65 ' 4 carrier 167 baud 4PSK
(int)(1.005 * (72 + (1 + 120 + 2 + 40) * 4 * 72)), // 64, 65 ' 4 carrier 167 baud 4PSK
(int)(1.005 * (72 + (72 * (8 * (1 + 159 + 2 + 60)) / 3))), // 66, 67 ' 4 carrier 167 baud 8PSK
(int)(1.005 * (72 + (72 * (8 * (1 + 159 + 2 + 60)) / 3))), // 66, 67 ' 4 carrier 167 baud 8PSK
(int)(1.005 * (120 * 4 * (1 + 64 + 2 + 16))), // 68, 69 ' 2 carrier 100 baud 4FSK
(int)(1.005 * (120 * 4 * (1 + 64 + 2 + 16))), // 68, 69 ' 2 carrier 100 baud 4FSK
-1,-1,-1,-1,-1,-1, // 6A - 6F
(int)(1.005 * (120 + (1 + 64 + 2 + 32) * 4 * 120)), // 70, 71 ' 8 carrier 100 baud 4PSK
(int)(1.005 * (120 + (1 + 64 + 2 + 32) * 4 * 120)), // 70, 71 ' 8 carrier 100 baud 4PSK
(int)(1.005 * (120 + (120 * (8 * (1 + 108 + 2 + 36)) / 3))), // 72, 73 ' 8 carrier 100 baud 8PSK
(int)(1.005 * (120 + (120 * (8 * (1 + 108 + 2 + 36)) / 3))), // 72, 73 ' 8 carrier 100 baud 8PSK
(int)(1.005 * (72 + (1 + 120 + 2 + 40) * 4 * 72)), //74, 75 ' 8 carrier 167 baud 4PSK
(int)(1.005 * (72 + (1 + 120 + 2 + 40) * 4 * 72)), //74, 75 ' 8 carrier 167 baud 4PSK
(int)(1.005 * (72 + (72 * (8 * (1 + 159 + 2 + 60)) / 3))), // 76, 77 ' 2 carrier 167 baud 8PSK ' 8 carrier 167 baud 4PSK
(int)(1.005 * (72 + (72 * (8 * (1 + 159 + 2 + 60)) / 3))), // 76, 77 ' 2 carrier 167 baud 8PSK ' 8 carrier 167 baud 4PSK
(int)(1.005 * (120 * 4 * (1 + 64 + 2 + 16))), // 78, 79 ' 4 carrier 100 baud 4FSK
(int)(1.005 * (120 * 4 * (1 + 64 + 2 + 16))), // 78, 79 ' 4 carrier 100 baud 4FSK
// experimental 600 baud for VHF/UHF FM
(int)(1.005 * (20 * 4 * 3 * (1 + 200 + 2 + 50))), // 7A, 7B ' 1 carrier 600 baud 4FSK (3 groups of 200 bytes each for RS compatibility)
(int)(1.005 * (20 * 4 * 3 * (1 + 200 + 2 + 50))), // 7A, 7B ' 1 carrier 600 baud 4FSK (3 groups of 200 bytes each for RS compatibility)
(int)(1.005 * (20 * 4 * (1 + 200 + 2 + 50))), // 7C, 7D ' 1 carrier 600 baud 4FSK short
(int)(1.005 * (20 * 4 * (1 + 200 + 2 + 50))), // 7C, 7D ' 1 carrier 600 baud 4FSK short
-1,-1, // 7E, 7F
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 80 - 8F
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 90 - 9F
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // A0 - AF
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // B0 - BF
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // C0 - CF
// experimental SOUNDINGs
(int)(1.005 * 60 * 18 * 40), // D0
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // D1 - DF
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // e0 - eF ACK and NAK
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // f0 - ff
*/
// Function to determine if a valid frame type
//extern const UCHAR isValidFrame[256];
//BOOL IsValidFrameType(UCHAR bytType)
//{
// // used in the minimum distance decoder (update if frames added or removed)
// return (isValidFrame[bytType]);
//}
// Function to determine if frame type is short control frame
BOOL IsShortControlFrame(UCHAR bytType)
{
if (bytType <= 0x1F) return TRUE; // NAK
if (bytType == 0x23 || bytType == 0x24 || bytType == 0x29 || bytType == 0x2C || bytType == 0x2D || bytType == 0x2E) return TRUE; // BREAK, IDLE, DISC, END, ConRejBusy, ConRejBW
if (bytType >= 0xE0) return TRUE; // ACK
return FALSE;
}
// Function to determine if it is a data frame (Even OR Odd)
BOOL IsDataFrame(UCHAR intFrameType)
{
const char * String = Name(intFrameType);
if (intFrameType == PktFrameHeader)
return TRUE;
if (String == NULL || String[0] == 0)
return FALSE;
if (strstr(String, ".E") || strstr(String, ".O"))
return TRUE;
return FALSE;
}
// Subroutine to clear all mixed samples
void ClearAllMixedSamples()
{
intFilteredMixedSamplesLength = 0;
intMFSReadPtr = 0;
rawSamplesLength = 0; // Clear saved
}
// Subroutine to Initialize mixed samples
void InitializeMixedSamples()
{
// Measure the time from release of PTT to leader detection of reply.
intARQRTmeasuredMs = min(10000, Now - dttStartRTMeasure); //?????? needs work
intPriorMixedSamplesLength = 120; // zero out prior samples in Prior sample buffer
intFilteredMixedSamplesLength = 0; // zero out the FilteredMixedSamples array
intMFSReadPtr = 30; // reset the MFSReadPtr offset 30 to accomodate the filter delay
}
// Subroutine to discard all sampled prior to current intRcvdSamplesRPtr
void DiscardOldSamples()
{
// This restructures the intRcvdSamples array discarding all samples prior to intRcvdSamplesRPtr
//not sure why we need this !!
/*
if (RcvdSamplesLen - intRcvdSamplesRPtr <= 0)
RcvdSamplesLen = intRcvdSamplesRPtr = 0;
else
{
// This is rather slow. I'd prefer a cyclic buffer. Lets see....
memmove(intRcvdSamples, &intRcvdSamples[intRcvdSamplesRPtr], (RcvdSamplesLen - intRcvdSamplesRPtr)* 2);
RcvdSamplesLen -= intRcvdSamplesRPtr;
intRcvdSamplesRPtr = 0;
}
*/
}
// Subroutine to apply 2000 Hz filter to mixed samples
float xdblZin_1 = 0, xdblZin_2 = 0, xdblZComb= 0; // Used in the comb generator
// The resonators
float xdblZout_0[27] = {0.0f}; // resonator outputs
float xdblZout_1[27] = {0.0f}; // resonator outputs delayed one sample
float xdblZout_2[27] = {0.0f}; // resonator outputs delayed two samples
float xdblCoef[27] = {0.0}; // the coefficients
float xdblR = 0.9995f; // insures stability (must be < 1.0) (Value .9995 7/8/2013 gives good results)
int xintN = 120; //Length of filter 12000/100
void FSMixFilter2000Hz(short * intMixedSamples, int intMixedSamplesLength)
{
// assumes sample rate of 12000
// implements 23 100 Hz wide sections (~2000 Hz wide @ - 30dB centered on 1500 Hz)
// FSF (Frequency Selective Filter) variables
// This works on intMixedSamples, len intMixedSamplesLength;
// Filtered data is appended to intFilteredMixedSamples
float dblRn;
float dblR2;
float dblZin = 0;
int i, j;
float intFilteredSample = 0; // Filtered sample
if (intFilteredMixedSamplesLength < 0)
WriteDebugLog(LOGERROR, "Corrupt intFilteredMixedSamplesLength");
dblRn = powf(xdblR, xintN);
dblR2 = powf(xdblR, 2);
// Initialize the coefficients
if (xdblCoef[26] == 0)
{
for (i = 4; i <= 26; i++)
{
xdblCoef[i] = 2 * xdblR * cosf(2 * M_PI * i / xintN); // For Frequency = bin i
}
}
for (i = 0; i < intMixedSamplesLength; i++)
{
intFilteredSample = 0;
if (i < xintN)
dblZin = intMixedSamples[i] - dblRn * intPriorMixedSamples[i];
else
dblZin = intMixedSamples[i] - dblRn * intMixedSamples[i - xintN];
//Compute the Comb
xdblZComb = dblZin - xdblZin_2 * dblR2;
xdblZin_2 = xdblZin_1;
xdblZin_1 = dblZin;
// Now the resonators
for (j = 4; j <= 26; j++) // calculate output for 3 resonators
{
xdblZout_0[j] = xdblZComb + xdblCoef[j] * xdblZout_1[j] - dblR2 * xdblZout_2[j];
xdblZout_2[j] = xdblZout_1[j];
xdblZout_1[j] = xdblZout_0[j];
//' scale each by transition coeff and + (Even) or - (Odd)
//' Resonators 2 and 13 scaled by .389 get best shape and side lobe supression
//' Scaling also accomodates for the filter "gain" of approx 60.
if (j == 4 || j == 26)
intFilteredSample += 0.389f * xdblZout_0[j];
else if ((j & 1) == 0)
intFilteredSample += xdblZout_0[j];
else
intFilteredSample -= xdblZout_0[j];
}
intFilteredSample = intFilteredSample * 0.00833333333f;
intFilteredMixedSamples[intFilteredMixedSamplesLength++] = intFilteredSample; // rescales for gain of filter
}
// update the prior intPriorMixedSamples array for the next filter call
memmove(intPriorMixedSamples, &intMixedSamples[intMixedSamplesLength - xintN], intPriorMixedSamplesLength * 2);
if (intFilteredMixedSamplesLength > 5000)
WriteDebugLog(LOGERROR, "Corrupt intFilteredMixedSamplesLength");
}
// Function to apply 150Hz filter used in Envelope correlator
void Filter150Hz(short * intFilterOut)
{
// assumes sample rate of 12000
// implements 3 100 Hz wide sections (~150 Hz wide @ - 30dB centered on 1500 Hz)
// FSF (Frequency Selective Filter) variables
static float dblR = 0.9995f; // insures stability (must be < 1.0) (Value .9995 7/8/2013 gives good results)
static int intN = 120; //Length of filter 12000/100
static float dblRn;
static float dblR2;
static float dblCoef[17] = {0.0}; // the coefficients
float dblZin = 0, dblZin_1 = 0, dblZin_2 = 0, dblZComb= 0; // Used in the comb generator
// The resonators
float dblZout_0[17] = {0.0}; // resonator outputs
float dblZout_1[17] = {0.0}; // resonator outputs delayed one sample
float dblZout_2[17] = {0.0}; // resonator outputs delayed two samples
int i, j;
float FilterOut = 0; // Filtered sample
float largest = 0;
dblRn = powf(dblR, intN);
dblR2 = powf(dblR, 2);
// Initialize the coefficients
if (dblCoef[17] == 0)
{
for (i = 14; i <= 16; i++)
{
dblCoef[i] = 2 * dblR * cosf(2 * M_PI * i / intN); // For Frequency = bin i
}
}
for (i = 0; i < 480; i++)
{
if (i < intN)
dblZin = intFilteredMixedSamples[intMFSReadPtr + i] - dblRn * 0; // no prior mixed samples
else
dblZin = intFilteredMixedSamples[intMFSReadPtr + i] - dblRn * intFilteredMixedSamples[intMFSReadPtr + i - intN];
// Compute the Comb
dblZComb = dblZin - dblZin_2 * dblR2;
dblZin_2 = dblZin_1;
dblZin_1 = dblZin;
// Now the resonators
for (j = 14; j <= 16; j++) // calculate output for 3 resonators
{
dblZout_0[j] = dblZComb + dblCoef[j] * dblZout_1[j] - dblR2 * dblZout_2[j];
dblZout_2[j] = dblZout_1[j];
dblZout_1[j] = dblZout_0[j];
// scale each by transition coeff and + (Even) or - (Odd)
// Scaling also accomodates for the filter "gain" of approx 120.
// These transition coefficients fairly close to optimum for WGN 0db PSK4, 100 baud (yield highest average quality) 5/24/2014
if (j == 14 || j == 16)
FilterOut = 0.2f * dblZout_0[j]; // this transisiton minimizes ringing and peaks
else
FilterOut -= dblZout_0[j];
}
intFilterOut[i] = (int)ceil(FilterOut * 0.00833333333); // rescales for gain of filter
}
}
// Function to apply 75Hz filter used in Envelope correlator
void Filter75Hz(short * intFilterOut, BOOL blnInitialise, int intSamplesToFilter)
{
// assumes sample rate of 12000
// implements 3 50 Hz wide sections (~75 Hz wide @ - 30dB centered on 1500 Hz)
// FSF (Frequency Selective Filter) variables
static float dblR = 0.9995f; // insures stability (must be < 1.0) (Value .9995 7/8/2013 gives good results)
static int intN = 240; // Length of filter 12000/50 - delays output 120 samples from input
static float dblRn;
static float dblR2;
static float dblCoef[3] = {0.0}; // the coefficients
float dblZin = 0, dblZin_1 = 0, dblZin_2 = 0, dblZComb= 0; // Used in the comb generator
// The resonators
float dblZout_0[3] = {0.0}; // resonator outputs
float dblZout_1[3] = {0.0}; // resonator outputs delayed one sample
float dblZout_2[3] = {0.0}; // resonator outputs delayed two samples
int i, j;
float FilterOut = 0; // Filtered sample
float largest = 0;
dblRn = powf(dblR, intN);
dblR2 = powf(dblR, 2);
// Initialize the coefficients
if (dblCoef[2] == 0)
{
for (i = 0; i <= 3; i++)
{
dblCoef[i] = 2 * dblR * cosf(2 * M_PI * (29 + i)/ intN); // For Frequency = bin 29, 30, 31
}
}
for (i = 0; i < intSamplesToFilter; i++)
{
if (i < intN)
dblZin = intFilteredMixedSamples[intMFSReadPtr + i] - dblRn * 0; // no prior mixed samples
else
dblZin = intFilteredMixedSamples[intMFSReadPtr + i] - dblRn * intFilteredMixedSamples[intMFSReadPtr + i - intN];
// Compute the Comb
dblZComb = dblZin - dblZin_2 * dblR2;
dblZin_2 = dblZin_1;
dblZin_1 = dblZin;
// Now the resonators
for (j = 0; j < 3; j++) // calculate output for 3 resonators
{
dblZout_0[j] = dblZComb + dblCoef[j] * dblZout_1[j] - dblR2 * dblZout_2[j];
dblZout_2[j] = dblZout_1[j];
dblZout_1[j] = dblZout_0[j];
// scale each by transition coeff and + (Even) or - (Odd)
// Scaling also accomodates for the filter "gain" of approx 120.
// These transition coefficients fairly close to optimum for WGN 0db PSK4, 100 baud (yield highest average quality) 5/24/2014
if (j == 0 || j == 2)
FilterOut -= 0.39811f * dblZout_0[j]; // this transisiton minimizes ringing and peaks
else
FilterOut += dblZout_0[j];
}
intFilterOut[i] = (int)ceil(FilterOut * 0.0041f); // rescales for gain of filter
}
}
// Subroutine to Mix new samples with NCO to tune to nominal 1500 Hz center with reversed sideband and filter.
void MixNCOFilter(short * intNewSamples, int Length, float dblOffsetHz)
{
// Correct the dimension of intPriorMixedSamples if needed (should only happen after a bandwidth setting change).
int i;
short intMixedSamples[2400]; // All we need at once ( I hope!) // may need to be int
int intMixedSamplesLength ; //size of intMixedSamples
if (Length == 0)
return;
// Nominal NCO freq is 3000 Hz to downmix intNewSamples (NCO - Fnew) to center of 1500 Hz (invertes the sideband too)
dblNCOFreq = 3000 + dblOffsetHz;
dblNCOPhaseInc = dblNCOFreq * dbl2Pi / 12000;
intMixedSamplesLength = Length;
for (i = 0; i < Length; i++)
{
intMixedSamples[i] = (int)ceilf(intNewSamples[i] * cosf(dblNCOPhase)); // later may want a lower "cost" implementation of "Cos"
dblNCOPhase += dblNCOPhaseInc;
if (dblNCOPhase > dbl2Pi)
dblNCOPhase -= dbl2Pi;
}
// showed no significant difference if the 2000 Hz filer used for all bandwidths.
// printtick("Start Filter");
FSMixFilter2000Hz(intMixedSamples, intMixedSamplesLength); // filter through the FS filter (required to reject image from Local oscillator)
// printtick("Done Filter");
// save for analysys
// WriteSamples(&intFilteredMixedSamples[oldlen], Length);
// WriteSamples(intMixedSamples, Length);
}
// Function to Correct Raw demodulated data with Reed Solomon FEC
int CorrectRawDataWithRS(UCHAR * bytRawData, UCHAR * bytCorrectedData, int intDataLen, int intRSLen, int bytFrameType, int Carrier)
{
BOOL blnRSOK;
BOOL FrameOK;
//Dim bytNoRS(1 + intDataLen + 2 - 1) As Byte ' 1 byte byte Count, Data, 2 byte CRC
//Array.Copy(bytRawData, 0, bytNoRS, 0, bytNoRS.Length)
if (CarrierOk[Carrier] && CarrierOk[Carrier] != 1)
CarrierOk[Carrier] = CarrierOk[Carrier];
if (CarrierOk[Carrier]) // Already decoded this carrier?
{
// Athough we have already checked the data, it may be in the wrong place
// in the buffer if another carrier was decoded wrong.
memcpy(bytCorrectedData, &bytRawData[1], bytRawData[0]);
WriteDebugLog(LOGDEBUG, "[CorrectRawDataWithRS] Carrier %d already decoded", Carrier);
return bytRawData[0]; // don't do it again
}
if (CheckCRC16FrameType(bytRawData, intDataLen + 1, bytFrameType)) // No RS correction needed
{
// return the actual data
memcpy(bytCorrectedData, &bytRawData[1], bytRawData[0]);
WriteDebugLog(LOGDEBUG, "[CorrectRawDataWithRS] OK without RS");
CarrierOk[Carrier] = TRUE;
return bytRawData[0];
}
// Try correcting with RS Parity
FrameOK = RSDecode(bytRawData, intDataLen + 3 + intRSLen, intRSLen, &blnRSOK);
if (blnRSOK)
{}
// WriteDebugLog(LOGDEBUG, "RS Says OK without correction");
else
if (FrameOK)
{}
// WriteDebugLog(LOGDEBUG, "RS Says OK after %d correction(s)", NErrors);
else
{
WriteDebugLog(LOGDEBUG, "[CorrectRawDataWithRS] RS Says Can't Correct");
goto returnBad;
}
if (FrameOK && CheckCRC16FrameType(bytRawData, intDataLen + 1, bytFrameType)) // RS correction successful
{
int intFailedByteCnt = 0;
// need to fix this if we want to use it
// test code just to determine how many corrections were applied ...later remove
//for (j = 0 ; j < intDataLen + 3; j++)
//{
// if (bytRawData[j] <> bytCorrectedData[j])
// intFailedByteCnt++;
//}
WriteDebugLog(LOGDEBUG, "[CorrectRawDataWithRS] OK with RS %d corrections", NErrors);
totalRSErrors += NErrors;
// End of test code
memcpy(bytCorrectedData, &bytRawData[1], bytRawData[0]);
CarrierOk[Carrier] = TRUE;
return bytRawData[0];
}
else
WriteDebugLog(LOGDEBUG, "[CorrectRawDataWithRS] RS says ok but CRC still bad");
// return uncorrected data without byte count or RS Parity
returnBad:
memcpy(bytCorrectedData, &bytRawData[1], intDataLen);
//Array.Copy(bytRawData, 1, bytCorrectedData, 0, bytCorrectedData.Length)
CarrierOk[Carrier] = FALSE;
return intDataLen;
}
// Subroutine to process new samples as received from the sound card via Main.ProcessCapturedData
// Only called when not transmitting
double dblPhaseInc; // in milliradians
short intNforGoertzel[8];
short intPSKPhase_1[8], intPSKPhase_0[8];
short intCP[8]; // Cyclic prefix offset
float dblFreqBin[8];
void ProcessNewSamples(short * Samples, int nSamples)
{
BOOL blnFrameDecodedOK = FALSE;
// LookforUZ7HOLeader(Samples, nSamples);
// printtick("Start afsk");
// DemodAFSK(Samples, nSamples);
// printtick("End afsk");
// return;
if (ProtocolState == FECSend)
return;
// Append new data to anything in rawSamples
if (rawSamplesLength)
{
memcpy(&rawSamples[rawSamplesLength], Samples, nSamples * 2);
rawSamplesLength += nSamples;
nSamples = rawSamplesLength;
Samples = rawSamples;
}
rawSamplesLength = 0;
if (nSamples < 1024)
{
memmove(rawSamples, Samples, nSamples * 2);
rawSamplesLength = nSamples;
return; // FFT needs 1024 samples
}
UpdateBusyDetector(Samples);
// searchforleader runs on unmixed and unfilered samples
// Searching for leader
if (State == SearchingForLeader)
{
// Search for leader as long as 960 samples (8 symbols) available
// printtick("Start Leader Search");
if (nSamples >= 1200)
{
// printtick("Start Busy");
// if (State == SearchingForLeader)
// UpdateBusyDetector(Samples);
// printtick("Done Busy");
if (ProtocolState == FECSend)
return;
}
while (State == SearchingForLeader && nSamples >= 1200)
{
int intSN;
blnLeaderFound = SearchFor2ToneLeader3(Samples, nSamples, &dblOffsetHz, &intSN);
// blnLeaderFound = SearchFor2ToneLeader2(Samples, nSamples, &dblOffsetHz, &intSN);
if (blnLeaderFound)
{
// WriteDebugLog(LOGDEBUG, "Got Leader");
dttLastLeaderDetect = Now;
nSamples -= 480;
Samples += 480; // !!!! needs attention !!!
InitializeMixedSamples();
State = AcquireSymbolSync;
}
else
{
if (SlowCPU)
{
nSamples -= 480;
Samples += 480; // advance pointer 2 symbols (40 ms) ' reduce CPU loading
}
else
{
nSamples -= 240;
Samples += 240; // !!!! needs attention !!!
}
}
}
if (State == SearchingForLeader)
{
// Save unused samples
memmove(rawSamples, Samples, nSamples * 2);
rawSamplesLength = nSamples;
// printtick("End Leader Search");
return;
}
}
// Got leader
// At this point samples haven't been processed, and are in Samples, len nSamples
// I'm going to filter all samples into intFilteredMixedSamples.
// printtick("Start Mix");
MixNCOFilter(Samples, nSamples, dblOffsetHz); // Mix and filter new samples (Mixing consumes all intRcvdSamples)
nSamples = 0; // all used
// printtick("Done Mix Samples");
// Acquire Symbol Sync
if (State == AcquireSymbolSync)
{
if ((intFilteredMixedSamplesLength - intMFSReadPtr) > 860)
{
blnSymbolSyncFound = Acquire2ToneLeaderSymbolFraming(); // adjust the pointer to the nominal symbol start based on phase
if (blnSymbolSyncFound)
State = AcquireFrameSync;
else
{
DiscardOldSamples();
ClearAllMixedSamples();
State = SearchingForLeader;
return;
}
// printtick("Got Sym Sync");
}
}
// Acquire Frame Sync
if (State == AcquireFrameSync)
{
blnFrameSyncFound = AcquireFrameSyncRSB();
if (blnFrameSyncFound)
{
State = AcquireFrameType;
// Have frame Sync. Remove used samples from buffer
// printtick("Got Frame Sync");
}
// Remove used samples
intFilteredMixedSamplesLength -= intMFSReadPtr;
if (intFilteredMixedSamplesLength < 0)
WriteDebugLog(LOGDEBUG, "Corrupt intFilteredMixedSamplesLength");