forked from df8oe/RS41HUP
-
Notifications
You must be signed in to change notification settings - Fork 9
/
horus_l2.c
1183 lines (951 loc) · 36 KB
/
horus_l2.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
/*---------------------------------------------------------------------------*\
FILE........: horus_l2.c
AUTHOR......: David Rowe
DATE CREATED: Dec 2015
Horus telemetry layer 2 processing. Takes an array of 8 bit payload
data, generates parity bits for a (23,12) Golay code, interleaves
data and parity bits, pre-pends a Unique Word for modem sync.
Caller is responsible for providing storage for output packet.
[ ] code based interleaver
[ ] test correction of 1,2 & 3 error patterms
1/ Unit test on a PC:
$ gcc horus_l2.c -o horus_l2 -Wall -DHORUS_L2_UNITTEST
$ ./horus_l2
test 0: 22 bytes of payload data BER: 0.00 errors: 0
test 0: 22 bytes of payload data BER: 0.01 errors: 0
test 0: 22 bytes of payload data BER: 0.05 errors: 0
test 0: 22 bytes of payload data BER: 0.10 errors: 7
This indicates it's correcting all channel errors for 22 bytes of
payload data, at bit error rate (BER) of 0, 0.01, 0.05. It falls
over at a BER of 0.10 which is expected.
2/ To build with just the tx function, ie for linking with the payload
firmware:
$ gcc horus_l2.c -c -Wall
By default the RX side is #ifdef-ed out, leaving the minimal amount
of code for tx.
3/ Generate some tx_bits as input for testing with fsk_horus:
$ gcc horus_l2.c -o horus_l2 -Wall -DGEN_TX_BITS -DSCRAMBLER
$ ./horus_l2
$ more ../octave/horus_tx_bits_binary.txt
4/ Unit testing interleaver:
$ gcc horus_l2.c -o horus_l2 -Wall -DINTERLEAVER -DTEST_INTERLEAVER -DSCRAMBLER
5/ Compile for use as decoder called by fsk_horus.m and fsk_horus_stream.m:
$ gcc horus_l2.c -o horus_l2 -Wall -DDEC_RX_BITS -DHORUS_L2_RX
\*---------------------------------------------------------------------------*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "horus_l2.h"
#ifdef HORUS_L2_UNITTEST
#define HORUS_L2_RX
#endif
#define RUN_TIME_TABLES
#define INTERLEAVER
#define SCRAMBLER
static char uw[] = {'$','$'};
/* Function Prototypes ------------------------------------------------*/
int32_t get_syndrome(int32_t pattern);
void golay23_init(void);
int golay23_decode(int received_codeword);
unsigned short gen_crc16(unsigned char* data_p, unsigned char length);
void interleave(unsigned char *inout, int nbytes, int dir);
void scramble(unsigned char *inout, int nbytes);
/* Functions ----------------------------------------------------------*/
/*
We are using a Golay (23,12) code which has a codeword 23 bits
long. The tx packet format is:
| Unique Word | payload data bits | parity bits |
This function works out how much storage the caller of
horus_l2_encode_tx_packet() will need to store the tx packet
*/
int horus_l2_get_num_tx_data_bytes(int num_payload_data_bytes) {
int num_payload_data_bits, num_golay_codewords;
int num_tx_data_bits, num_tx_data_bytes;
num_payload_data_bits = num_payload_data_bytes*8;
num_golay_codewords = num_payload_data_bits/12;
if (num_payload_data_bits % 12) /* round up to 12 bits, may mean some unused bits */
num_golay_codewords++;
num_tx_data_bits = sizeof(uw)*8 + num_payload_data_bits + num_golay_codewords*11;
num_tx_data_bytes = num_tx_data_bits/8;
if (num_tx_data_bits % 8) /* round up to nearest byte, may mean some unused bits */
num_tx_data_bytes++;
#ifdef DEBUG0
fprintf(stderr, "\nnum_payload_data_bytes: %d\n", num_payload_data_bytes);
fprintf(stderr, "num_golay_codewords...: %d\n", num_golay_codewords);
fprintf(stderr, "num_tx_data_bits......: %d\n", num_tx_data_bits);
fprintf(stderr, "num_tx_data_bytes.....: %d\n\n", num_tx_data_bytes);
#endif
return num_tx_data_bytes;
}
/*
Takes an array of payload data bytes, prepends a unique word and appends
parity bits.
The encoder will run on the payload on a small 8-bit uC. As we are
memory constrained so we do a lot of burrowing for bits out of
packed arrays, and don't use a LUT for Golay encoding. Hopefully it
will run fast enough. This was quite difficult to get going,
suspect there is a better way to write this. Oh well, have to start
somewhere.
*/
int horus_l2_encode_tx_packet(unsigned char *output_tx_data,
unsigned char *input_payload_data,
int num_payload_data_bytes)
{
int num_tx_data_bytes, num_payload_data_bits;
unsigned char *pout = output_tx_data;
int ninbit, ningolay, nparitybits;
int32_t ingolay, paritybyte, inbit, golayparity;
int ninbyte, shift, golayparitybit, i;
num_tx_data_bytes = horus_l2_get_num_tx_data_bytes(num_payload_data_bytes);
memcpy(pout, uw, sizeof(uw)); pout += sizeof(uw);
memcpy(pout, input_payload_data, num_payload_data_bytes); pout += num_payload_data_bytes;
/* Read input bits one at a time. Fill input Golay codeword. Find output Golay codeword.
Write this to parity bits. Write parity bytes when we have 8 parity bits. Bits are
written MSB first. */
num_payload_data_bits = num_payload_data_bytes*8;
ninbit = 0;
ingolay = 0;
ningolay = 0;
paritybyte = 0;
nparitybits = 0;
while (ninbit < num_payload_data_bits) {
/* extract input data bit */
ninbyte = ninbit/8;
shift = 7 - (ninbit % 8);
inbit = (input_payload_data[ninbyte] >> shift) & 0x1;
#ifdef DEBUG1
fprintf(stderr, "inbit %d ninbyte: %d inbyte: 0x%02x inbit: %d\n",
ninbit, ninbyte, input_payload_data[ninbyte], inbit);
#endif
ninbit++;
/* build up input golay codeword */
ingolay = ingolay | inbit;
ningolay++;
/* when we get 12 bits do a Golay encode */
if (ningolay % 12) {
ingolay <<= 1;
}
else {
#ifdef DEBUG0
fprintf(stderr, " ningolay: %d ingolay: 0x%04x\n", ningolay, ingolay);
#endif
golayparity = get_syndrome(ingolay<<11);
ingolay = 0;
#ifdef DEBUG0
fprintf(stderr, " golayparity: 0x%04x\n", golayparity);
#endif
/* write parity bits to output data */
for (i=0; i<11; i++) {
golayparitybit = (golayparity >> (10-i)) & 0x1;
paritybyte = paritybyte | golayparitybit;
#ifdef DEBUG0
fprintf(stderr, " i: %d golayparitybit: %d paritybyte: 0x%02x\n",
i, golayparitybit, paritybyte);
#endif
nparitybits++;
if (nparitybits % 8) {
paritybyte <<= 1;
}
else {
/* OK we have a full byte ready */
*pout = paritybyte;
#ifdef DEBUG0
fprintf(stderr," Write paritybyte: 0x%02x\n", paritybyte);
#endif
pout++;
paritybyte = 0;
}
}
}
} /* while(.... */
/* Complete final Golay encode, we may have partially finished ingolay, paritybyte */
#ifdef DEBUG0
fprintf(stderr, "finishing up .....\n");
#endif
if (ningolay % 12) {
ingolay >>= 1;
golayparity = get_syndrome(ingolay<<12);
#ifdef DEBUG0
fprintf(stderr, " ningolay: %d ingolay: 0x%04x\n", ningolay, ingolay);
fprintf(stderr, " golayparity: 0x%04x\n", golayparity);
#endif
/* write parity bits to output data */
for (i=0; i<11; i++) {
golayparitybit = (golayparity >> (10 - i)) & 0x1;
paritybyte = paritybyte | golayparitybit;
#ifdef DEBUG1
fprintf(stderr, " i: %d golayparitybit: %d paritybyte: 0x%02x\n",
i, golayparitybit, paritybyte);
#endif
nparitybits++;
if (nparitybits % 8) {
paritybyte <<= 1;
}
else {
/* OK we have a full byte ready */
*pout++ = (unsigned char)paritybyte;
#ifdef DEBUG0
fprintf(stderr," Write paritybyte: 0x%02x\n", paritybyte);
#endif
paritybyte = 0;
}
}
}
/* and final, partially complete, parity byte */
if (nparitybits % 8) {
paritybyte <<= 7 - (nparitybits % 8); // use MS bits first
*pout++ = (unsigned char)paritybyte;
#ifdef DEBUG0
fprintf(stderr," Write last paritybyte: 0x%02x nparitybits: %d \n", paritybyte, nparitybits);
#endif
}
#ifdef DEBUG0
fprintf(stderr, "\npout - output_tx_data: %ld num_tx_data_bytes: %d\n",
pout - output_tx_data, num_tx_data_bytes);
#endif
assert(pout == (output_tx_data + num_tx_data_bytes));
/* optional interleaver - we dont interleave UW */
#ifdef INTERLEAVER
interleave(&output_tx_data[sizeof(uw)], num_tx_data_bytes-2, 0);
#endif
/* optional scrambler to prevent long strings of the same symbol
which upsets the modem - we dont scramble UW */
#ifdef SCRAMBLER
scramble(&output_tx_data[sizeof(uw)], num_tx_data_bytes-2);
#endif
return num_tx_data_bytes;
}
#ifdef HORUS_L2_RX
void horus_l2_decode_rx_packet(unsigned char *output_payload_data,
unsigned char *input_rx_data,
int num_payload_data_bytes)
{
int num_payload_data_bits;
unsigned char *pout = output_payload_data;
unsigned char *pin = input_rx_data;
int ninbit, ingolay, ningolay, paritybyte, nparitybits;
int ninbyte, shift, inbit, golayparitybit, i, outbit, outbyte, noutbits, outdata;
int num_tx_data_bytes = horus_l2_get_num_tx_data_bytes(num_payload_data_bytes);
/* optional scrambler and interleaver - we dont interleave UW */
#ifdef SCRAMBLER
scramble(&input_rx_data[sizeof(uw)], num_tx_data_bytes-2);
#endif
#ifdef INTERLEAVER
interleave(&input_rx_data[sizeof(uw)], num_tx_data_bytes-2, 1);
#endif
pin = input_rx_data + sizeof(uw) + num_payload_data_bytes;
/* Read input data bits one at a time. When we have 12 read 11 parity bits. Golay decode.
Write decoded (output data) bits every time we have 8 of them. */
num_payload_data_bits = num_payload_data_bytes*8;
ninbit = 0;
ingolay = 0;
ningolay = 0;
nparitybits = 0;
paritybyte = *pin++;
#ifdef DEBUG0
fprintf(stderr," Read paritybyte: 0x%02x\n", paritybyte);
#endif
pout = output_payload_data;
noutbits = 0;
outbyte = 0;
while (ninbit < num_payload_data_bits) {
/* extract input data bit */
ninbyte = ninbit/8 + sizeof(uw);
shift = 7 - (ninbit % 8);
inbit = (input_rx_data[ninbyte] >> shift) & 0x1;
#ifdef DEBUG1
fprintf(stderr, "inbit %d ninbyte: %d inbyte: 0x%02x inbit: %d\n",
ninbit, ninbyte, input_rx_data[ninbyte], inbit);
#endif
ninbit++;
/* build up golay codeword */
ingolay = ingolay | inbit;
ningolay++;
ingolay <<= 1;
/* when we get 12 data bits start reading parity bits */
if ((ningolay % 12) == 0) {
#ifdef DEBUG0
fprintf(stderr, " ningolay: %d ingolay: 0x%04x\n", ningolay, ingolay>>1);
#endif
for (i=0; i<11; i++) {
shift = 7 - (nparitybits % 8);
golayparitybit = (paritybyte >> shift) & 0x1;
ingolay |= golayparitybit;
if (i != 10)
ingolay <<=1;
nparitybits++;
if ((nparitybits % 8) == 0) {
/* OK grab a new byte */
paritybyte = *pin++;
#ifdef DEBUG0
fprintf(stderr," Read paritybyte: 0x%02x\n", paritybyte);
#endif
}
}
#ifdef DEBUG0
fprintf(stderr, " golay code word: 0x%04x\n", ingolay);
fprintf(stderr, " golay decode...: 0x%04x\n", golay23_decode(ingolay));
#endif
/* write decoded/error corrected bits to output payload data */
outdata = golay23_decode(ingolay) >> 11;
#ifdef DEBUG0
fprintf(stderr, " outdata...: 0x%04x\n", outdata);
#endif
for(i=0; i<12; i++) {
shift = 11 - i;
outbit = (outdata >> shift) & 0x1;
outbyte |= outbit;
noutbits++;
if (noutbits % 8) {
outbyte <<= 1;
}
else {
#ifdef DEBUG0
fprintf(stderr, " output payload byte: 0x%02x\n", outbyte);
#endif
*pout++ = outbyte;
outbyte = 0;
}
}
ingolay = 0;
}
} /* while(.... */
#ifdef DEBUG0
fprintf(stderr, "finishing up .....\n");
#endif
/* Complete final Golay decode */
int golayparity = 0;
if (ningolay % 12) {
for (i=0; i<11; i++) {
shift = 7 - (nparitybits % 8);
golayparitybit = (paritybyte >> shift) & 0x1;
golayparity |= golayparitybit;
if (i != 10)
golayparity <<=1;
nparitybits++;
if ((nparitybits % 8) == 0) {
/* OK grab a new byte */
paritybyte = *pin++;
#ifdef DEBUG0
fprintf(stderr," Read paritybyte: 0x%02x\n", paritybyte);
#endif
}
}
ingolay >>= 1;
int codeword = (ingolay<<12) + golayparity;
#ifdef DEBUG0
fprintf(stderr, " ningolay: %d ingolay: 0x%04x\n", ningolay, ingolay);
fprintf(stderr, " golay code word: 0x%04x\n", codeword);
fprintf(stderr, " golay decode...: 0x%04x\n", golay23_decode(codeword));
#endif
outdata = golay23_decode(codeword) >> 11;
#ifdef DEBUG0
fprintf(stderr, " outdata...: 0x%04x\n", outdata);
fprintf(stderr, " num_payload_data_bits: %d noutbits: %d\n", num_payload_data_bits, noutbits);
#endif
/* write final byte */
int ntogo = num_payload_data_bits - noutbits;
for(i=0; i<ntogo; i++) {
shift = ntogo - i;
outbit = (outdata >> shift) & 0x1;
outbyte |= outbit;
noutbits++;
if (noutbits % 8) {
outbyte <<= 1;
}
else {
#ifdef DEBUG0
fprintf(stderr, " output payload byte: 0x%02x\n", outbyte);
#endif
*pout++ = outbyte;
outbyte = 0;
}
}
}
#ifdef DEBUG0
fprintf(stderr, "\npin - output_payload_data: %ld num_payload_data_bytes: %d\n",
pout - output_payload_data, num_payload_data_bytes);
#endif
assert(pout == (output_payload_data + num_payload_data_bytes));
}
#endif
#ifdef INTERLEAVER
uint16_t primes[] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347
};
void interleave(unsigned char *inout, int nbytes, int dir)
{
uint16_t nbits = (uint16_t)nbytes*8;
uint32_t i, j, n, ibit, ibyte, ishift, jbyte, jshift;
uint32_t b;
unsigned char out[nbytes];
memset(out, 0, nbytes);
/* b chosen to be co-prime with nbits, I'm cheating by just finding the
nearest prime to nbits. It also uses storage, is run on every call,
and has an upper limit. Oh Well, still seems to interleave OK. */
i = 1;
uint16_t imax = sizeof(primes)/sizeof(uint16_t);
while ((primes[i] < nbits) && (i < imax))
i++;
b = primes[i-1];
for(n=0; n<nbits; n++) {
/*
"On the Analysis and Design of Good Algebraic Interleavers", Xie et al,eq (5)
*/
i = n;
j = (b*i) % nbits;
if (dir) {
uint16_t tmp = j;
j = i;
i = tmp;
}
#ifdef DEBUG0
printf("i: %d j: %d\n",i, j);
#endif
/* read bit i and write to bit j postion */
ibyte = i/8;
ishift = i%8;
ibit = (inout[ibyte] >> ishift) & 0x1;
jbyte = j/8;
jshift = j%8;
/* write jbit to ibit position */
out[jbyte] |= ibit << jshift; // replace with i-th bit
//out[ibyte] |= ibit << ishift; // replace with i-th bit
}
memcpy(inout, out, nbytes);
#ifdef DEBUG0
printf("\nInterleaver Out:\n");
for (i=0; i<nbytes; i++)
printf("%02d 0x%02x\n", i, inout[i]);
#endif
}
#endif
#ifdef TEST_INTERLEAVER
int main(void) {
int nbytes = 43;
unsigned char inout[nbytes];
unsigned char inter[nbytes];
unsigned char incopy[nbytes];
int i;
/* copy of input for later comp */
for(i=0; i<nbytes; i++)
inout[i] = incopy[i] = rand() & 0xff;
interleave(inout, nbytes, 0); /* interleave */
memcpy(inter, inout, nbytes); /* snap shot of interleaved bytes */
interleave(inout, nbytes, 1); /* de-interleave */
/* all ones in last col means it worked! */
for(i=0; i<nbytes; i++) {
printf("%d 0x%02x 0x%02x 0x%02x %d\n",
i, incopy[i], inter[i], inout[i], incopy[i] == inout[i]);
assert(incopy[i] == inout[i]);
}
printf("Interleaver tested OK!\n");
return 0;
}
#endif
#ifdef SCRAMBLER
/* 16 bit DVB additive scrambler as per Wikpedia example */
void scramble(unsigned char *inout, int nbytes)
{
int nbits = nbytes*8;
int i, ibit, ibits, ibyte, ishift, mask;
uint16_t scrambler = 0x4a80; /* init additive scrambler at start of every frame */
uint16_t scrambler_out;
/* in place modification of each bit */
for(i=0; i<nbits; i++) {
scrambler_out = ((scrambler & 0x2) >> 1) ^ (scrambler & 0x1);
/* modify i-th bit by xor-ing with scrambler output sequence */
ibyte = i/8;
ishift = i%8;
ibit = (inout[ibyte] >> ishift) & 0x1;
ibits = ibit ^ scrambler_out; // xor ibit with scrambler output
mask = 1 << ishift;
inout[ibyte] &= ~mask; // clear i-th bit
inout[ibyte] |= ibits << ishift; // set to scrambled value
/* update scrambler */
scrambler >>= 1;
scrambler |= scrambler_out << 14;
#ifdef DEBUG0
printf("i: %02d ibyte: %d ishift: %d ibit: %d ibits: %d scrambler_out: %d\n",
i, ibyte, ishift, ibit, ibits, scrambler_out);
#endif
}
#ifdef DEBUG0
printf("\nScrambler Out:\n");
for (i=0; i<nbytes; i++)
printf("%02d 0x%02x\n", i, inout[i]);
#endif
}
#endif
#ifdef HORUS_L2_UNITTEST
/*
Test function to construct a packet of payload data, encode, add
some bit errors, decode, count errors.
*/
int test_sending_bytes(int nbytes, float ber, int error_pattern) {
unsigned char input_payload[nbytes];
int num_tx_data_bytes = horus_l2_get_num_tx_data_bytes(sizeof(input_payload));
unsigned char tx[num_tx_data_bytes];
unsigned char output_payload[sizeof(input_payload)];
int b, nbiterrors = 0;
int i;
for(i=0; i<nbytes; i++)
input_payload[i] = i;
horus_l2_encode_tx_packet(tx, input_payload, sizeof(input_payload));
#ifdef DEBUG0
fprintf(stderr, "\nTx Data:\n");
for(i=0; i<num_tx_data_bytes; i++)
fprintf(stderr, " %02d 0x%02x\n", i, tx[i]);
#endif
/* insert random bit errors */
if (error_pattern == 0) {
float r;
for(i=0; i<num_tx_data_bytes; i++) {
for (b=0; b<8; b++) {
r = (float)rand()/RAND_MAX;
if (r < ber) {
unsigned char mask = (1<<b);
#ifdef DEBUG1
fprintf("mask: 0x%x tx[%d] = 0x%x ", mask, i, tx[i]);
#endif
tx[i] ^= mask;
#ifdef DEBUG1
fprintf("0x%x\n", tx[i]);
#endif
nbiterrors++;
}
}
}
}
/* insert and error burst */
if (error_pattern == 1) {
tx[2] ^= 0xff;
tx[3] ^= 0xff;
}
/* insert 1 error every 12 bits, this gives up to 3 errors per 23
bit codeword, which is the limit of the code */
if (error_pattern == 2) {
int bn = 0;
for(i=0; i<num_tx_data_bytes; i++) {
for (b=0; b<8; b++) {
bn++;
if ((bn % 12) == 0) {
unsigned char mask = (1<<(7-b));
#ifdef DEBUG1
fprintf("mask: 0x%x tx[%d] = 0x%x ", mask, i, tx[i]);
#endif
tx[i] ^= mask;
#ifdef DEBUG1
fprintf("0x%x\n", tx[i]);
#endif
nbiterrors++;
}
}
}
}
#ifdef DEBUG0
fprintf(stderr, "\nTx Data after errors:\n");
for(i=0; i<num_tx_data_bytes; i++)
fprintf(stderr, " %02d 0x%02x\n", i, tx[i]);
#endif
#ifdef DEBUG0
fprintf(stderr, "nbiterrors: %d BER: %3.2f\n", nbiterrors, (float)nbiterrors/(num_tx_data_bytes*8));
#endif
golay23_init();
horus_l2_decode_rx_packet(output_payload, tx, sizeof(input_payload));
#ifdef DEBUG0
fprintf(stderr, "\nOutput Payload:\n");
for(i=0; i<sizeof(input_payload); i++)
fprintf(stderr, " %02d 0x%02x\n", i, output_payload[i]);
#endif
/* count bit errors */
int nerr = 0;
for(i=0; i<nbytes; i++) {
int error_pattern = input_payload[i] ^ output_payload[i];
for(b=0; b<8; b++)
nerr += (error_pattern>>b) & 0x1;
}
return nerr;
}
/* unit test designed to run on a PC */
/* Horus binary packet */
struct TBinaryPacket
{
uint8_t PayloadID;
uint16_t Counter;
uint8_t Hours;
uint8_t Minutes;
uint8_t Seconds;
float Latitude;
float Longitude;
uint16_t Altitude;
uint8_t Speed; // Speed in Knots (1-255 knots)
uint8_t Sats;
int8_t Temp; // Twos Complement Temp value.
uint8_t BattVoltage; // 0 = 0.5v, 255 = 2.0V, linear steps in-between.
uint16_t Checksum; // CRC16-CCITT Checksum.
} __attribute__ ((packed));
int main(void) {
int nbytes = sizeof(struct TBinaryPacket);
printf("test 0: BER: 0.00 ...........: %d\n", test_sending_bytes(nbytes, 0.00, 0));
printf("test 1: BER: 0.01 ...........: %d\n", test_sending_bytes(nbytes, 0.01, 0));
printf("test 2: BER: 0.05 ...........: %d\n", test_sending_bytes(nbytes, 0.05, 0));
/* we expect this always to fail, as chance of > 3 errors/codeword is high */
printf("test 3: BER: 0.10 ...........: %d\n", test_sending_bytes(nbytes, 0.10, 0));
/* -DINTERLEAVER will make this puppy pass */
printf("test 4: 8 bit burst error....: %d\n", test_sending_bytes(nbytes, 0.00, 1));
/* Insert 2 errors in every codeword, the maximum correction
capability of a Golay (23,12) code. note this one will fail
with -DINTERLEAVER, as we can't guarantee <= 3 errors per
codeword after interleaving */
printf("test 5: 1 error every 12 bits: %d\n", test_sending_bytes(nbytes, 0.00, 2));
return 0;
}
#endif
#ifdef GEN_TX_BITS
/* generate a file of tx_bits to modulate using fsk_horus.m for modem simulations */
int main(void) {
int nbytes = sizeof(struct TBinaryPacket);
struct TBinaryPacket input_payload;
int num_tx_data_bytes = horus_l2_get_num_tx_data_bytes(nbytes);
unsigned char tx[num_tx_data_bytes];
int i;
/* all zeros is nastiest sequence for demod before scrambling */
memset(&input_payload, 0, nbytes);
input_payload.Checksum = gen_crc16((unsigned char*)&input_payload, nbytes-2);
horus_l2_encode_tx_packet(tx, (unsigned char*)&input_payload, nbytes);
FILE *f = fopen("../octave/horus_tx_bits_binary.txt","wt");
assert(f != NULL);
int b, tx_bit;
for(i=0; i<num_tx_data_bytes; i++) {
for(b=0; b<8; b++) {
tx_bit = (tx[i] >> (7-b)) & 0x1; /* msb first */
fprintf(f,"%d ", tx_bit);
}
}
fclose(f);
return 0;
}
#endif
#ifdef DEC_RX_BITS
/* Decode a binary file rx_bytes, e.g. from fsk_horus.m */
int main(void) {
int nbytes = 22;
unsigned char output_payload[nbytes];
int num_tx_data_bytes = horus_l2_get_num_tx_data_bytes(nbytes);
/* real world data horus payload generated when running tx above */
unsigned char rx[45] = {
0x24,0x24,0x01,0x0b,0x00,0x00,0x05,0x3b,0xf2,0xa7,0x0b,0xc2,0x1b,
0xaa,0x0a,0x43,0x7e,0x00,0x05,0x00,0x25,0xc0,0xce,0xbb,0x36,0x69,
0x50,0x00,0x41,0xb0,0xa6,0x5e,0x91,0xa2,0xa3,0xf8,0x1d,0x00,0x00,
0x0c,0x76,0xc6,0x05,0xb0,0xb8};
int i, ret;
assert(num_tx_data_bytes == 45);
#define READ_FILE /* overwrite tx[] above, that's OK */
#ifdef READ_FILE
FILE *f = fopen("../octave/horus_rx_bits_binary.bin","rb");
assert(f != NULL);
ret = fread(rx, sizeof(char), num_tx_data_bytes, f);
assert(ret == num_tx_data_bytes);
fclose(f);
#endif
golay23_init();
horus_l2_decode_rx_packet(output_payload, rx, nbytes);
#ifdef HEX_DUMP
fprintf(stderr, "\nOutput Payload:\n");
for(i=0; i<nbytes; i++)
fprintf(stderr, " %02d 0x%02x 0x%02x\n", i, output_payload[i], rx[i+2]);
#endif
struct TBinaryPacket h;
assert(sizeof(h) == nbytes);
memcpy(&h, output_payload, nbytes);
uint16_t crc_rx = gen_crc16(output_payload, nbytes-2);
char crc_str[80];
if (crc_rx == h.Checksum) {
sprintf(crc_str, "CRC OK");
} else {
sprintf(crc_str, "CRC BAD");
}
fprintf(stderr, "%d,%d,%02d:%02d:%02d,%f,%f,%d,%d,%d,%d,%d,%04x %s\n",
h.PayloadID, h.Counter, h.Hours, h.Minutes, h.Seconds,
h.Latitude, h.Longitude, h.Altitude, h.Speed, h.Sats, h.Temp,
h.BattVoltage, h.Checksum, crc_str);
/* Hex ASCII file output */
#define WRITE_HEX_FILE /* overwrite tx[] above, that's OK */
#ifdef WRITE_HEX_FILE
FILE *fh = fopen("../octave/horus_rx_bits_hex.txt","wt");
assert(fh != NULL);
for(i=0; i<nbytes; i++) {
fprintf(fh, "%02X", (unsigned int)output_payload[i]);
}
fclose(fh);
#endif
return 0;
}
#endif
/*---------------------------------------------------------------------------*\
GOLAY FUNCTIONS
\*---------------------------------------------------------------------------*/
/* File: golay23.c
* Title: Encoder/decoder for a binary (23,12,7) Golay code
* Author: Robert Morelos-Zaragoza (robert@spectra.eng.hawaii.edu)
* Date: August 1994
*
* The binary (23,12,7) Golay code is an example of a perfect code, that is,
* the number of syndromes equals the number of correctable error patterns.
* The minimum distance is 7, so all error patterns of Hamming weight up to
* 3 can be corrected. The total number of these error patterns is:
*
* Number of errors Number of patterns
* ---------------- ------------------
* 0 1
* 1 23
* 2 253
* 3 1771
* ----
* Total number of error patterns = 2048 = 2^{11} = number of syndromes
* --
* number of redundant bits -------^
*
* Because of its relatively low length (23), dimension (12) and number of
* redundant bits (11), the binary (23,12,7) Golay code can be encoded and
* decoded simply by using look-up tables. The program below uses a 16K
* encoding table and an 8K decoding table.
*
* For more information, suggestions, or other ideas on implementing error
* correcting codes, please contact me at (I'm temporarily in Japan, but
* below is my U.S. address):
*
* Robert Morelos-Zaragoza
* 770 S. Post Oak Ln. #200
* Houston, Texas 77056
*
* email: robert@spectra.eng.hawaii.edu
*
* Homework: Add an overall parity-check bit to get the (24,12,8)
* extended Golay code.
*
* COPYRIGHT NOTICE: This computer program is free for non-commercial purposes.
* You may implement this program for any non-commercial application. You may
* also implement this program for commercial purposes, provided that you
* obtain my written permission. Any modification of this program is covered
* by this copyright.
*
* == Copyright (c) 1994 Robert Morelos-Zaragoza. All rights reserved. ==
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define X22 0x00400000 /* vector representation of X^{22} */
#define X11 0x00000800 /* vector representation of X^{11} */
#define MASK12 0xfffff800 /* auxiliary vector for testing */
#define GENPOL 0x00000c75 /* generator polinomial, g(x) */
/* Global variables:
*
* pattern = error pattern, or information, or received vector
* encoding_table[] = encoding table
* decoding_table[] = decoding table
* data = information bits, i(x)
* codeword = code bits = x^{11}i(x) + (x^{11}i(x) mod g(x))
* numerr = number of errors = Hamming weight of error polynomial e(x)
* position[] = error positions in the vector representation of e(x)
* recd = representation of corrupted received polynomial r(x) = c(x) + e(x)
* decerror = number of decoding errors
* a[] = auxiliary array to generate correctable error patterns
*/
#ifdef HORUS_L2_RX
static int inited = 0;
#ifdef RUN_TIME_TABLES
static int encoding_table[4096], decoding_table[2048];
#else
#include "golayenctable.h"
#include "golaydectable.h"
#endif
#ifdef RUN_TIME_TABLES
static int arr2int(int a[], int r)
/*
* Convert a binary vector of Hamming weight r, and nonzero positions in
* array a[1]...a[r], to a long integer \sum_{i=1}^r 2^{a[i]-1}.
*/
{
int i;
long mul, result = 0, temp;
for (i=1; i<=r; i++) {
mul = 1;
temp = a[i]-1;
while (temp--)
mul = mul << 1;
result += mul;
}
return(result);
}
#endif
#endif
#ifdef HORUS_L2_RX
void nextcomb(int n, int r, int a[])
/*
* Calculate next r-combination of an n-set.
*/
{
int i, j;