-
Notifications
You must be signed in to change notification settings - Fork 0
/
encodeframe.py
178 lines (143 loc) · 5.4 KB
/
encodeframe.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
#!/usr/bin/env python3
import sys
from vecutil import list2vec, vec2list
from matutil import listlist2mat
from GF2 import one
golay24mat = listlist2mat([[one, one, 0, 0, 0, one, one, one, 0, one, 0, one],
[0, one, one, 0, 0, 0, one, one, one, 0, one, one],
[one, one, one, one, 0, one, one, 0, one, 0, 0, 0],
[0, one, one, one, one, 0, one, one, 0, one, 0, 0],
[0, 0, one, one, one, one, 0, one, one, 0, one, 0],
[one, one, 0, one, one, 0, 0, one, one, 0, 0, one],
[0, one, one, 0, one, one, 0, 0, one, one, 0, one],
[0, 0, one, one, 0, one, one, 0, 0, one, one, one],
[one, one, 0, one, one, one, 0, 0, 0, one, one, 0],
[one, 0, one, 0, one, 0, 0, one, 0, one, one, one],
[one, 0, 0, one, 0, 0, one, one, one, one, one, 0],
[one, 0, 0, 0, one, one, one, 0, one, 0, one, one]])
golay23mat = listlist2mat([[one, one, 0, 0, 0, one, one, one, 0, one, 0],
[0, one, one, 0, 0, 0, one, one, one, 0, one],
[one, one, one, one, 0, one, one, 0, one, 0, 0],
[0, one, one, one, one, 0, one, one, 0, one, 0],
[0, 0, one, one, one, one, 0, one, one, 0, one],
[one, one, 0, one, one, 0, 0, one, one, 0, 0],
[0, one, one, 0, one, one, 0, 0, one, one, 0],
[0, 0, one, one, 0, one, one, 0, 0, one, one],
[one, one, 0, one, one, one, 0, 0, 0, one, one],
[one, 0, one, 0, one, 0, 0, one, 0, one, one],
[one, 0, 0, one, 0, 0, one, one, one, one, one],
[one, 0, 0, 0, one, one, one, 0, one, 0, one]])
def bitArrayToGF2(data):
return [one if x==1 else 0 for x in data]
def GF2toBitArray(data):
return [1 if x==one else 0 for x in data]
def golay24Encode(data):
if len(data) != 12:
return None
# convert to a vector over GF(2)
datavec = list2vec(bitArrayToGF2(data))
codeword = datavec * golay24mat
# convert codeword to bit array
codebits = GF2toBitArray(vec2list(codeword))
return data + codebits
def golay23Encode(data):
if len(data) != 12:
return None
# convert to a vector over GF(2)
datavec = list2vec(bitArrayToGF2(data))
codeword = datavec * golay23mat
# convert codeword to bit array
codebits = GF2toBitArray(vec2list(codeword))
return data + codebits
def ambeModGen(seed, length):
if seed < 0 or seed > 4095:
return None
modVec = [16 * seed] + [0]*length
for n in range(1, length+1):
modVec[n] = (173 * modVec[n-1]) + 13849 - (65536 * (((173 * modVec[n-1]) + 13849) // 65536))
modVec.pop(0)
return [n // 32768 for n in modVec]
def dstarInterleave(blk0, blk1, blk2):
bit_count = 0;
bit_array = [0]*72
for i in range(12):
for j in range(6):
bit_array[(j*12)+i] = bit_count^7;
bit_count += 1;
output = [0]*72
counter = 0
for i in range(24):
output[bit_array[counter]] = blk0[i]
counter += 1
for i in range(24):
output[bit_array[counter]] = blk1[i]
counter += 1
for i in range(24):
output[bit_array[counter]] = blk2[i]
counter += 1
return output
# main starts here
argv = sys.argv
if len(argv) != 3:
sys.stderr.write("Usage: %s infile.txt outfile.ambe\n" % argv[0])
sys.exit(-1)
infile = open(argv[1], 'r')
outfile = open(argv[2], 'w')
count = 0 # loop counter used for writing out
while True:
# parse a line of input
instr = infile.readline();
if len(instr) != 50 and len(instr) != 49:
break
# transfer the data into an array
indata = [int(x) for x in instr if x.isdigit()]
# break into four fields
if len(indata) == 49:
indata.pop(24)
u0 = indata[0:12]
u1 = indata[12:24]
u2 = indata[24:]
# encode u0
c0 = golay24Encode(u0)
# encode u1
intermediate_c1 = golay24Encode(u1)
# apply PRNG output seeded by u0 to intermediate_c1
seed = (u0[0] << 11 |
u0[1] << 10 |
u0[2] << 9 |
u0[3] << 8 |
u0[4] << 7 |
u0[5] << 6 |
u0[6] << 5 |
u0[7] << 4 |
u0[8] << 3 |
u0[9] << 2 |
u0[10] << 1 |
u0[11])
modArray = ambeModGen(seed, 24)
# this is also over GF(2)
c1 = GF2toBitArray(vec2list(list2vec(bitArrayToGF2(intermediate_c1)) + list2vec(bitArrayToGF2(modArray))))
# just copy u2->c2
c2 = u2
output = dstarInterleave(c0, c1, c2)
# convert output to a BYTE array
byteoutput = []
for n in range(9):
outbyte = int(output[n*8+7] << 7 |
output[n*8+6] << 6 |
output[n*8+5] << 5 |
output[n*8+4] << 4 |
output[n*8+3] << 3 |
output[n*8+2] << 2 |
output[n*8+1] << 1 |
output[n*8])
byteoutput.append(outbyte)
outstr = "{:05d} {:02d} {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format(count // 100, count % 100, byteoutput[0], byteoutput[1], byteoutput[2], byteoutput[3], byteoutput[4], byteoutput[5], byteoutput[6], byteoutput[7], byteoutput[8])
# write data out
if(count == 0):
outfile.write("#C Version 1.0\n")
outfile.write(outstr)
outfile.write("\n")
count += 2
infile.close()
outfile.close()