-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvolutional_code.py
259 lines (224 loc) · 10.7 KB
/
convolutional_code.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
from typing import List
import itertools
import copy
class ConvolutionalCode:
"""The code assumes zero state termination, and k=1"""
def __init__(self, generators: tuple):
"""
:param generators: each element in the tuple represents a single generator polynomial. The convention
we use is: 1+D=b011=3 (and not 1+D=6)
"""
self.gen = gnrs(generators)
self.K = len(max(self.gen, key=len)) - 1
self.regi = [0] * self.K
def gen_op(self, s):
""" This function operates the generators on a given bits. """
shift = self.regi
end = []
for i in range(len(s)):
shift.insert(0, s[i])
for fun in self.gen:
xor_bits = [shift[i] for i, s in enumerate(fun[::-1]) if s == '1']
end.append(xor(*xor_bits))
shift.pop()
return end
def encode(self, data: bytes) -> List[int]:
"""
encode input data bytes. Uses zero tail termination
:param data: data to be encoded
:return: encoded data
:rtype: list[int]
"""
enc = [f'{data[i]:0>8b}' for i in range(len(data))]
input_enc = []
for s in enc:
input_enc += [int(k) for k in s]
ipt = self.gen_op(input_enc + [0] * self.K)
return ipt
def decode(self, data: List[int]) -> (bytes, int):
"""
decode data bytes. The function assumes initial and final state of encoder was at the zero state.
:param data: coded data to be decoded, list of ints representing each received bit.
:return: return a tuple of decoded data, and the amount of corrected errors.
:rtype: (bytes, int)
"""
register = self.regi.copy()
f_bits = ''.join(str(e) for e in data)
F_bits = [f_bits[i:i + len(self.gen)] for i in range(0, len(f_bits), len(self.gen))]
Vit_dict_keys = [''.join(map(str, i)) for i in itertools.product([0, 1], repeat=self.K)]
Vit_dict = {i: [] for i in Vit_dict_keys} # counter for the The Viterbi Decoder.
turn = 0
while turn < len(F_bits):
if turn == 0:
prev = "".join(str(i) for i in register)
op_1 = self.one_bit_change(register, 0)
op_1_d = hamming_distance(F_bits[turn], op_1)
Vit_dict[f'0{prev[1:]}'] = [f"{prev} -> 0{prev[:self.K - 1]}"] + [op_1_d]
op_2 = self.one_bit_change(register, 1)
op_2_d = hamming_distance(F_bits[turn], op_2)
Vit_dict[f'1{prev[1:]}'] = [f"{prev} -> 1{prev[:self.K - 1]}"] + [op_2_d]
else:
temp_trails = []
for path in Vit_dict.values():
if path:
register = [int(i) for i in path[-2][-self.K:]]
prev = "".join(str(i) for i in register)
op_1 = self.one_bit_change(register, 0)
op_1_d = hamming_distance(F_bits[turn], op_1)
new_path1 = path + [f"{prev} -> 0{prev[:self.K - 1]}", path[-1] + op_1_d]
temp_trails.append(new_path1)
op_2 = self.one_bit_change(register, 1)
op_2_d = hamming_distance(F_bits[turn], op_2)
new_path2 = path + [f"{prev} -> 1{prev[:self.K - 1]}", path[-1] + op_2_d]
temp_trails.append(new_path2)
find_path = sorted(temp_trails, key=lambda x: x[-2][-self.K:])
for i, best_path in enumerate(find_path):
try:
if best_path[-2][-self.K:] == find_path[i + 1][-2][-self.K:]:
want = min(best_path[-1], find_path[i + 1][-1])
if want == best_path[-1]:
find_path.remove(find_path[i + 1])
pass
else:
best_path = find_path[i + 1]
find_path.remove(best_path)
Vit_dict[best_path[-2][-self.K:]] = best_path
continue
except IndexError:
pass
Vit_dict[best_path[-2][-self.K:]] = best_path
turn += 1
win_order = []
try:
win_order = sorted(Vit_dict.items(), key=lambda x: x[-1][-1])
except IndexError:
for i in Vit_dict.items():
if i:
win_order.append(i)
win_order = sorted(win_order, key=lambda x: x[-1][-1])
winner = next(iter(win_order))
trans = [i[:self.K] for i in winner[1] if type(i) == str]
final = []
for index, item in enumerate(trans):
try:
a = f'1{item[:-1]}'
if f'1{item[:-1]}' == trans[index + 1]:
final.append(1)
else:
final.append(0)
except IndexError:
pass
del final[-self.K + 1:]
full = ''.join(str(e) for e in final)
bits_8 = []
for index in range(0, len(full), 8):
bits_8.append(full[index: index + 8])
decoded_bytes = bytes([int(chunk, 2) for chunk in bits_8])
return decoded_bytes, winner[1][-1]
def one_bit_change(self, shift_list, num_in):
shift = shift_list.copy()
data = []
shift.insert(0, num_in)
for fun in self.gen:
xor_bits = [shift[i] for i, s in enumerate(fun[::-1]) if s == '1']
data.append(xor(*xor_bits))
number = ''.join(str(i) for i in data)
return number
def xor(*kwargs) -> int:
sum = 0
for num in kwargs:
sum += num
return int(1 == sum % 2)
def hamming_distance(s1: str, s2: str) -> str:
return sum(s1[i] != s2[i] for i in range(len(s1)))
def gnrs(gnr: tuple) -> list:
k = [bin(i).replace('0b', '') for i in gnr]
s = max(k, key=len)
for i, b in enumerate(k[:]):
if len(b) < len(s):
b = '0' * (len(s) - len(b)) + b
k.insert(i, b)
k.pop(i + 1)
return k
if __name__ == "__main__":
# example of constructing an encoder with constraint length = 2
# and generators:
# g1(x) = 1 + x^2, represented in binary as b101 = 5
# g2(x) = 1 + x+ x^2, represented in binary as b111 = 7
conv = ConvolutionalCode((5, 7))
# encoding a byte stream
input_bytes = b"\xFE\xF0\x0A\x01"
encoded = conv.encode(input_bytes)
print(encoded == [1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1,
1, 1])
# decoding a byte stream
decoded, corrected_errors = conv.decode(encoded)
print(decoded == input_bytes)
print(corrected_errors)
# introduced five random bit flips
import random
corrupted = encoded.copy()
for _ in range(5):
idx = random.randint(0, len(encoded) - 1)
corrupted[idx] = int(not (corrupted[idx]))
decoded, corrected_errors = conv.decode(corrupted)
print(decoded == input_bytes)
print(corrected_errors)
# example of constructing an encoder with constraint length = 3, and rate 1/3
# and generators:
# g1(x) = 1 + x, represented in binary as b011 = 3
# g2(x) = 1 + x + x^2, represented in binary as b111 = 7
# g3(x) = 1 + x^2 + x^3, represented in binary as b1101 = 13
conv = ConvolutionalCode((3, 7, 13))
# encoding a byte stream
input_bytes = b"\x72\x01"
encoded = conv.encode(input_bytes)
encoded = [0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1]
# decoding a byte stream
decoded, corrected_errors = conv.decode(encoded)
print(decoded == input_bytes)
print(corrected_errors)
# introduced five random bit flips
import random
corrupted = encoded.copy()
for _ in range(5):
idx = random.randint(0, len(encoded) - 1)
corrupted[idx] = int(not (corrupted[idx]))
decoded, corrected_errors = conv.decode(corrupted)
print(decoded == input_bytes)
print(corrected_errors)
conv = ConvolutionalCode((5, 7, 27, 111, 230, 34, 52, 66, 89, 103, 153, 255))
# encoding a byte stream
input_bytes = b"\xFE\xF0\x0A\x01"
encoded = conv.encode(input_bytes)
print(encoded == [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1,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0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1,
1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1,
0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 1]
)
# decoding a byte stream
decoded, corrected_errors = conv.decode(encoded)
print(decoded == input_bytes)
print(corrected_errors)
# introduced five random bit flips
import random
corrupted = encoded.copy()
for _ in range(30):
idx = random.randint(0, len(encoded) - 1)
corrupted[idx] = int(not (corrupted[idx]))
decoded, corrected_errors = conv.decode(corrupted)
print(decoded == input_bytes)
print(corrected_errors)