Â
import functools
import random
def calculateEXOR(data):
exoredData = functools.reduce(lambda x, y: x ^ y, data)
return str(exoredData)
def checkError(data):
if calculateEXOR(data) == "0":
print("There is No error in the received data.\n")
elif calculateEXOR(data) == "1":
print("There is an error in the received data.\n")
else:
print("Invalid Input.\n")
def OneBitError(input_data, VRCbit):
dataBits = input_data[0:len(input_data)-1]
oneBitError = random.choice(dataBits)
i = dataBits.index(oneBitError)
oneBitErrorDataBits = dataBits[:i]+[1-oneBitError]+dataBits[i+1:]
print("\n\nThe Data with one bit error is: " + str(oneBitErrorDataBits))
oneBitErrorDataBits.append(int(VRCbit))
print("The Received data with one bit error is: " + str(oneBitErrorDataBits))
print("The index of the error bit is: " + str(i))
print("Error Detection of One Bit Error is: ")
checkError(oneBitErrorDataBits)
def TwoBitError(input_data, VRCbit):
dataBits = input_data[0:len(input_data)-1]
twoBitError = random.sample(dataBits, 2)
indices = []
for i in range(len(twoBitError)):
indices.append(dataBits.index(i))
for i in range(len(indices)):
if dataBits[indices[i]] == 1:
dataBits[indices[i]] = 0
else:
dataBits[indices[i]] = 1
print("\n\nThe Data with two bit error is: " + str(dataBits))
dataBits.append(int(VRCbit))
print("The Received data with two bit error is: " + str(dataBits))
print("The Index of the error bit is: " + str(indices))
print("Error Detection of Two Bit Error is: ")
checkError(dataBits)
input_data = input("\n\nEnter the 8 bits Binary data: ")
input_data = input_data.split()
input_data = [int(i) for i in input_data]
VRCbit = calculateEXOR(input_data)
print("\nThe VRC Parity Bit is : " + calculateEXOR(input_data))
input_data.append(int(calculateEXOR(input_data)))
print("\n\nThe Received data is: " + str(input_data))
checkError(input_data)
print("The Decoder Output is: " + str(input_data[0:len(input_data)-1]))
OneBitError(input_data, VRCbit)
TwoBitError(input_data, VRCbit)
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP