Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
749 B
2
Indexable
Never
import numpy as np

G = [[1, 1, 0, 1],
     [1, 0, 1, 1],
     [1, 0, 0, 0],
     [0, 1, 1, 1],
     [0, 1, 0, 0],
     [0, 0, 1, 0],
     [0, 0, 0, 1]]

H = [[1, 0, 1, 0, 1, 0, 1],
     [0, 1, 1, 0, 0, 1, 1],
     [0, 0, 0, 1, 1, 1, 1]]

G = np.array(G)
H = np.array(H)

word_to_test = [1, 1, 0, 1]


def hamming_detect_errors(cw):
    parity_bits = np.zeros((3,))
    if False in np.equal(np.mod(np.matmul(H, cw), 2), parity_bits):
        return "Error"
    else:
        return "No errors detected!"


def hamming_encode_cw(inp):
    return np.mod(np.matmul(G, inp), 2)


code_word = hamming_encode_cw(np.array(word_to_test))
print(code_word)

check_error = hamming_detect_errors(code_word)
print(check_error)