danielmuthama / Hamming-Code

Implementation of Hamming Code error detection and correction in Python. Deployed in Telecommunication endpoints to detect and correct any errors encountered during packet delivery
29 stars 10 forks source link

Parity check always fails when the first bit is changed #1

Open emilio-nuno opened 3 years ago

emilio-nuno commented 3 years ago

Hello! I've been using this module to help test my own hamming code module. However, I've stumbled upon a problem wherein your decoding function says it could not find the error in the string when the first bit is changed. Here's an example of what I'm talking about (using an already encoded and valid hamming code string):

cadena = '1010101' print(detectar_error_externo(cadena))

The output is -1, which means your decoding function says there is no error, and there isn't. But when I change the first bit like so:

cadena = '0010101' print(detectar_error_externo(cadena))

The output is a print statement saying the error could not be detected.

danielmuthama commented 3 years ago

Responding to issue #1. Thanks emilio-nuno for sharing the issue with me. Let me look into it and I will get back to you sooner. Will it be possible to share your module please

emilio-nuno commented 3 years ago
def generar(d):
    data = list(d)
    data.reverse()
    c, ch, j, r, h = 0, 0, 0, 0, []

    while ((len(d) + r + 1) > (pow(2, r))):
        r = r + 1

    for i in range(0, (r + len(data))):
        p = (2 ** c)

        if (p == (i + 1)):
            h.append(0)
            c = c + 1

        else:
            h.append(int(data[j]))
            j = j + 1

    for parity in range(0, (len(h))):
        ph = (2 ** ch)
        if (ph == (parity + 1)):
            startIndex = ph - 1
            i = startIndex
            toXor = []

            while (i < len(h)):
                block = h[i:i + ph]
                toXor.extend(block)
                i += 2 * ph

            for z in range(1, len(toXor)):
                h[startIndex] = h[startIndex] ^ toXor[z]
            ch += 1

    h.reverse()
    return ''.join(map(str, h))

def detectar_error_externo(d):
    data = list(d)
    data.reverse()
    c, ch, j, r, error, h, parity_list, h_copy = 0, 0, 0, 0, 0, [], [], []

    for k in range(0, len(data)):
        p = (2 ** c)
        h.append(int(data[k]))
        h_copy.append(data[k])
        if (p == (k + 1)):
            c = c + 1

    for parity in range(0, (len(h))):
        ph = (2 ** ch)
        if (ph == (parity + 1)):

            startIndex = ph - 1
            i = startIndex
            toXor = []

            while (i < len(h)):
                block = h[i:i + ph]
                toXor.extend(block)
                i += 2 * ph

            for z in range(1, len(toXor)):
                h[startIndex] = h[startIndex] ^ toXor[z]
            parity_list.append(h[parity])
            ch += 1
    parity_list.reverse()
    error = sum(int(parity_list) * (2 ** i) for i, parity_list in enumerate(parity_list[::-1]))

    if ((error) == 0):
        #print('There is no error in the hamming code received')
        return -1

    elif ((error) >= len(h_copy)):
        #print('Error cannot be detected')
        pass

    else:
        return error
        #print('Error is in', error, 'bit')

        if (h_copy[error - 1] == '0'):
            h_copy[error - 1] = '1'

        elif (h_copy[error - 1] == '1'):
            h_copy[error - 1] = '0'
            print('After correction hamming code is:- ')
        h_copy.reverse()
        print(int(''.join(map(str, h_copy))))

if __name__ == '__main__':
    detectar_error_externo('01110')
danielmuthama commented 3 years ago

Responding to issue #1. Emilio-nuno, I have your point now, I will have to schedule some time tomorrow for thorough debugging and validation but you can also find time and help in resolving the matter