Python program for (7,4) Hamming Code, error detection and correction | Python

In this we are going to see generation of (7,4) Hamming code, error detection and correction using python.

import numpy as np

HMatrix = [[1, 1, 0, 1, 1, 0, 0], [1, 0, 1, 1, 0, 1, 0], [1, 1, 1, 0, 0, 0, 1]]
errors = [[1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [
    0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1]]

HTranspose = [[HMatrix[j][i]
               for j in range(len(HMatrix))] for i in range(len(HMatrix[0]))]

sentCode = input("Enter the sent code: ")
userCode = []

for i in range(7):
    userCode.append(int(sentCode[i]))


m1 = np.array(userCode)
m2 = np.array(HTranspose)
m3 = np.array(errors)
m4 = np.dot(m3, m2)
m5 = np.dot(m1, m2)


for i in range(3):
    if m5[i] % 2 == 0:
        m5[i] = 0
    else:
        m5[i] = 1


emptyList = []
print("Syndrome is: ", m5)

for i in range(7):
    if HTranspose[i][0] == m5[0] and HTranspose[i][1] == m5[1] and HTranspose[i][2] == m5[2]:
        print("Error is at position (in bit): ", i+1)
        result = m3[i] ^ m1

try:
    print("Corrected code is: ", result, "\n\n")
except:
    print("No error\n\n")


#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post