res = Mat(({0, 1, 2, 3, 4, 5, 6},S.D[1]), {}) for i in res.D[1]: e = find_error(S[i]) # S[i] returns i-th column of S for j in res.D[0]: res[j,i] = e[j] return res ## 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)) # p[i] column is 4-bit nibble ## Ungraded task # Use noise(A, s) to produce the transmitted message # input: P - matrix being transmitted print("Message w/o encoding and ecc:", bits2str(mat2bits(P + noise(P, 0.02))).encode("utf-8")) ## Task 9 C = G*P # encode bits_before = len(str2bits(s)) # bits before encoding bits_after = bits_before//4*7 # bits after encoding print(bits_before, bits_after) ## Ungraded Task CTILDE = C + noise(C, 0.02) # encoded message received print("Message with encoding, but w/o ecc:", bits2str(mat2bits(R*CTILDE)).encode("utf-8")) ## Task 10 def correct(A): """
print(P) ## Task 9 C = G * P print(C) bits_before = len(str2bits(s)) print(bits_before) bits_after = len(mat2bits(C)) print(bits_after ) ## Ungraded Task CTILDE = C + noise(C, 0.02) print(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(CTILDE))) print(bits2str(mat2bits(R * correct(CTILDE))))
def error_test(s, prob): msg = bits2mat(str2bits(s)) cw = G*msg rcvd = cw + noise(cw, prob) msg_rcvd = R*correct(rcvd) return s == bits2str(mat2bits(msg_rcvd))
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
bits_before = len(mat2bits(P)) bits_after = len(mat2bits(C)) ## Ungraded Task CTILDE = C + noise(C, 0.02) ## Task 8 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}) """ return A + find_error_matrix(H * A) if __name__ == '__main__': import sys ctilde = C + noise(C, float(sys.argv[2])) s2 = bits2str(mat2bits(R * correct(ctilde))) if s2 == s: print('Fine') else: print('Failed to recover') print(s2) print(s)
e = find_error(S[i]) # S[i] returns i-th column of S for j in res.D[0]: res[j, i] = e[j] return res ## 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)) # p[i] column is 4-bit nibble ## Ungraded task # Use noise(A, s) to produce the transmitted message # input: P - matrix being transmitted print("Message w/o encoding and ecc:", bits2str(mat2bits(P + noise(P, 0.02))).encode("utf-8")) ## Task 9 C = G * P # encode bits_before = len(str2bits(s)) # bits before encoding bits_after = bits_before // 4 * 7 # bits after encoding print(bits_before, bits_after) ## Ungraded Task CTILDE = C + noise(C, 0.02) # encoded message received print("Message with encoding, but w/o ecc:", bits2str(mat2bits(R * CTILDE)).encode("utf-8")) ## Task 10 def correct(A):
def decode_string_matrix(A): return bits2str(mat2bits(A))
# Task 4.14.6 Derive original message from [1, 0, 1, 1, 0, 1, 1] non_c = list2vec([One(), 0, One(), One(), 0, One(), One()]) syndrome = H*non_c e = find_error(syndrome) codeword = e + non_c message = R * codeword # Task 4.14.7 def find_error_matrix(s): return coldict2mat({pos: find_error(vec) for pos, vec in mat2coldict(s).items()}) test_matrix = listlist2mat([[One(), 0], [One(), 0], [One(), One()]]) # Task 4.14.8 s = ''.join([chr(i) for i in range(256)]) assert bits2str(str2bits(s)) == s # Task 4.14.9 & 4.14.10 encode message string to matrix of nibbles msg = "I'm trying to free your mind, Neo." P = bits2mat(str2bits(msg)) # Task 4.14.11 add noise to message E = noise(P, 0.02) EP = E + P perturbed_msg = bits2str(mat2bits(EP)) # Task 4.14.12 C = G*P # Task 4.14.13 CE = noise(C, 0.02)