-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecc_lab.py
111 lines (94 loc) · 4.26 KB
/
ecc_lab.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
# version code 80e56511a793+
# Please fill out this stencil and submit using the provided submission script.
from vec import Vec
from mat import Mat
from bitutil import bits2mat, str2bits, noise, bits2str, mat2bits
from GF2 import one
from matutil import listlist2mat, mat2coldict, coldict2mat
## Task 1
""" Create an instance of Mat representing the generator matrix G. You can use
the procedure listlist2mat in the matutil module (be sure to import first).
Since we are working over GF (2), you should use the value one from the
GF2 module to represent 1"""
G = listlist2mat([[one, 0, one, one],
[one, one, 0, one],
[0, 0, 0, one],
[one, one, one, 0],
[0, 0, one, 0],
[0, one, 0, 0],
[one, 0, 0, 0]])
## Task 2
# Please write your answer as a list. Use one from GF2 and 0 as the elements.
encoding_1001 = [0, 0, one, one, 0, 0, one]
## Task 3
# Express your answer as an instance of the Mat class.
R = listlist2mat([[0, 0, 0, 0, 0, 0, one],
[0, 0, 0, 0, 0, one, 0],
[0, 0, 0, 0, one, 0, 0],
[0, 0, one, 0, 0, 0, 0]])
## Task 4
# Create an instance of Mat representing the check matrix H.
H = listlist2mat([[0, 0, 0, one, one, one, one],
[0, one, one, 0, 0, one, one],
[one, 0, one, 0, one, 0, one]])
## Task 5
def find_error(syndrome):
"""
Input: an error syndrome as an instance of Vec
Output: the corresponding error vector e
Examples:
>>> find_error(Vec({0,1,2}, {0:one})) == Vec({0, 1, 2, 3, 4, 5, 6},{3: one})
True
>>> find_error(Vec({0,1,2}, {2:one})) == Vec({0, 1, 2, 3, 4, 5, 6},{0: one})
True
>>> find_error(Vec({0,1,2}, {1:one, 2:one})) == Vec({0, 1, 2, 3, 4, 5, 6},{2: one})
True
>>> find_error(Vec({0,1,2}, {})) == Vec({0,1,2,3,4,5,6}, {})
True
"""
if syndrome[0] == 0 and syndrome[1] == 0 and syndrome[2] == 0:
return Vec({0,1,2,3,4,5,6}, {})
return Vec({0, 1, 2, 3, 4, 5, 6}, {int(syndrome[0] == one) * 4 + int(syndrome[1] == one) * 2 + int(syndrome[2] == one) - 1: one})
## Task 6
# Use the Vec class for your answers.
non_codeword = Vec({0,1,2,3,4,5,6}, {0: one, 1:0, 2:one, 3:one, 4:0, 5:one, 6:one})
error_vector = find_error(H * non_codeword)
code_word = non_codeword - error_vector
original = R * code_word
## Task 7
def find_error_matrix(S):
"""
Input: a matrix S whose columns are error syndromes
Output: a matrix whose cth column is the error corresponding to the cth column of S.
Example:
>>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
>>> find_error_matrix(S) == Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 3): 0, (3, 0): 0, (2, 1): 0, (6, 2): 0, (5, 1): one, (0, 3): 0, (4, 0): 0, (1, 2): 0, (3, 3): 0, (6, 3): 0, (5, 0): 0, (2, 2): 0, (4, 1): 0, (1, 1): 0, (3, 2): one, (0, 0): 0, (6, 0): 0, (2, 3): 0, (4, 2): 0, (1, 0): 0, (5, 3): 0, (0, 1): 0, (6, 1): 0, (3, 1): 0, (2, 0): 0, (4, 3): one, (5, 2): 0, (0, 2): 0})
True
"""
return coldict2mat(dict((k, find_error(c)) for (k, c) in mat2coldict(S).items()))
## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))
EP = P + noise(P, 0.02)
s_break = bits2str(mat2bits(EP))
# print(s_break)
## Task 9
C = G * P
bits_before = len(str2bits(s))
bits_after = len(mat2bits(C))
# print(bits_before, bits_after)
## Ungraded Task
CTILDE = C + noise(C, 0.04)
# print(bits2str(mat2bits(R * CTILDE)))
## Task 10
def correct(A):
"""
Input: a matrix A each column of which differs from a codeword in at most one bit
Output: a matrix whose columns are the corresponding valid codewords.
Example:
>>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
>>> correct(A) == Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
True
"""
return A + find_error_matrix(H * A)
# print(bits2str(mat2bits(R * correct(CTILDE))))