I'm trying to write the Hamming Code encoding in Python and I'm stuck on the part where I have to calculate the index for the parity bits.
Hamming Code is the use of extra parity bits to allow the identification of a single error. Create the code word as follows:
Mark all bit positions that are powers of two as parity bits. (positions 1, 2, 4, 8, 16, 32, 64, etc.) All other bit positions are for the data to be encoded. (positions 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, etc.) Each parity bit calculates the parity for some of the bits in the code word. The position of the parity bit determines the sequence of bits that it alternately checks and skips.
Position 1: check 1 bit, skip 1 bit, check 1 bit, skip 1 bit, etc. (1,3,5,7,9,11,13,15,...)
Position 2: check 2 bits, skip 2 bits, check 2 bits, skip 2 bits, etc. (2,3,6,7,10,11,14,15,...)
Position 4: check 4 bits, skip 4 bits, check 4 bits, skip 4 bits, etc. (4,5,6,7,12,13,14,15,20,21,22,23,...)
Position 8: check 8 bits, skip 8 bits, check 8 bits, skip 8 bits, etc. (8-15,24-31,40-47,...)
Position 16: check 16 bits, skip 16 bits, check 16 bits, skip 16 bits, etc. (16-31,48-63,80-95,...)
Position 32: check 32 bits, skip 32 bits, check 32 bits, skip 32 bits, etc. (32-63,96-127,160-191,...) etc.
Set a parity bit to 1 if the total number of ones in the positions it checks is odd. Set a parity bit to 0 if the total number of ones in the positions it checks is even.
Essentially, I need to generate the parity bits at indexes:
P1 = [1,3,5,7,9,11 etc]
P2 = [2,3,6,7,10,11 etc]
P3 = [4,5,6,7,12,13 etc]
P4 = [8,9,10,11,12,13,14,15,24 etc]
Once these are found, I need to add up the bits, mod by 2 and insert at the correct position.
My code to find where to insert
# Function to calculate Hamming code
def hamming(number):
print("Hamming Code - Beginning to encode. ")
listP = half_ham(number)
print("Inserting P at relevant indices: ", listP)
encode = []
print("Length of messgae: ",len(listP), "bits.")
index = []
for i, j in enumerate(listP, start = 1):
print("i", i)
if isPowerOfTwo(i):
index = gen_indices(i, len(listP))
c = [ listP[i] for i in index]
print("C: ", c)
# Function to insert P at positions which are powers of 2
def half_ham(bin_str):
parity_positions = [2**i for i in range(0, len(bin_str))]
ret = []
current_index = 1
indexDict = {}
while (len(bin_str) > 0):
if current_index in parity_positions:
ret.append('P')
else:
ret.append(bin_str[0])
bin_str = bin_str[1:]
current_index += 1
return ret
Input:
hamming("10101010")
Current Output:
Hamming Code - Beginning to encode.
Inserting P at relevant indices: ['P', 'P', '1', 'P', '0', '1', '0', 'P', '1', '0', '1', '0']
Length of message: 12
Now I need to find the correct parity indexes (P1, P2, P3 as above).
Originally, I was trying to generate the index with a while loop. This works until I get a list out of range error. Plus it is barely readable.
# Function to get a list of the indexes
def gen_indices(x, y):
jump = x
current = x
index = []
while current <= y:
index.extend(list(range(current, current+jump)))
print("before current:",current)
current += jump * 2
return index
Now I'm trying to use the enumerate function to get a list of the desired indexes. Something like:
def index(x, listP):
for x, y in enumerate(listP, start = x):
print(x,y)
Desired output would be:
P1= 1 P P2= 2 P 3 1 3 1 5 0 6 1 7 1 7 1
From this I could then add up the bits into a string. Can anyone help?
I hope that makes sense. I am very new to coding so please don't judge.