-
Notifications
You must be signed in to change notification settings - Fork 20
/
ft8.py
executable file
·5382 lines (4562 loc) · 174 KB
/
ft8.py
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
#!/usr/local/bin/python
#
# encode and decode FT8.
#
# for new 77-bit FT8.
# LDPC tables and pack/unpack details from wsjt-x 2.0.
#
# Robert Morris, AB1HL
#
import numpy
import wave
import scipy
import scipy.signal
import sys
import os
import math
import time
import multiprocessing
import threading
import re
import random
import ctypes
import weakaudio
import weakutil
#
# tuning parameters.
#
budget = 2.2 # max seconds of time for decoding.
pass0_fstep = 2 # coarse search granularity, per FFT bin
pass0_tstep = 2 # coarse search granularity, per symbol time
passN_fstep = 2 # coarse search granularity, per FFT bin
passN_tstep = 4 # coarse search granularity, per symbol time
pass0_tminus = 2.2 # start search this many seconds before 0.5
pass0_tplus = 2.7 # end search this many seconds after 0.5
passN_tminus = 1.5
passN_tplus = 1.8
coarse_no = 1 # number of best offsets to use per hz
fine_no = 1 # number of best fine offsets to look at
fine_fstep = 4 # fine-tuning steps per coarse_fstep
fine_tstep = 8 # fine-tuning steps per coarse_tstep
start_adj = 0.1 # signals seem on avg to start this many seconds late.
ldpc_iters = 30 # how hard LDPC should work on pass 0
softboost = 1.0 # log(prob) if #2 symbol has same bit value
do_subtract = 1 # 0 none, 1 once per unique decode, 2 three per unique, 3 once per decode
subgap = 0.8 # extra subtract()s this many hz on either side of main bin
substeps = 16 # subtract phase steps, in 2pi
subpasses = 2 # 0 means no subtraction, 1 means subtract, 2 means another subtraction pass
pass0_frac = 1.0
pass0_hints = True # hints in pass 0 (as well as later passes)?
contrast_weight = 0.5
noise_factor = 0.8 # for strength()
top_high_order = 0 # 0 for cheby, 19 for butter
high_cutoff = 1.05
low_pass_order = 0 # 15
top_down = True
bottom_slow = True
osd_crc = False # True means OSD only accepts if CRC is correct
osd0_crc = True # True means OSD accepts if depth=0 CRC is correct
osd_depth = 6
osd_thresh = -500
already_o = 1
already_f = 1
down200 = False # process1() that down-converts to 200 hz / 32 samples/symbol
use_apriori = True
nchildren = 4
child_overlap = 30
osd_no_snr = False
padfactor = 0.1 # quiet-ish before/after padding
osd_hints = False # use OSD on hints BUT causes lots of false pseudo-juicy CQs!
down_cutoff = 0.45 # low-pass filter cutoff before down-sampling
cheb_cut1 = 0.48
cheb_cut2 = 0.61
cheb_ripple_pass = 0.5
cheb_atten_stop = 50
cheb_high_minus = 40
cheb_high_plus = 60
hint_tol = 9 # look for CQ XXX hints in this +/- hz range of where heard
crc_and_83 = True # True means require both CRC and LDPC
ldpc_thresh = 83 # 83 means all LDPC check-bits must be correct
snr_overlap = 3 # -1 means don't convert to snr, 0 means each sym time by itself
snr_wintype = "blackman"
real_min_hz = 150
real_max_hz = 2900
sub_amp_win = 2
adjust_hz_for_sub = True
adjust_off_for_sub = True
yes_mul = 1.0
yes_add = 0.0
no_mul = 1.0
no_add = 0.0
soft1 = 7
soft2 = 8
soft3 = 4
soft4 = 6
guard200 = 10
order200 = 5
strength_div = 4.0
decimate_order = 8
# FT8 modulation and protocol definitions.
# 1920-point FFT at 12000 samples/second
# yields 6.25 Hz spacing, 0.16 seconds/symbol
# encode chain:
# 77 bits
# append 14 bits CRC (for 91 bits)
# LDPC(174,91) yields 174 bits
# that's 58 3-bit FSK-8 symbols
# gray code each 3 bits
# insert three 7-symbol Costas sync arrays
# at symbol #s 0, 36, 72 of final signal
# thus: 79 FSK-8 symbols
# total transmission time is 12.64 seconds
costas_symbols = [ 3, 1, 4, 0, 6, 5, 2 ] # new FT8
# gray map for encoding 3-bit chunks of the 174 bits,
# after LDPC and before generating FSK-8 tones.
graymap = [ 0, 1, 3, 2, 5, 6, 4, 7 ]
# the CRC-14 polynomial, from wsjt-x's 0x2757,
# with leading 1 bit.
crc14poly = [ 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1 ]
def crc_c(msg):
msgtype = ctypes.c_int * len(msg)
outtype = ctypes.c_int * 14
msg1 = msgtype()
for i in range(0, len(msg)):
msg1[i] = msg[i]
out1 = outtype()
libldpc.ft8_crc(msg1, len(msg), out1)
out = numpy.zeros(14, dtype=numpy.int32)
for i in range(0, 14):
out[i] = out1[i]
return out
#
# thank you, evan sneath.
# https://gist.github.com/evansneath/4650991
#
# generate with x^3 + x + 1:
# >>> xc.crc([1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0], [1, 0, 1, 1])
# array([1, 0, 0])
# check:
# >>> xc.crc([1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0], [1, 0, 1, 1], [1, 0, 0])
# array([0, 0, 0])
#
# 0xc06 is really 0x1c06 or [ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 ]
#
def crc_python(msg, div, code=None):
"""Cyclic Redundancy Check
Generates an error detecting code based on an inputted message
and divisor in the form of a polynomial representation.
Arguments:
msg: The input message of which to generate the output code.
div: The divisor in polynomial form. For example, if the polynomial
of x^3 + x + 1 is given, this should be represented as '1011' in
the div argument.
code: This is an option argument where a previously generated code may
be passed in. This can be used to check validity. If the inputted
code produces an outputted code of all zeros, then the message has
no errors.
Returns:
An error-detecting code generated by the message and the given divisor.
"""
# Append the code to the message. If no code is given, default to '000'
if code is None:
code = numpy.zeros(len(div)-1, dtype=numpy.int32)
assert len(code) == len(div) - 1
msg = numpy.append(msg, code)
div = numpy.array(div, dtype=numpy.int32)
divlen = len(div)
# Loop over every message bit (minus the appended code)
for i in range(len(msg)-len(code)):
# If that messsage bit is 1, perform modulo 2 multiplication
if msg[i] == 1:
#for j in range(len(div)):
# # Perform modulo 2 multiplication on each index of the divisor
# msg[i+j] = (msg[i+j] + div[j]) % 2
msg[i:i+divlen] = numpy.mod(msg[i:i+divlen] + div, 2)
# Output the last error-checking code portion of the message generated
return msg[-len(code):]
def crc(msg, div):
if True:
return crc_c(msg)
else:
return crc_python(msg, div)
def check_crc(a91):
padded = numpy.append(a91[0:77], numpy.zeros(5, dtype=numpy.int32))
cksum = crc(padded, crc14poly)
if numpy.array_equal(cksum, a91[-14:]) == False:
# CRC failed.
return False
return True
# this is the LDPC(174,91) parity check matrix.
# each row describes one parity check.
# each number is an index into the codeword (1-origin).
# the codeword bits mentioned in each row must xor to zero.
# From WSJT-X's ldpc_174_91_c_reordered_parity.f90
Nm = [
[ 4, 31, 59, 91, 92, 96, 153 ],
[ 5, 32, 60, 93, 115, 146, 0 ],
[ 6, 24, 61, 94, 122, 151, 0 ],
[ 7, 33, 62, 95, 96, 143, 0 ],
[ 8, 25, 63, 83, 93, 96, 148 ],
[ 6, 32, 64, 97, 126, 138, 0 ],
[ 5, 34, 65, 78, 98, 107, 154 ],
[ 9, 35, 66, 99, 139, 146, 0 ],
[ 10, 36, 67, 100, 107, 126, 0 ],
[ 11, 37, 67, 87, 101, 139, 158 ],
[ 12, 38, 68, 102, 105, 155, 0 ],
[ 13, 39, 69, 103, 149, 162, 0 ],
[ 8, 40, 70, 82, 104, 114, 145 ],
[ 14, 41, 71, 88, 102, 123, 156 ],
[ 15, 42, 59, 106, 123, 159, 0 ],
[ 1, 33, 72, 106, 107, 157, 0 ],
[ 16, 43, 73, 108, 141, 160, 0 ],
[ 17, 37, 74, 81, 109, 131, 154 ],
[ 11, 44, 75, 110, 121, 166, 0 ],
[ 45, 55, 64, 111, 130, 161, 173 ],
[ 8, 46, 71, 112, 119, 166, 0 ],
[ 18, 36, 76, 89, 113, 114, 143 ],
[ 19, 38, 77, 104, 116, 163, 0 ],
[ 20, 47, 70, 92, 138, 165, 0 ],
[ 2, 48, 74, 113, 128, 160, 0 ],
[ 21, 45, 78, 83, 117, 121, 151 ],
[ 22, 47, 58, 118, 127, 164, 0 ],
[ 16, 39, 62, 112, 134, 158, 0 ],
[ 23, 43, 79, 120, 131, 145, 0 ],
[ 19, 35, 59, 73, 110, 125, 161 ],
[ 20, 36, 63, 94, 136, 161, 0 ],
[ 14, 31, 79, 98, 132, 164, 0 ],
[ 3, 44, 80, 124, 127, 169, 0 ],
[ 19, 46, 81, 117, 135, 167, 0 ],
[ 7, 49, 58, 90, 100, 105, 168 ],
[ 12, 50, 61, 118, 119, 144, 0 ],
[ 13, 51, 64, 114, 118, 157, 0 ],
[ 24, 52, 76, 129, 148, 149, 0 ],
[ 25, 53, 69, 90, 101, 130, 156 ],
[ 20, 46, 65, 80, 120, 140, 170 ],
[ 21, 54, 77, 100, 140, 171, 0 ],
[ 35, 82, 133, 142, 171, 174, 0 ],
[ 14, 30, 83, 113, 125, 170, 0 ],
[ 4, 29, 68, 120, 134, 173, 0 ],
[ 1, 4, 52, 57, 86, 136, 152 ],
[ 26, 51, 56, 91, 122, 137, 168 ],
[ 52, 84, 110, 115, 145, 168, 0 ],
[ 7, 50, 81, 99, 132, 173, 0 ],
[ 23, 55, 67, 95, 172, 174, 0 ],
[ 26, 41, 77, 109, 141, 148, 0 ],
[ 2, 27, 41, 61, 62, 115, 133 ],
[ 27, 40, 56, 124, 125, 126, 0 ],
[ 18, 49, 55, 124, 141, 167, 0 ],
[ 6, 33, 85, 108, 116, 156, 0 ],
[ 28, 48, 70, 85, 105, 129, 158 ],
[ 9, 54, 63, 131, 147, 155, 0 ],
[ 22, 53, 68, 109, 121, 174, 0 ],
[ 3, 13, 48, 78, 95, 123, 0 ],
[ 31, 69, 133, 150, 155, 169, 0 ],
[ 12, 43, 66, 89, 97, 135, 159 ],
[ 5, 39, 75, 102, 136, 167, 0 ],
[ 2, 54, 86, 101, 135, 164, 0 ],
[ 15, 56, 87, 108, 119, 171, 0 ],
[ 10, 44, 82, 91, 111, 144, 149 ],
[ 23, 34, 71, 94, 127, 153, 0 ],
[ 11, 49, 88, 92, 142, 157, 0 ],
[ 29, 34, 87, 97, 147, 162, 0 ],
[ 30, 50, 60, 86, 137, 142, 162 ],
[ 10, 53, 66, 84, 112, 128, 165 ],
[ 22, 57, 85, 93, 140, 159, 0 ],
[ 28, 32, 72, 103, 132, 166, 0 ],
[ 28, 29, 84, 88, 117, 143, 150 ],
[ 1, 26, 45, 80, 128, 147, 0 ],
[ 17, 27, 89, 103, 116, 153, 0 ],
[ 51, 57, 98, 163, 165, 172, 0 ],
[ 21, 37, 73, 138, 152, 169, 0 ],
[ 16, 47, 76, 130, 137, 154, 0 ],
[ 3, 24, 30, 72, 104, 139, 0 ],
[ 9, 40, 90, 106, 134, 151, 0 ],
[ 15, 58, 60, 74, 111, 150, 163 ],
[ 18, 42, 79, 144, 146, 152, 0 ],
[ 25, 38, 65, 99, 122, 160, 0 ],
[ 17, 42, 75, 129, 170, 172, 0 ],
]
# Mn from WSJT-X's ldpc_174_91_c_reordered_parity.f90
# each of the 174 rows corresponds to a codeword bit.
# the numbers indicate which three parity
# checks (rows in Nm) refer to the codeword bit.
# 1-origin.
Mn = [
[ 16, 45, 73 ],
[ 25, 51, 62 ],
[ 33, 58, 78 ],
[ 1, 44, 45 ],
[ 2, 7, 61 ],
[ 3, 6, 54 ],
[ 4, 35, 48 ],
[ 5, 13, 21 ],
[ 8, 56, 79 ],
[ 9, 64, 69 ],
[ 10, 19, 66 ],
[ 11, 36, 60 ],
[ 12, 37, 58 ],
[ 14, 32, 43 ],
[ 15, 63, 80 ],
[ 17, 28, 77 ],
[ 18, 74, 83 ],
[ 22, 53, 81 ],
[ 23, 30, 34 ],
[ 24, 31, 40 ],
[ 26, 41, 76 ],
[ 27, 57, 70 ],
[ 29, 49, 65 ],
[ 3, 38, 78 ],
[ 5, 39, 82 ],
[ 46, 50, 73 ],
[ 51, 52, 74 ],
[ 55, 71, 72 ],
[ 44, 67, 72 ],
[ 43, 68, 78 ],
[ 1, 32, 59 ],
[ 2, 6, 71 ],
[ 4, 16, 54 ],
[ 7, 65, 67 ],
[ 8, 30, 42 ],
[ 9, 22, 31 ],
[ 10, 18, 76 ],
[ 11, 23, 82 ],
[ 12, 28, 61 ],
[ 13, 52, 79 ],
[ 14, 50, 51 ],
[ 15, 81, 83 ],
[ 17, 29, 60 ],
[ 19, 33, 64 ],
[ 20, 26, 73 ],
[ 21, 34, 40 ],
[ 24, 27, 77 ],
[ 25, 55, 58 ],
[ 35, 53, 66 ],
[ 36, 48, 68 ],
[ 37, 46, 75 ],
[ 38, 45, 47 ],
[ 39, 57, 69 ],
[ 41, 56, 62 ],
[ 20, 49, 53 ],
[ 46, 52, 63 ],
[ 45, 70, 75 ],
[ 27, 35, 80 ],
[ 1, 15, 30 ],
[ 2, 68, 80 ],
[ 3, 36, 51 ],
[ 4, 28, 51 ],
[ 5, 31, 56 ],
[ 6, 20, 37 ],
[ 7, 40, 82 ],
[ 8, 60, 69 ],
[ 9, 10, 49 ],
[ 11, 44, 57 ],
[ 12, 39, 59 ],
[ 13, 24, 55 ],
[ 14, 21, 65 ],
[ 16, 71, 78 ],
[ 17, 30, 76 ],
[ 18, 25, 80 ],
[ 19, 61, 83 ],
[ 22, 38, 77 ],
[ 23, 41, 50 ],
[ 7, 26, 58 ],
[ 29, 32, 81 ],
[ 33, 40, 73 ],
[ 18, 34, 48 ],
[ 13, 42, 64 ],
[ 5, 26, 43 ],
[ 47, 69, 72 ],
[ 54, 55, 70 ],
[ 45, 62, 68 ],
[ 10, 63, 67 ],
[ 14, 66, 72 ],
[ 22, 60, 74 ],
[ 35, 39, 79 ],
[ 1, 46, 64 ],
[ 1, 24, 66 ],
[ 2, 5, 70 ],
[ 3, 31, 65 ],
[ 4, 49, 58 ],
[ 1, 4, 5 ],
[ 6, 60, 67 ],
[ 7, 32, 75 ],
[ 8, 48, 82 ],
[ 9, 35, 41 ],
[ 10, 39, 62 ],
[ 11, 14, 61 ],
[ 12, 71, 74 ],
[ 13, 23, 78 ],
[ 11, 35, 55 ],
[ 15, 16, 79 ],
[ 7, 9, 16 ],
[ 17, 54, 63 ],
[ 18, 50, 57 ],
[ 19, 30, 47 ],
[ 20, 64, 80 ],
[ 21, 28, 69 ],
[ 22, 25, 43 ],
[ 13, 22, 37 ],
[ 2, 47, 51 ],
[ 23, 54, 74 ],
[ 26, 34, 72 ],
[ 27, 36, 37 ],
[ 21, 36, 63 ],
[ 29, 40, 44 ],
[ 19, 26, 57 ],
[ 3, 46, 82 ],
[ 14, 15, 58 ],
[ 33, 52, 53 ],
[ 30, 43, 52 ],
[ 6, 9, 52 ],
[ 27, 33, 65 ],
[ 25, 69, 73 ],
[ 38, 55, 83 ],
[ 20, 39, 77 ],
[ 18, 29, 56 ],
[ 32, 48, 71 ],
[ 42, 51, 59 ],
[ 28, 44, 79 ],
[ 34, 60, 62 ],
[ 31, 45, 61 ],
[ 46, 68, 77 ],
[ 6, 24, 76 ],
[ 8, 10, 78 ],
[ 40, 41, 70 ],
[ 17, 50, 53 ],
[ 42, 66, 68 ],
[ 4, 22, 72 ],
[ 36, 64, 81 ],
[ 13, 29, 47 ],
[ 2, 8, 81 ],
[ 56, 67, 73 ],
[ 5, 38, 50 ],
[ 12, 38, 64 ],
[ 59, 72, 80 ],
[ 3, 26, 79 ],
[ 45, 76, 81 ],
[ 1, 65, 74 ],
[ 7, 18, 77 ],
[ 11, 56, 59 ],
[ 14, 39, 54 ],
[ 16, 37, 66 ],
[ 10, 28, 55 ],
[ 15, 60, 70 ],
[ 17, 25, 82 ],
[ 20, 30, 31 ],
[ 12, 67, 68 ],
[ 23, 75, 80 ],
[ 27, 32, 62 ],
[ 24, 69, 75 ],
[ 19, 21, 71 ],
[ 34, 53, 61 ],
[ 35, 46, 47 ],
[ 33, 59, 76 ],
[ 40, 43, 83 ],
[ 41, 42, 63 ],
[ 49, 75, 83 ],
[ 20, 44, 48 ],
[ 42, 49, 57 ],
]
#
# LDPC generator matrix from WSJT-X's ldpc_174_91_c_generator.f90.
# 83 rows, since LDPC(174,91) needs 83 parity bits.
# each row has 23 hex digits, to be turned into 91 bits,
# to be xor'd with the 91 data bits.
#
rawg = [
"8329ce11bf31eaf509f27fc",
"761c264e25c259335493132",
"dc265902fb277c6410a1bdc",
"1b3f417858cd2dd33ec7f62",
"09fda4fee04195fd034783a",
"077cccc11b8873ed5c3d48a",
"29b62afe3ca036f4fe1a9da",
"6054faf5f35d96d3b0c8c3e",
"e20798e4310eed27884ae90",
"775c9c08e80e26ddae56318",
"b0b811028c2bf997213487c",
"18a0c9231fc60adf5c5ea32",
"76471e8302a0721e01b12b8",
"ffbccb80ca8341fafb47b2e",
"66a72a158f9325a2bf67170",
"c4243689fe85b1c51363a18",
"0dff739414d1a1b34b1c270",
"15b48830636c8b99894972e",
"29a89c0d3de81d665489b0e",
"4f126f37fa51cbe61bd6b94",
"99c47239d0d97d3c84e0940",
"1919b75119765621bb4f1e8",
"09db12d731faee0b86df6b8",
"488fc33df43fbdeea4eafb4",
"827423ee40b675f756eb5fe",
"abe197c484cb74757144a9a",
"2b500e4bc0ec5a6d2bdbdd0",
"c474aa53d70218761669360",
"8eba1a13db3390bd6718cec",
"753844673a27782cc42012e",
"06ff83a145c37035a5c1268",
"3b37417858cc2dd33ec3f62",
"9a4a5a28ee17ca9c324842c",
"bc29f465309c977e89610a4",
"2663ae6ddf8b5ce2bb29488",
"46f231efe457034c1814418",
"3fb2ce85abe9b0c72e06fbe",
"de87481f282c153971a0a2e",
"fcd7ccf23c69fa99bba1412",
"f0261447e9490ca8e474cec",
"4410115818196f95cdd7012",
"088fc31df4bfbde2a4eafb4",
"b8fef1b6307729fb0a078c0",
"5afea7acccb77bbc9d99a90",
"49a7016ac653f65ecdc9076",
"1944d085be4e7da8d6cc7d0",
"251f62adc4032f0ee714002",
"56471f8702a0721e00b12b8",
"2b8e4923f2dd51e2d537fa0",
"6b550a40a66f4755de95c26",
"a18ad28d4e27fe92a4f6c84",
"10c2e586388cb82a3d80758",
"ef34a41817ee02133db2eb0",
"7e9c0c54325a9c15836e000",
"3693e572d1fde4cdf079e86",
"bfb2cec5abe1b0c72e07fbe",
"7ee18230c583cccc57d4b08",
"a066cb2fedafc9f52664126",
"bb23725abc47cc5f4cc4cd2",
"ded9dba3bee40c59b5609b4",
"d9a7016ac653e6decdc9036",
"9ad46aed5f707f280ab5fc4",
"e5921c77822587316d7d3c2",
"4f14da8242a8b86dca73352",
"8b8b507ad467d4441df770e",
"22831c9cf1169467ad04b68",
"213b838fe2ae54c38ee7180",
"5d926b6dd71f085181a4e12",
"66ab79d4b29ee6e69509e56",
"958148682d748a38dd68baa",
"b8ce020cf069c32a723ab14",
"f4331d6d461607e95752746",
"6da23ba424b9596133cf9c8",
"a636bcbc7b30c5fbeae67fe",
"5cb0d86a07df654a9089a20",
"f11f106848780fc9ecdd80a",
"1fbb5364fb8d2c9d730d5ba",
"fcb86bc70a50c9d02a5d034",
"a534433029eac15f322e34c",
"c989d9c7c3d3b8c55d75130",
"7bb38b2f0186d46643ae962",
"2644ebadeb44b9467d1f42c",
"608cc857594bfbb55d69600"
]
# gen[row][col], derived from rawg, has one row per
# parity bit, to be xor'd with the 91 data bits.
# thus gen[83][91].
# as in encode174_91.f90
gen = [ ]
# turn rawg into gen.
def make_gen():
global gen
# hex digit to number
hex2 = { }
for i in range(0, 16):
hex2[hex(i)[2]] = i
assert len(rawg) == 83
for e in rawg:
row = numpy.zeros(91, dtype=numpy.int32)
for i,c in enumerate(e):
x = hex2[c]
for j in range(0, 4):
ind = i*4 + (3-j)
if ind >= 0 and ind < 91:
if (x & (1 << j)) != 0:
row[ind] = 1
else:
row[ind] = 0
gen.append(row)
make_gen()
# turn gen[] into a systematic array by prepending
# a 91x91 identity matrix.
gen_sys = numpy.zeros((174, 91), dtype=numpy.int32)
gen_sys[91:,:] = gen
gen_sys[0:91,:] = numpy.eye(91, dtype=numpy.int32)
# plain is 91 bits of plain-text.
# returns a 174-bit codeword.
# mimics wsjt-x's encode174_91.f90.
def ldpc_encode(plain):
assert len(plain) == 91
ncw = numpy.zeros(174, dtype=numpy.int32)
numpy.dot(gen_sys[91:,:], plain, out=ncw[91:])
numpy.mod(ncw[91:], 2, out=ncw[91:])
ncw[0:91] = plain
return ncw
# given a 174-bit codeword as an array of log-likelihood of zero,
# return a 91-bit plain text, or zero-length array.
# this is an implementation of the sum-product algorithm
# from Sarah Johnson's Iterative Error Correction book.
# codeword[i] = log ( P(x=0) / P(x=1) )
# returns [ nok, plain ], where nok is the number of parity
# checks that worked out, should be 83=174-91.
def ldpc_decode_python(codeword, ldpc_iters):
# 174 codeword bits:
# 91 systematic data bits
# 83 parity checks
mnx = numpy.array(Mn, dtype=numpy.int32)
nmx = numpy.array(Nm, dtype=numpy.int32)
# Mji
# each codeword bit i tells each parity check j
# what the bit's log-likelihood of being 0 is
# based on information *other* than from that
# parity check.
m = numpy.zeros((83, 174))
for i in range(0, 174):
for j in range(0, 83):
m[j][i] = codeword[i]
for iter in range(0, ldpc_iters):
# Eji
# each check j tells each codeword bit i the
# log likelihood of the bit being zero based
# on the *other* bits in that check.
e = numpy.zeros((83, 174))
# messages from checks to bits.
# for each parity check
#for j in range(0, 83):
# # for each bit mentioned in this parity check
# for i in Nm[j]:
# if i <= 0:
# continue
# a = 1
# # for each other bit mentioned in this parity check
# for ii in Nm[j]:
# if ii != i:
# a *= math.tanh(m[j][ii-1] / 2.0)
# e[j][i-1] = math.log((1 + a) / (1 - a))
for i in range(0, 7):
a = numpy.ones(83)
for ii in range(0, 7):
if ii != i:
x1 = numpy.tanh(m[range(0, 83), nmx[:,ii]-1] / 2.0)
x2 = numpy.where(numpy.greater(nmx[:,ii], 0.0), x1, 1.0)
a = a * x2
# avoid divide by zero, i.e. a[i]==1.0
# XXX why is a[i] sometimes 1.0?
b = numpy.where(numpy.less(a, 0.99999), a, 0.99)
c = numpy.log((b + 1.0) / (1.0 - b))
# have assign be no-op when nmx[a,b] == 0
d = numpy.where(numpy.equal(nmx[:,i], 0),
e[range(0,83), nmx[:,i]-1],
c)
e[range(0,83), nmx[:,i]-1] = d
# decide if we are done -- compute the corrected codeword,
# see if the parity check succeeds.
# sum the three log likelihoods contributing to each codeword bit.
e0 = e[mnx[:,0]-1, range(0,174)]
e1 = e[mnx[:,1]-1, range(0,174)]
e2 = e[mnx[:,2]-1, range(0,174)]
ll = codeword + e0 + e1 + e2
# log likelihood > 0 => bit=0.
cw = numpy.select( [ ll < 0 ], [ numpy.ones(174, dtype=numpy.int32) ])
if ldpc_check(cw):
# success!
# it's a systematic code, though the plain-text bits are scattered.
# collect them.
decoded = cw[0:91]
return [ 91, decoded ]
# messages from bits to checks.
for j in range(0, 3):
# for each column in Mn.
ll = codeword
if j != 0:
e0 = e[mnx[:,0]-1, range(0,174)]
ll = ll + e0
if j != 1:
e1 = e[mnx[:,1]-1, range(0,174)]
ll = ll + e1
if j != 2:
e2 = e[mnx[:,2]-1, range(0,174)]
ll = ll + e2
m[mnx[:,j]-1, range(0,174)] = ll
# could not decode.
return [ 0, numpy.array([]) ]
# turn log-likelihood bits into hard bits.
# codeword[i] = log ( P(x=0) / P(x=1) )
# so > 0 means bit=0, < 0 means bit=1.
def soft2hard(codeword):
hard = numpy.less(codeword, 0.0)
hard = numpy.array(hard, dtype=numpy.int32) # T/F -> 1/0
two = numpy.array([0, 1], dtype=numpy.int32)
hardword = two[hard]
return hardword
# given a 174-bit codeword as an array of log-likelihood of zero,
# return a 91-bit plain text, or zero-length array.
# this is an implementation of the bit-flipping algorithm
# from Sarah Johnson's Iterative Error Correction book.
# codeword[i] = log ( P(x=0) / P(x=1) )
# returns [ nok, plain ], where nok is the number of parity
# checks that worked out, should be 83=174-91.
def ldpc_decode_flipping(codeword):
cw = soft2hard(codeword)
for iter in range(0,100):
# for each codeword bit,
# count of votes for 0 and 1.
votes = numpy.zeros((len(codeword), 2))
# for each parity check equation.
for e in Nm:
# for each codeword bit mentioned in e.
for bi in e:
if bi == 0:
continue
# value for bi implied by remaining bits.
x = 0
for i in e:
if i != bi:
x ^= cw[i-1]
# the other bits in the equation suggest that
# bi must have value x.
votes[(bi-1),x] += 1
for i in range(0, len(cw)):
if cw[i] == 0 and votes[i][1] > votes[i][0]:
cw[i] = 1
elif cw[i] == 1 and votes[i][0] > votes[i][1]:
cw[i] = 0
if ldpc_check(cw):
# success!
# it's a systematic code; data is first 91 bits.
return [ 91, cw[0:91] ]
return [ 0, numpy.array([]) ]
# does a 174-bit codeword pass the LDPC parity checks?
def ldpc_check(codeword):
for e in Nm:
x = 0
for i in e:
if i != 0:
x ^= codeword[i-1]
if x != 0:
return False
return True
libldpc = None
try:
libldpc = ctypes.cdll.LoadLibrary("libldpc/libldpc.so")
except:
libldpc = None
sys.stderr.write("ft8: using the Python LDPC decoder, not the C decoder.\n")
if False:
# test CRC
# nov 30 2018: crc12 works
# dec 3 2018: crc14 works
msg = numpy.zeros(82, dtype=numpy.int32)
msg[3] = 1
msg[7] = 1
msg[44] = 1
msg[45] = 1
msg[46] = 1
msg[51] = 1
msg[61] = 1
msg[71] = 1
expected = [ 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, ]
cksum = crc_python(msg, crc14poly)
eq = numpy.equal(cksum, numpy.array(expected, dtype=numpy.int32))
assert numpy.all(eq)
cksum = crc_c(msg)
eq = numpy.equal(cksum, numpy.array(expected, dtype=numpy.int32))
assert numpy.all(eq)
sys.exit(1)
def ldpc_test(ldpci):
tt = 0.0
niters = 5000
ok = 0
for iter in range(0, niters):
# ldpc_encode() takes 91 bits.
a91 = numpy.random.randint(0, 2, 91, dtype=numpy.int32)
a174 = ldpc_encode(a91)
if True:
# check that ldpc_encode() generated the right parity bits.
assert ldpc_check(a174)
# turn hard bits into 0.99 vs 0.01 log-likelihood,
# log( P(0) / P(1) )
# log base e.
two = numpy.array([ 4.6, -4.6 ])
ll174 = two[a174]
if True:
# check decode is perfect before wrecking bits.
[ nn, d91 ] = ldpc_decode(ll174, ldpci)
assert numpy.array_equal(a91, d91)
assert nn == 83
# wreck some bits
#for junk in range(0, 70):
# ll174[random.randint(0, len(ll174)-1)] = (random.random() - 0.5) * 4
perm = numpy.random.permutation(len(ll174))
perm = perm[0:70]
for i in perm:
p = random.random()
bit = a174[i]
if random.random() > p:
# flip the bit
bit = 1 - bit
if bit == 0:
p = 0.5 + (p / 2)
else:
p = 0.5 - (p / 2)
ll = math.log(p / (1.0 - p))
ll174[i] = ll
t0 = time.time()
# decode LDPC(174,91)
[ _, d91 ] = ldpc_decode(ll174, ldpci)
t1 = time.time()
tt += t1 - t0
if numpy.array_equal(a91, d91):
ok += 1
print("ldpc_iters %d, success %.2f, %.6f sec/call" % (ldpci,
ok / float(niters),
tt / niters))
# success 0.88
# 0.019423 per call
# but Dec 28 2017
# ldpc_iters 20, success 0.64, 0.000592 sec/call
# ldpc_iters 33, success 0.68, 0.000749 sec/call
# ldpc_iters 37, success 0.68, 0.000806 sec/call
# ldpc_iters 50, success 0.69, 0.000943 sec/call
# ldpc_iters 100, success 0.71, 0.001515 sec/call
# fast_tanh is a bit faster, but has same success as tanh()
# ldpc_decode_python() has about the same success rate
# nov 30 2018, old FT8
# ldpc_iters 15, success 0.60, 0.000383 sec/call
# dec 3 2018, new FT8
# ldpc_iters 15, success 0.43, 0.000341 sec/call
# ldpc_iters 15, success 0.41, 0.014654 sec/call
# codeword is 174 log-likelihoods.
# return is [ ok, 83 bits ].
# ok is 83 if all ldpc parity checks worked, < 83 otherwise.
# result is usually garbage if ok < 83.
def ldpc_decode_c(codeword, ldpc_iters):
double174 = ctypes.c_double * 174
int174 = ctypes.c_int * 174
c174 = double174()
for i in range(0, 174):
c174[i] = codeword[i]
out174 = int174()
for i in range(0, 174):
out174[i] = -1;
ok = ctypes.c_int()
ok.value = -1
libldpc.ldpc_decode(c174, ldpc_iters, out174, ctypes.byref(ok))
plain174 = numpy.zeros(174, dtype=numpy.int32);
for i in range(0, 174):
plain174[i] = out174[i];
plain91 = plain174[0:91]
return [ ok.value, plain91 ]
# returns [ nok, plain ], where nok is the number of parity
# checks that worked out, should be 83=174-91.
def ldpc_decode(codeword, ldpc_iters):
if libldpc != None:
return ldpc_decode_c(codeword, ldpc_iters)
else:
return ldpc_decode_python(codeword, ldpc_iters)
if False:
ldpc_test(1*17)
ldpc_test(2*17)
ldpc_test(4*17)
ldpc_test(8*17)
sys.exit(1)
# nov 30 2018:
# C: ldpc_iters 15, success 0.59, 0.000383 sec/call
# python: ldpc_iters 15, success 0.58, 0.013829 sec/call
# dec 3 2018:
# ldpc_iters 15, success 0.45, 0.000339 sec/call
# XXX why worse now?
# apr 26 2019:
# ldpc_iters 17, success 0.46, 0.000139 sec/call
# ldpc_iters 34, success 0.51, 0.000183 sec/call
# ldpc_iters 68, success 0.51, 0.000272 sec/call
# ldpc_iters 136, success 0.52, 0.000436 sec/call
# gauss-jordan elimination of rows.
# m[row][col]
# inverts the square top of the matrix, swapping
# with lower rows as needed.
# returns the inverse of the top of the matrix.
# cooks up the identity matrix (the right-hand half)
# as needed.
# mod 2, so elements should be 0 or 1.
# keeps track of swaps in which[] -- every time it swaps two
# rows, it swaps the same two rows in which[].
# which[] could start out as range(0, n_rows).
def python_gauss_jordan(m, which):
rows = m.shape[1] # rows to invert = columns
# assert numpy.all(numpy.greater_equal(m, 0))
# assert numpy.all(numpy.less_equal(m, 1))
b = numpy.zeros((m.shape[0], rows * 2), dtype=m.dtype)
b[:,0:rows] = m
for row in range(0, rows):
if b[row,row] != 1:
# oops, find a row that has a 1 in row,row,
# and swap.
for row1 in range(row+1,m.shape[0]):
if b[row1,row] == 1:
tmp = numpy.copy(b[row])
b[row] = b[row1]
b[row1] = tmp
tmp = which[row]
which[row] = which[row1]
which[row1] = tmp
break
if b[row,row] != 1:
sys.stderr.write("not reducible\n")
print(b)
return numpy.array([])
# lazy creation of identity matrix in right half
b[row,rows+row] = (b[row,rows+row] + 1) % 2
# now eliminate
for row1 in range(0, m.shape[0]):
if row1 == row:
continue
if b[row1,row] != 0:
b[row1] = numpy.mod(b[row]+b[row1], 2)
# assert numpy.array_equal(b[0:rows,0:rows],
# numpy.eye(rows, dtype=numpy.int32))