Exemplo n.º 1
0
def HammingCodeDriver():
    mode = input("Enter mode: ")
    total_bits, num_data_bits = (15, 11) if mode == "H1511" else (7, 4)

    message = np.array(helper.generate_random_message(num_data_bits))
    print("Message          :", message)

    x = helper.encodeMessage(G11, message) if total_bits == 15 else helper.encodeMessage(G4, message)
    print("Send Vector      :", x)

    # Create error
    helper.generate_error(x, total_bits)

    z = helper.generateParityCheckMatrix(H11, x) if total_bits == 15 else helper.generateParityCheckMatrix(H4, x)
    print("Received Message :", x)

    print("Parity Check     :", z)

    # Convert to Big endian
    z = z[::-1]

    if sum(z) == 0:
        print("Decoded Message  :", helper.decodeMessage(R11, x)) if total_bits == 15 else print("Decoded Message  :", helper.decodeMessage(R4, x))
    else:
        x = helper.errorCorrection(x, z)
        print("Corrected Message:", x)
        print("Decoded Message  :", helper.decodeMessage(R11, x)) if total_bits == 15 else print("Decoded Message  :", helper.decodeMessage(R4, x))
Exemplo n.º 2
0
def Hamming1511NoError(print_results):
    flag = True
    for i in range(0, 2048):
        flag = True
        if print_results: print("Num: ",i)
        message = np.array([int(a) for a in list(bin(i)[2:].zfill(11))])
        x = helper.encodeMessage(G11, message)
        if print_results: print("Message: ",message)
        z = helper.generateParityCheckMatrix(H11, x)[::-1]
        if print_results: print("Parity: ", z)
        if sum(z) == 0:
            if message.all() != helper.decodeMessage(R11, x).all():
                flag = False
        if not flag:
            print("Failed Test: ",i)
            break
    if flag:
        print("Hamming(15, 11) No Errors: SUCCESS")
Exemplo n.º 3
0
def Hamming1511WithError(print_results):
    flag = True
    for i in range(0, 2048):
        for error_location in range(0,15):
            flag = True
            if print_results: print("Num: ",i, "Error location: ", error_location)
            message = np.array([int(a) for a in list(bin(i)[2:].zfill(11))])
            if print_results: print("Message: ",message)
            x = helper.encodeMessage(G11, message)
            if print_results: print("Syndrome: ", x)
            helper.flip_bit(x, error_location)
            if print_results: print("Syndrome we: ", x)
            z = helper.generateParityCheckMatrix(H11, x)[::-1]
            if print_results: print("Parity: ", z)
            x = helper.errorCorrection(x, z)
            if print_results: print("Fixed Syndrome: ", x)
            if sum(z) == 0:
                if message.all() != helper.decodeMessage(R11, x).all():
                    flag = False
            if not flag:
                print("Failed Test: ",i)
                break
    if flag:
        print("Hamming(15, 11) with Errors: SUCCESS")