import random
import numpy as np
def Change_Type(num_list):
num_list_new = [int(i) for i in num_list]
return num_list_new
def Sequence_adjustment(list_created):
M=np.mat((np.array([list_created])))
print(M)
def matrix_multi(lista, listb):
return [a*b for a,b in zip(lista,listb)]
def flip_bit(message, location):
# This will flip the bit at position e-1 (0->1 and 1->0)
message[location] = 1 - message[location]
return message
def binlist_to_int(list):
result = 0
for digit in list:
result = (result << 1) | digit
return result
def flip_random_bit(message):
e = random.randint(1, len(message))
print("Flipping bit (=introducing error) at location: " + str(e))
# This will flip the bit at position e-1 (0->1 and 1->0)
message= flip_bit(message, e - 1)
def hamming_find_error(hamming):
# Finding the location of any potential error
error_location_in_Ht_list = []
# Matrix multiplication between H and the message
# + checking the parity
# This will indicate which row in Ht indicates the location of the error
for i in H:
parity = matrix_multi(i, hamming).count(1) % 2
error_location_in_Ht_list.append(parity)
error_location_in_Ht = binlist_to_int(error_location_in_Ht_list) - 1
# If any parity bit is set to 1, it means there was an error.
# The value of the each parity bit can provide the location of the
# wrong bit.
if error_location_in_Ht >= 0:
error_location_in_hamming = binlist_to_int(Ht[error_location_in_Ht])
else:
error_location_in_hamming = 0
print('Where did we find an error? ' + str(error_location_in_hamming))
return error_location_in_hamming
def hamming_correct(hamming):
error_location = hamming_find_error(hamming)
# If there was an error, correct it by swapping the corresponding bit
if error_location > 0:
flip_bit(hamming, error_location - 1)
return hamming
# --------------------------------------part 1-------------------------------------------
message=input('Input the message: ')
y=len(message)
print('the length of message y is: ', y)
p=0
while 2**p<p+1+y:
p=p+1
print('the number of parity bits are: ', p)
x=y+p
print('the total length of hamming code is: ', x)
m = Change_Type(message)
print('the message is: ', m)
print('\n')
# --------------------------------------part 2-------------------------------------------
Ht=[]
for i in range(1,x+1):
DEC_to_BIN = bin(i)[2:].zfill(p)
L = list(reversed(Change_Type(DEC_to_BIN)))
Ht.append(L)
Ht_tool=[]
for i in range(1,x+1):
DEC_to_BIN = bin(i)[2:].zfill(p)
L = list(reversed(Change_Type(DEC_to_BIN)))
Ht_tool.append(L)
# Ht_tool can make H independent
np.transpose(Ht_tool)
H=np.transpose(Ht_tool).tolist()
H_tool=H
# H_tool can make H independent
Parity_bit_list=[]
for j in range(0,p):
Parity_bit_list.append(2**j)
Parity_bit_list.reverse()
for i in Parity_bit_list:
del Ht_tool[i-1]
import numpy as np
np.transpose(Ht_tool)
H_tool=np.transpose(Ht_tool).tolist()
h=H_tool
print("The Ht matrix of the message is: ")
Sequence_adjustment(Ht)
print('\n')
print("The H matrix of the message is: ")
Sequence_adjustment(H)
print(type(H))
print('\n')
print("The h matrix of the message is: ")
Sequence_adjustment(h)
# print(h,type(h))
print('\n')
# --------------------------------------Part3--------------------------------------------
# encode
Parity = []
for i in h:
message_and_h = matrix_multi(i, m)
bit = message_and_h.count(1) % 2
Parity.append(bit)
print('The parity bits is: ',Parity,'(in the position of 2**n)')
Parity_bit_list.reverse()
# print(Parity_bit_list)
encode=m
k=0
for i in Parity_bit_list:
encode.insert(i-1,Parity[k])
k+=1
print('The hamming encode message is: ',encode)
# create an error
encode_with_error=encode
flip_random_bit(encode_with_error)
print('The hamming code with error is: ',encode_with_error)
# check and correct error
corrected_message=hamming_correct(encode_with_error)
print('The corrected message is: ', corrected_message)
# decode
Parity_bit_list.reverse()
for i in Parity_bit_list:
del corrected_message[i-1]
print('The corrected message is: ', corrected_message)
学习资料,仅供参考~