-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulate.c
More file actions
1419 lines (1090 loc) · 42.5 KB
/
Modulate.c
File metadata and controls
1419 lines (1090 loc) · 42.5 KB
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
// Sample Creation routines (encode and filter) for ARDOP Modem
#include "ARDOPC.h"
#pragma warning(disable : 4244) // Code does lots of float to int
FILE * fp1;
#define MAX(x, y) ((x) > (y) ? (x) : (y))
// Function to generate the Two-tone leader and Frame Sync (used in all frame types)
extern short Dummy;
int intSoftClipCnt = 0;
void Flush();
void GetTwoToneLeaderWithSync(int intSymLen)
{
// Generate a 50 baud (20 ms symbol time) 2 tone leader
// leader tones used are 1475 and 1525 Hz.
int intSign = 1;
int i, j;
short intSample;
if ((intSymLen & 1) == 1)
intSign = -1;
for (i = 0; i < intSymLen; i++) //for the number of symbols needed (two symbols less than total leader length)
{
for (j = 0; j < 240; j++) // for 240 samples per symbol (50 baud)
{
if (i != (intSymLen - 1))
intSample = intSign * int50BaudTwoToneLeaderTemplate[j];
else
intSample = -intSign * int50BaudTwoToneLeaderTemplate[j];
SampleSink(intSample);
}
intSign = -intSign;
}
}
void SendLeaderAndSYNC(UCHAR * bytEncodedBytes, int intLeaderLen)
{
int intMask = 0;
int intLeaderLenMS;
int j, k, n;
UCHAR bytMask;
UCHAR bytSymToSend;
short intSample;
if (intLeaderLen == 0)
intLeaderLenMS = LeaderLength;
else
intLeaderLenMS = intLeaderLen;
// Create the leader
GetTwoToneLeaderWithSync(intLeaderLenMS / 20);
//Create the 8 symbols (16 bit) 50 baud 4FSK frame type with Implied SessionID
// No reference needed for 4FSK
// note revised To accomodate 1 parity symbol per byte (10 symbols total)
for(j = 0; j < 2; j++) // for the 2 bytes of the frame type
{
bytMask = 0xc0;
for(k = 0; k < 5; k++) // for 5 symbols per byte (4 data + 1 parity)
{
if (k < 4)
bytSymToSend = (bytMask & bytEncodedBytes[j]) >> (2 * (3 - k));
else
bytSymToSend = ComputeTypeParity(bytEncodedBytes[0]);
for(n = 0; n < 240; n++)
{
if (((5 * j + k) & 1 ) == 0)
intSample = intFSK50bdCarTemplate[bytSymToSend][n];
else
intSample = -intFSK50bdCarTemplate[bytSymToSend][n]; // -sign insures no phase discontinuity at symbol boundaries
SampleSink(intSample);
}
bytMask = bytMask >> 2;
}
}
}
void Mod4FSKDataAndPlay(int Type, unsigned char * bytEncodedBytes, int Len, int intLeaderLen)
{
// Function to Modulate data encoded for 4FSK, create
// the 16 bit samples and send to sound interface
// Function works for 1, 2 or 4 simultaneous carriers
int intNumCar, intBaud, intDataLen, intRSLen, intDataPtr, intSampPerSym, intDataBytesPerCar;
BOOL blnOdd;
int intSample;
char strType[18] = "";
char strMod[16] = "";
UCHAR bytSymToSend, bytMask, bytMinQualThresh;
float dblCarScalingFactor;
int intMask = 0;
int intLeaderLenMS;
int k, m, n;
if (!FrameInfo(Type, &blnOdd, &intNumCar, strMod, &intBaud, &intDataLen, &intRSLen, &bytMinQualThresh, strType))
return;
if (strcmp(strMod, "4FSK") != 0)
return;
WriteDebugLog(LOGDEBUG, "Sending Frame Type %s", strType);
DrawTXFrame(strType);
if (Type == PktFrameHeader)
{
// Meader is 4FSK which needs 500 filter
if (pktBW[pktMode] < 1000)
initFilter(500,1500);
else if (pktBW[pktMode] < 2000)
initFilter(1000,1500);
else
initFilter(2000,1500);
}
else
{
if (intBaud == 50)
initFilter(200,1500);
else if (intNumCar == 1)
initFilter(500,1500);
else if (intNumCar == 2)
initFilter(1000,1500);
else if (intNumCar == 4)
initFilter(2000,1500);
}
// If Not (strType = "DataACK" Or strType = "DataNAK" Or strType = "IDFrame" Or strType.StartsWith("ConReq") Or strType.StartsWith("ConAck")) Then
// strLastWavStream = strType
// End If
if (intLeaderLen == 0)
intLeaderLenMS = LeaderLength;
else
intLeaderLenMS = intLeaderLen;
switch(intBaud)
{
case 50:
intSampPerSym = 240;
break;
case 100:
intSampPerSym = 120;
}
intDataBytesPerCar = (Len - 2) / intNumCar; // We queue the samples here, so dont copy below
SendLeaderAndSYNC(bytEncodedBytes, intLeaderLen);
intDataPtr = 2;
Reenter:
switch(intNumCar)
{
case 1: // use carriers 0-3
dblCarScalingFactor = 1.0; // (scaling factors determined emperically to minimize crest factor)
for (m = 0; m < intDataBytesPerCar; m++) // For each byte of input data
{
bytMask = 0xC0; // Initialize mask each new data byte
for (k = 0; k < 4; k++) // for 4 symbol values per byte of data
{
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr]) >> (2 * (3 - k)); // Values 0-3
for (n = 0; n < intSampPerSym; n++) // Sum for all the samples of a symbols
{
if((k & 1) == 0)
{
if(intBaud == 50)
intSample = intFSK50bdCarTemplate[bytSymToSend][n];
else
intSample = intFSK100bdCarTemplate[bytSymToSend][n];
SampleSink(intSample);
}
else
{
if(intBaud == 50)
intSample = -intFSK50bdCarTemplate[bytSymToSend][n];
else
intSample = -intFSK100bdCarTemplate[bytSymToSend][n];
SampleSink(intSample);
}
}
bytMask = bytMask >> 2;
}
intDataPtr += 1;
}
if (Type == PktFrameHeader)
{
// just sent packet header. Send rest in current mode
// Assumes we are using 4FSK for Packet Header
Type = 0; // Prevent reentry
strcpy(strMod, &pktMod[pktMode][0]);
intDataBytesPerCar = pktDataLen + pktRSLen + 3;
intDataPtr = 11; // Over Header
intNumCar = pktCarriers[pktMode];
// This assumes Packet Data is sent as PSK/QAM
switch(intNumCar)
{
case 1:
// intCarStartIndex = 4;
dblCarScalingFactor = 1.0f; // Starting at 1500 Hz (scaling factors determined emperically to minimize crest factor) TODO: needs verification
break;
case 2:
// intCarStartIndex = 3;
dblCarScalingFactor = 0.53f; // Starting at 1400 Hz
break;
case 4:
// intCarStartIndex = 2;
dblCarScalingFactor = 0.29f; // Starting at 1200 Hz
break;
case 8:
// intCarStartIndex = 0;
dblCarScalingFactor = 0.17f; // Starting at 800 Hz
}
// Reenter to send rest of variable length packet frame
if (pktFSK[pktMode])
goto Reenter;
else
ModPSKDataAndPlay(PktFrameData, bytEncodedBytes, 0, 0);
return;
}
Flush();
break;
case 2: // use carriers 8-15 (100 baud only)
dblCarScalingFactor = 0.51f; // (scaling factors determined emperically to minimize crest factor)
for (m = 0; m < intDataBytesPerCar; m++) // For each byte of input data
{
bytMask = 0xC0; // Initialize mask each new data byte
for (k = 0; k < 4; k++) // for 4 symbol values per byte of data
{
for (n = 0; n < intSampPerSym; n++) // for all the samples of a symbol for 2 carriers
{
//' First carrier
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr]) >> (2 * (3 - k)); // Values 0-3
intSample = intFSK100bdCarTemplate[8 + bytSymToSend][n];
// Second carrier
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr + intDataBytesPerCar]) >> (2 * (3 - k)); // Values 0-3
intSample = dblCarScalingFactor * (intSample + intFSK100bdCarTemplate[12 + bytSymToSend][n]);
SampleSink(intSample);
}
bytMask = bytMask >> 2;
}
intDataPtr += 1;
}
Flush();
break;
case 4: // use carriers 4-19 (100 baud only)
dblCarScalingFactor = 0.27f; // (scaling factors determined emperically to minimize crest factor)
for (m = 0; m < intDataBytesPerCar; m++) // For each byte of input data
{
bytMask = 0xC0; // Initialize mask each new data byte
for (k = 0; k < 4; k++) // for 4 symbol values per byte of data
{
for (n = 0; n < intSampPerSym; n++) // for all the samples of a symbol for 2 carriers
{
//' First carrier
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr]) >> (2 * (3 - k)); // Values 0-3
intSample = intFSK100bdCarTemplate[4 + bytSymToSend][n];
// Second carrier
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr + intDataBytesPerCar]) >> (2 * (3 - k)); // Values 0-3
intSample = intSample + intFSK100bdCarTemplate[8 + bytSymToSend][n];
//' Third carrier
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr + 2 * intDataBytesPerCar]) >> (2 * (3 - k)); // Values 0-3
intSample = intSample + intFSK100bdCarTemplate[12 + bytSymToSend][n];
// ' Fourth carrier
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr + 3 * intDataBytesPerCar]) >> (2 * (3 - k)); // Values 0-3
intSample = dblCarScalingFactor * (intSample + intFSK100bdCarTemplate[16 + bytSymToSend][n]);
SampleSink(intSample);
}
bytMask = bytMask >> 2;
}
intDataPtr += 1;
}
Flush();
break;
}
}
// Function to Modulate encoded data to 8FSK and send to sound interface
void Mod8FSKDataAndPlay(int Type, unsigned char * bytEncodedBytes, int Len, int intLeaderLen)
{
// Function to Modulate data encoded for 8FSK, create
// the 16 bit samples and send to sound interface
int intBaud, intDataLen, intRSLen, intDataPtr, intSampPerSym, intDataBytesPerCar;
BOOL blnOdd;
int intNumCar;
short intSample;
unsigned int intThreeBytes = 0;
char strType[18] = "";
char strMod[16] = "";
UCHAR bytSymToSend, bytMinQualThresh;
int intMask = 0;
int k, m, n;
if (!FrameInfo(Type, &blnOdd, &intNumCar, strMod, &intBaud, &intDataLen, &intRSLen, &bytMinQualThresh, strType))
return;
if (strcmp(strMod, "8FSK") != 0)
return;
WriteDebugLog(LOGDEBUG, "Sending Frame Type %s", strType);
DrawTXFrame(strType);
initFilter(200,1500);
// If Not (strType = "DataACK" Or strType = "DataNAK" Or strType = "IDFrame" Or strType.StartsWith("ConReq") Or strType.StartsWith("ConAck")) Then
// strLastWavStream = strType
// End If
intSampPerSym = 240;
intDataBytesPerCar = (Len - 2) / intNumCar; // We queue the samples here, so dont copy below
SendLeaderAndSYNC(bytEncodedBytes, intLeaderLen);
intSampPerSym = 480; // 25 Baud
intDataPtr = 2;
for (m = 0; m < intDataBytesPerCar; m += 3) // For each byte of input data
{
intThreeBytes = bytEncodedBytes[intDataPtr++];
intThreeBytes = (intThreeBytes << 8) + bytEncodedBytes[intDataPtr++];
intThreeBytes = (intThreeBytes << 8) + bytEncodedBytes[intDataPtr++];
intMask = 0xE00000;
for (k = 0; k < 8; k++)
{
bytSymToSend = (intMask & intThreeBytes) >> (3 * (7 - k));
// note value of "+ 4" below allows using 16FSK template for 8FSK using only the "inner" 8 tones around 1500
for (n = 0; n < intSampPerSym; n++) // Sum for all the samples of a symbols
{
if((k & 1) == 0)
intSample = intFSK25bdCarTemplate[bytSymToSend + 4][n]; // Symbol vlaues 4- 11 (surrounding 1500 Hz)
else
intSample = -intFSK25bdCarTemplate[bytSymToSend + 4][n]; // Symbol vlaues 4- 11 (surrounding 1500 Hz)
SampleSink(intSample);
}
intMask = intMask >> 3;
}
}
Flush();
}
// Function to Modulate encoded data to 16FSK and send to sound interface
void Mod16FSKDataAndPlay(int Type, unsigned char * bytEncodedBytes, int Len, int intLeaderLen)
{
// Function to Modulate data encoded for 16FSK, create
// the 16 bit samples and send to sound interface
int intBaud, intDataLen, intRSLen, intDataPtr, intSampPerSym, intDataBytesPerCar;
BOOL blnOdd;
int intNumCar;
short intSample;
unsigned int intThreeBytes = 0;
char strType[18] = "";
char strMod[16] = "";
UCHAR bytSymToSend, bytMask, bytMinQualThresh;
int intMask = 0;
int k, m, n;
if (!FrameInfo(Type, &blnOdd, &intNumCar, strMod, &intBaud, &intDataLen, &intRSLen, &bytMinQualThresh, strType))
return;
if (strcmp(strMod, "16FSK") != 0)
return;
WriteDebugLog(LOGDEBUG, "Sending Frame Type %s", strType);
DrawTXFrame(strType);
initFilter(500,1500);
// If Not (strType = "DataACK" Or strType = "DataNAK" Or strType = "IDFrame" Or strType.StartsWith("ConReq") Or strType.StartsWith("ConAck")) Then
// strLastWavStream = strType
// End If
intDataBytesPerCar = (Len - 2) / intNumCar; // We queue the samples here, so dont copy below
intSampPerSym = 480; // 25 Baud
SendLeaderAndSYNC(bytEncodedBytes, intLeaderLen);
intDataPtr = 2;
for (m = 0; m < intDataBytesPerCar; m++) // For each byte of input data
{
bytMask = 0xF0; // Initialize mask each new data byte
for (k = 0; k < 2; k++) // for 2 symbol values per byte of data
{
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr]) >> (4 * (1 - k)); // Values 0 - 15
for (n = 0; n < intSampPerSym; n++) // Sum for all the samples of a symbols
{
if((k & 1) == 0)
intSample = intFSK25bdCarTemplate[bytSymToSend][n];
else
intSample = -intFSK25bdCarTemplate[bytSymToSend][n];
SampleSink(intSample);
}
bytMask = bytMask >> 4;
}
intDataPtr++;
}
Flush();
}
// Function to Modulate data encoded for 4FSK High baud rate and create the integer array of 32 bit samples suitable for playing
void Mod4FSK600BdDataAndPlay(int Type, unsigned char * bytEncodedBytes, int Len, int intLeaderLen)
{
// Function to Modulate data encoded for 4FSK, create
// the 16 bit samples and send to sound interface
// Function works for 1, 2 or 4 simultaneous carriers
int intNumCar, intBaud, intDataLen, intRSLen, intDataPtr, intSampPerSym, intDataBytesPerCar;
BOOL blnOdd;
short intSample;
char strType[18] = "";
char strMod[16] = "";
UCHAR bytSymToSend, bytMask, bytMinQualThresh;
int intMask = 0;
int k, m, n;
if (!FrameInfo(Type, &blnOdd, &intNumCar, strMod, &intBaud, &intDataLen, &intRSLen, &bytMinQualThresh, strType))
return;
if (strcmp(strMod, "4FSK") != 0)
return;
WriteDebugLog(LOGDEBUG, "Sending Frame Type %s", strType);
DrawTXFrame(strType);
initFilter(2000,1500);
// If Not (strType = "DataACK" Or strType = "DataNAK" Or strType = "IDFrame" Or strType.StartsWith("ConReq") Or strType.StartsWith("ConAck")) Then
// strLastWavStream = strType
// End If
intDataBytesPerCar = (Len - 2) / intNumCar; // We queue the samples here, so dont copy below
intSampPerSym = 12000 / intBaud;
SendLeaderAndSYNC(bytEncodedBytes, intLeaderLen);
intDataPtr = 2;
for (m = 0; m < intDataBytesPerCar; m++) // For each byte of input data
{
bytMask = 0xC0; // Initialize mask each new data byte
for (k = 0; k < 4; k++) // for 4 symbol values per byte of data
{
bytSymToSend = (bytMask & bytEncodedBytes[intDataPtr]) >> (2 * (3 - k)); // Values 0-3
for (n = 0; n < intSampPerSym; n++) // Sum for all the samples of a symbols
{
intSample = intFSK600bdCarTemplate[bytSymToSend][n];
SampleSink(intSample);
}
bytMask = bytMask >> 2;
}
intDataPtr += 1;
}
Flush();
}
// Function to extract an 8PSK symbol from an encoded data array
UCHAR GetSym8PSK(int intDataPtr, int k, int intCar, UCHAR * bytEncodedBytes, int intDataBytesPerCar)
{
int int3Bytes = bytEncodedBytes[intDataPtr + intCar * intDataBytesPerCar];
// int intMask = 7;
int intSym;
UCHAR bytSym;
int3Bytes = int3Bytes << 8;
int3Bytes += bytEncodedBytes[intDataPtr + intCar * intDataBytesPerCar + 1];
int3Bytes = int3Bytes << 8;
int3Bytes += bytEncodedBytes[intDataPtr + intCar * intDataBytesPerCar + 2]; // now have 3 bytes, 24 bits or 8 8PSK symbols
// intMask = intMask << (3 * (7 - k));
intSym = int3Bytes >> (3 * (7 - k));
bytSym = intSym & 7; //(intMask && int3Bytes) >> (3 * (7 - k));
return bytSym;
}
// Function to soft clip combined waveforms.
int SoftClip(int intInput)
{
if (intInput > 30000) // soft clip above/below 30000
{
intInput = min(32700, 30000 + 20 * sqrt(intInput - 30000));
intSoftClipCnt += 1;
}
else if(intInput < -30000)
{
intInput = max(-32700, -30000 - 20 * sqrt(-(intInput + 30000)));
intSoftClipCnt += 1;
}
return intInput;
}
// Function to Modulate data encoded for PSK and 16QAM, create
// the 16 bit samples and send to sound interface
void ModPSKDataAndPlay(int Type, unsigned char * bytEncodedBytes, int Len, int intLeaderLen)
{
int intNumCar, intBaud, intDataLen, intRSLen, intDataPtr, intSampPerSym, intDataBytesPerCar;
BOOL blnOdd;
int intSample;
char strType[18] = "";
char strMod[16] = "";
UCHAR bytSym, bytSymToSend, bytMask, bytMinQualThresh;
float dblCarScalingFactor;
int intMask = 0;
int intLeaderLenMS;
int i, k, m, n;
int intCarStartIndex;
int intPeakAmp;
int intCarIndex;
UCHAR bytLastSym[9]; // = {0}; // Holds the last symbol sent (per carrier). bytLastSym(4) is 1500 Hz carrier (only used on 1 carrier modes)
if (!FrameInfo(Type, &blnOdd, &intNumCar, strMod, &intBaud, &intDataLen, &intRSLen, &bytMinQualThresh, strType))
return;
intDataBytesPerCar = (Len - 2) / intNumCar; // We queue the samples here, so dont copy below
switch(intNumCar)
{
// These new scaling factor combined with soft clipping to provide near optimum scaling Jan 6, 2018
// The Test form was changed to calculate the Peak power to RMS power (PAPR) of the test waveform and count the number of "soft clips" out of ~ 50,000 samples.
// These values arrived at emperically using the Test form (Quick brown fox message) to minimize PAPR at a minor decrease in maximum constellation quality
// Rick uses these for QAM
//dblCarScalingFactor = 1.2 ' Starting at 1500 Hz Selected to give < 9% clipped values yielding a PAPR = 1.77 Constellation Quality >98
//dblCarScalingFactor = 0.67 ' Carriers at 1400 and 1600 Selected to give < 2.5% clipped values yielding a PAPR = 2.17, Constellation Quality >92
// dblCarScalingFactor = 0.4 ' Starting at 1200 Hz Selected to give < 1.5% clipped values yielding a PAPR = 2.48, Constellation Quality >92
// dblCarScalingFactor = 0.27 ' Starting at 800 Hz Selected to give < 1% clipped values yielding a PAPR = 2.64, Constellation Quality >94
case 1:
intCarStartIndex = 4;
// dblCarScalingFactor = 1.0f; // Starting at 1500 Hz (scaling factors determined emperically to minimize crest factor) TODO: needs verification
dblCarScalingFactor = 1.2f; // Starting at 1500 Hz Selected to give < 13% clipped values yielding a PAPR = 1.6 Constellation Quality >98
case 2:
intCarStartIndex = 3;
// dblCarScalingFactor = 0.53f;
if (strcmp(strMod, "16QAM") == 0)
dblCarScalingFactor = 0.67f; // Carriers at 1400 and 1600 Selected to give < 2.5% clipped values yielding a PAPR = 2.17, Constellation Quality >92
else
dblCarScalingFactor = 0.65f; // Carriers at 1400 and 1600 Selected to give < 4% clipped values yielding a PAPR = 2.0, Constellation Quality >95
break;
case 4:
intCarStartIndex = 2;
// dblCarScalingFactor = 0.29f; // Starting at 1200 Hz
dblCarScalingFactor = 0.4f; // Starting at 1200 Hz Selected to give < 3% clipped values yielding a PAPR = 2.26, Constellation Quality >95
break;
case 8:
intCarStartIndex = 0;
// dblCarScalingFactor = 0.17f; // Starting at 800 Hz
if (strcmp(strMod, "16QAM") == 0)
dblCarScalingFactor = 0.27f; // Starting at 800 Hz Selected to give < 1% clipped values yielding a PAPR = 2.64, Constellation Quality >94
else
dblCarScalingFactor = 0.25f; // Starting at 800 Hz Selected to give < 2% clipped values yielding a PAPR = 2.5, Constellation Quality >95
}
intSampPerSym = 120;
if (Type == PktFrameData)
{
intDataBytesPerCar = pktDataLen + pktRSLen + 3;
intDataPtr = 11; // Over Header
goto PktLoopBack;
}
WriteDebugLog(LOGDEBUG, "Sending Frame Type %s", strType);
DrawTXFrame(strType);
/* // DOnt use PSK Header at the moment
if (Type == PktFrameHeader)
{
// Header is always 200 but Packet Data may vary
if (pktNumCar == 1)
initFilter(200,1500);
else if (pktNumCar == 2)
initFilter(500,1500);
else if (pktNumCar == 4)
initFilter(1000,1500);
else if (pktNumCar == 8)
initFilter(2000,1500);
}
else
{
*/
if (intNumCar == 1)
initFilter(200,1500);
else if (intNumCar == 2)
initFilter(500,1500);
else if (intNumCar == 4)
initFilter(1000,1500);
else if (intNumCar == 8)
initFilter(2000,1500);
// }
// If Not (strType = "DataACK" Or strType = "DataNAK" Or strType = "IDFrame" Or strType.StartsWith("ConReq") Or strType.StartsWith("ConAck")) Then
// strLastWavStream = strType
// End If
if (intLeaderLen == 0)
intLeaderLenMS = LeaderLength;
else
intLeaderLenMS = intLeaderLen;
intSoftClipCnt = 0;
// Create the leader
SendLeaderAndSYNC(bytEncodedBytes, intLeaderLen);
intPeakAmp = 0;
intDataPtr = 2; // initialize pointer to start of data.
PktLoopBack: // Reenter here to send rest of variable length packet frame
// Now create a reference symbol for each carrier
// We have to do each carrier for each sample, as we write
// the sample immediately
for (n = 0; n < intSampPerSym; n++) // Sum for all the samples of a symbols
{
intSample = 0;
intCarIndex = intCarStartIndex; // initialize to correct starting carrier
for (i = 0; i < intNumCar; i++) // across all carriers
{
bytSymToSend = 0; // using non 0 causes error on first data byte 12/8/2014 ...Values 0-3 not important (carries no data). (Possible chance for Crest Factor reduction?)
bytLastSym[intCarIndex] = bytSymToSend;
if (intBaud == 100)
intSample += intPSK100bdCarTemplate[intCarIndex][0][n]; // double the symbol value during template lookup for 4PSK. (skips over odd PSK 8 symbols)
else
intSample += intPSK200bdCarTemplate[intCarIndex][0][n]; // subtract 2 from the symbol value before doubling and subtract value of table
intCarIndex += 1;
if (intCarIndex == 4)
intCarIndex += 1; // skip over 1500 Hz for multi carrier modes (multi carrier modes all use even hundred Hz tones)
}
intSample = intSample * dblCarScalingFactor; // on the last carrier rescale value based on # of carriers to bound output
SampleSink(intSample);
}
// End of reference phase generation
if (strcmp(strMod, "4PSK") == 0)
{
for (m = 0; m < intDataBytesPerCar; m++) // For each byte of input data (all carriers)
{
bytMask = 0xC0; // Initialize mask each new data byte
for (k = 0; k < 4; k++) // for 4 symbol values per byte of data
{
for (n = 0; n < intSampPerSym; n++) // Sum for all the samples of a symbols
{
intSample = 0;
intCarIndex = intCarStartIndex; // initialize the carrrier index
for (i = 0; i < intNumCar ; i++) // across all carriers
{
bytSym = (bytMask & bytEncodedBytes[intDataPtr + i * intDataBytesPerCar]) >> (2 * (3 - k));
bytSymToSend = ((bytLastSym[intCarIndex] + bytSym) & 3); // Values 0-3
if (intBaud == 100)
{
if (bytSymToSend < 2)
intSample += intPSK100bdCarTemplate[intCarIndex][bytSymToSend * 2][n]; // double the symbol value during template lookup for 4PSK. (skips over odd PSK 8 symbols)
else
intSample -= intPSK100bdCarTemplate[intCarIndex][2 * (bytSymToSend - 2)][n]; // subtract 2 from the symbol value before doubling and subtract value of table
}
else
{
if (bytSymToSend < 2)
intSample += intPSK200bdCarTemplate[intCarIndex][bytSymToSend * 2][n]; // double the symbol value during template lookup for 4PSK. (skips over odd PSK 8 symbols)
else
intSample -= intPSK200bdCarTemplate[intCarIndex][2 * (bytSymToSend - 2)][n]; // subtract 2 from the symbol value before doubling and subtract value of table
}
if (n == intSampPerSym - 1) // Last sample?
bytLastSym[intCarIndex] = bytSymToSend;
intCarIndex += 1;
if (intCarIndex == 4)
intCarIndex += 1; // skip over 1500 Hz for multi carrier modes (multi carrier modes all use even hundred Hz tones)
}
intSample = intSample * dblCarScalingFactor; // on the last carrier rescale value based on # of carriers to bound output
// if (intSample > 32700)
// intSample = 32700;
// if (intSample < -32700)
// intSample = -32700;
intSample = SoftClip(intSample);
SampleSink(intSample);
}
bytMask = bytMask >> 2;
}
intDataPtr += 1;
}
}
else if (strcmp(strMod, "8PSK") == 0)
{
// More complex ...must go through data in 3 byte chunks creating 8 Three bit symbols for each 3 bytes of data.
for (m = 0; m < intDataBytesPerCar / 3; m++)
{
for (k = 0; k < 8; k++) // for 8 symbols in 24 bits of int3Bytes
{
for (n = 0; n < intSampPerSym; n++) // Sum for all the samples of a symbols
{
intSample = 0;
// We have to sum all samples for all carriers
intCarIndex = intCarStartIndex;
for (i = 0; i < intNumCar; i++)
{
bytSym = GetSym8PSK(intDataPtr, k, i, bytEncodedBytes, intDataBytesPerCar);
bytSymToSend = ((bytLastSym[intCarIndex] + bytSym) & 7); // mod 8
if (intBaud == 100)
{
if (bytSymToSend < 4) // This uses the symmetry of the symbols to reduce the table size by a factor of 2
intSample += intPSK100bdCarTemplate[intCarIndex][bytSymToSend][n]; // positive phase values template lookup for 8PSK.
else
intSample -= intPSK100bdCarTemplate[intCarIndex][bytSymToSend - 4][n]; // negative phase values, subtract value of table
}
else
{
if (bytSymToSend < 4) // This uses the symmetry of the symbols to reduce the table size by a factor of 2
intSample += intPSK200bdCarTemplate[intCarIndex][bytSymToSend][n]; // positive phase values template lookup for 8PSK.
else
intSample -= intPSK200bdCarTemplate[intCarIndex][bytSymToSend - 4][n]; // negative phase values, subtract value of table
}
if (n == intSampPerSym - 1) // Last sample?
bytLastSym[intCarIndex] = bytSymToSend;
intCarIndex += 1;
if (intCarIndex == 4)
intCarIndex += 1; // skip over 1500 Hz for multi carrier modes (multi carrier modes all use even hundred Hz tones)
}
intSample = intSample * dblCarScalingFactor; // on the last carrier rescale value based on # of carriers to bound output
// if (intSample > 32700)
// intSample = 32700;
// if (intSample < -32700)
// intSample = -32700;
intSample = SoftClip(intSample);
SampleSink(intSample);
}
}
intDataPtr += 3;
}
}
else if (strcmp(strMod, "16QAM") == 0)
{
for (m = 0; m < intDataBytesPerCar; m++) // For each byte of input data (all carriers)
{
bytMask = 0xF0; // Initialize mask each new data byte
for (k = 0; k < 2; k++) // for 2 symbol values per byte of data
{
for (n = 0; n < intSampPerSym; n++) // Sum for all the samples of a symbols
{
intSample = 0;
intCarIndex = intCarStartIndex; // initialize the carrrier index
for (i = 0; i < intNumCar ; i++) // across all carriers
{
bytSym = (bytMask & bytEncodedBytes[intDataPtr + i * intDataBytesPerCar]) >> (4 * (1 - k));
bytSymToSend = (bytLastSym[intCarIndex] + (bytSym & 7)) & 7; // Values 0-7
//if (intBaud == 100) only use 100
//{
if (bytSym < 8)
{
if (bytSymToSend < 4) // This uses the symmetry of the symbols to reduce the table size by a factor of 2
intSample += intPSK100bdCarTemplate[intCarIndex][bytSymToSend][n]; // positive phase values template lookup for 8PSK.
else
intSample -= intPSK100bdCarTemplate[intCarIndex][bytSymToSend - 4][n]; // negative phase values, subtract value of table
}
else
{
if (bytSymToSend < 4) // This uses the symmetry of the symbols to reduce the table size by a factor of 2
intSample += 0.5f * intPSK100bdCarTemplate[intCarIndex][bytSymToSend][n]; // positive phase values template lookup for 8PSK.
else
intSample -= 0.5f * intPSK100bdCarTemplate[intCarIndex][bytSymToSend - 4][n]; // negative phase values, subtract value of table
}
//}
if (n == intSampPerSym - 1) // Last sample?
bytLastSym[intCarIndex] = bytSymToSend;
intCarIndex += 1;
if (intCarIndex == 4)
intCarIndex += 1; // skip over 1500 Hz for multi carrier modes (multi carrier modes all use even hundred Hz tones)
}
intSample = intSample * dblCarScalingFactor; // on the last carrier rescale value based on # of carriers to bound output
// if (intSample > 32700)
// intSample = 32700;
// if (intSample < -32700)
// intSample = -32700;
intSample = SoftClip(intSample);
SampleSink(intSample);
}
bytMask = bytMask >> 4;
}
intDataPtr += 1;
}
}
if (Type == PktFrameHeader)
{
// just sent packet header. Send rest in current mode
Type = 0; // Prevent reentry
strcpy(strMod, &pktMod[pktMode][0]);
intDataBytesPerCar = pktDataLen + pktRSLen + 3;
intDataPtr = 11; // Over Header
intNumCar = pktCarriers[pktMode];
switch(intNumCar)
{
case 1:
intCarStartIndex = 4;
// dblCarScalingFactor = 1.0f; // Starting at 1500 Hz (scaling factors determined emperically to minimize crest factor) TODO: needs verification
dblCarScalingFactor = 1.2f; // Starting at 1500 Hz Selected to give < 13% clipped values yielding a PAPR = 1.6 Constellation Quality >98
case 2:
intCarStartIndex = 3;
// dblCarScalingFactor = 0.53f;
if (strcmp(strMod, "16QAM") == 0)
dblCarScalingFactor = 0.67f; // Carriers at 1400 and 1600 Selected to give < 2.5% clipped values yielding a PAPR = 2.17, Constellation Quality >92
else
dblCarScalingFactor = 0.65f; // Carriers at 1400 and 1600 Selected to give < 4% clipped values yielding a PAPR = 2.0, Constellation Quality >95
break;
case 4:
intCarStartIndex = 2;
// dblCarScalingFactor = 0.29f; // Starting at 1200 Hz
dblCarScalingFactor = 0.4f; // Starting at 1200 Hz Selected to give < 3% clipped values yielding a PAPR = 2.26, Constellation Quality >95
break;
case 8:
intCarStartIndex = 0;
// dblCarScalingFactor = 0.17f; // Starting at 800 Hz
if (strcmp(strMod, "16QAM") == 0)
dblCarScalingFactor = 0.27f; // Starting at 800 Hz Selected to give < 1% clipped values yielding a PAPR = 2.64, Constellation Quality >94
else
dblCarScalingFactor = 0.25f; // Starting at 800 Hz Selected to give < 2% clipped values yielding a PAPR = 2.5, Constellation Quality >95
}
goto PktLoopBack; // Reenter to send rest of variable length packet frame
}
Flush();
if (intSoftClipCnt > 0)
WriteDebugLog(LOGDEBUG, "Soft Clips %d ", intSoftClipCnt);
}
// Subroutine to add trailer before filtering
void AddTrailer()
{
int intAddedSymbols = 1 + TrailerLength / 10; // add 1 symbol + 1 per each 10 ms of MCB.Trailer
int i, k;
for (i = 1; i <= intAddedSymbols; i++)
{
for (k = 0; k < 120; k++)
{
SampleSink(intPSK100bdCarTemplate[4][0][k]);
}
}
}
// Resends the last frame
void RemodulateLastFrame()
{
int intNumCar, intBaud, intDataLen, intRSLen;
UCHAR bytMinQualThresh;
BOOL blnOdd;
char strType[18] = "";
char strMod[16] = "";
if (!FrameInfo(bytEncodedBytes[0], &blnOdd, &intNumCar, strMod, &intBaud, &intDataLen, &intRSLen, &bytMinQualThresh, strType))
return;
if (strcmp(strMod, "4FSK") == 0)
{
if (bytEncodedBytes[0] >= 0x7A && bytEncodedBytes[0] <= 0x7D)