def encode(msg):
""" passed a list of bits (integers, 1 or 0), returns a hamming(8,4)-coded
list of bits """
while len(msg) % 4 != 0:
# pad the message to length
msg.append(0)
msg = np.reshape(np.array(msg), (-1, 4))
# create parity bits using transition matrix
transition = np.mat('1,0,0,0,0,1,1,1;\
0,1,0,0,1,0,1,1;\
0,0,1,0,1,1,0,1;\
0,0,0,1,1,1,1,0')
result = np.dot(msg, transition)
# mod 2 the matrix multiplication
return np.mod(result, 2)
评论列表
文章目录