Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Import numpy for linear algebra
- import numpy as np
- """
- open and read both the input of the channel and the output
- Assuming the files are stored in a folder called: "Channelfiles"
- """
- with open("ChannelFiles/chout74.bin", "rb") as binary_file:
- outputChannel7 = binary_file.read()
- with open("ChannelFiles/code74.bin", "rb") as binary_file:
- inputChannel7 = binary_file.read()
- with open("ChannelFiles/chout1511.bin", "rb") as binary_file:
- outputChannel15 = binary_file.read()
- with open("ChannelFiles/code1511.bin", "rb") as binary_file:
- inputChannel15 = binary_file.read()
- """
- Defining the parity-check matrix for both the (7,4) and the (15,11) Hamming codes.
- They are both stored in a numpy array.
- """
- H73 = np.array([[1, 0, 1, 0, 1, 0, 1],
- [0, 1, 1, 0, 0, 1, 1],
- [0, 0, 0, 1, 1, 1, 1]],
- dtype= bool)
- H1511 = np.array([[1, 0, 1, 0, 1, 0, 1 ,0, 1, 0, 1, 0, 1, 0, 1],
- [0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
- [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
- [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]],
- dtype = bool)
- v4 = [2, 4, 5, 6]
- v11 = [2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14]
- """
- This lambda function wil be used for rewritting the error vector:
- """
- toBits = lambda x: x%2
- """
- The codewords for the (7,4) hamming code will be stored in a numpy array of size (7, 11,000)
- And the codewords for the (15, 11) Hamming code will be stored in a numpy array of size (15, 4,000)
- """
- inputCodeWords7 = np.array([[inputChannel7[i] for i in range(a, a+7)] for a in range(0,77000,7)])
- ouputCodeWords7 = np.array([[outputChannel7[i] for i in range(a, a+7)] for a in range(0,77000,7)])
- inputCodeWords15 = np.array([[inputChannel15[i] for i in range(a, a+15)] for a in range(0,60000,15)])
- ouputCodeWords15 = np.array([[outputChannel15[i] for i in range(a, a+15)] for a in range(0,60000,15)])
- def decodeChannels():
- """
- When called, this function will decode all words of both channels and print the result
- For both the Hamming (7,4) code and the Hamming (15,11) code
- """
- correct74=0
- correctedWords74 = 0
- correct1511=0
- correctedWords1511 = 0
- for i in range(0, len(inputCodeWords7)):
- #Calculate the hi = H*Yn.T
- hi = np.matmul(H73, ouputCodeWords7[i].T)
- hi = toBits(hi)
- #calculate the error vector e:
- e = np.array([0, 0, 0, 0, 0, 0, 0], dtype = bool)
- errorIndex = hi[0]+2*hi[1]+4*hi[2]-1
- #If an error is made, correct it, this is done by swapping the element at with index errorIndex
- if errorIndex!=-1:
- correctedWords74 +=1
- e[errorIndex] = 1
- ouputCodeWords7[i][errorIndex] = True^ouputCodeWords7[i][errorIndex]
- #If the algorithm choose the correct word
- if np.array_equal((inputCodeWords7[i][v4]),(ouputCodeWords7[i][v4])):
- correct74 += 1
- for i in range(0, len(inputCodeWords15)):
- #Calculate the hi = H*Yn.T
- hi = np.matmul(H1511, ouputCodeWords15[i].T)
- hi = toBits(hi)
- #calculate the error vector e:
- e = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype = bool)
- errorIndex = hi[0]+2*hi[1]+4*hi[2]+8*hi[3]-1
- #If an error is detected: swap the bit:
- if errorIndex!=-1:
- correctedWords1511 += 1
- e[errorIndex] = 1
- ouputCodeWords15[i][errorIndex] = True^ouputCodeWords15[i][errorIndex]
- if np.array_equal((ouputCodeWords15[i][v11]),(inputCodeWords15[i][v11])):
- correct1511 += 1
- print("The hamming (7, 4) code is decoded, {} errors are detected, {}/{} words where correct".format(correctedWords74, correct74, len(inputCodeWords7)))
- print("Thus the bit error rate: {}".format(correctedWords74/len(inputCodeWords7)))
- print("And the decoded bit error rate: {}".format(1-correct74/len(inputCodeWords7)))
- print("The hamming (15, 11) code is decoded, {} errors are detected, {}/{} words where correct".format(correctedWords1511, correct1511, len(inputCodeWords15)))
- print("Thus the bit error rate: {}".format(correctedWords1511/len(inputCodeWords15)))
- print("And the decoded bit error rate: {}".format(1-correct1511/len(inputCodeWords15)))
- def explainDecodeChannels():
- """
- When called, this function will decode the rirst 20 wordsd of both channels.
- Iit will print the intermidiate results of the decoding procedure of the first 20 code words
- For both the Hamming (7,4) code and the Hamming (15,11) code.
- """
- print("----------------First the Hamming (7,4) code \t------------")
- for i in range(0, 20):
- print("\t\t---------------\tCodeword: \t{}: ---------------".format(i))
- print("\tInput \t Xn: \t\t\t{}".format(inputCodeWords7[i]))
- print("\tOutput\t Yn: \t\t\t{}".format(ouputCodeWords7[i]))
- #Calculate the hi = H*Yn.T
- hi = np.matmul(H73, ouputCodeWords7[i].T)
- hi = toBits(hi)
- print("\th_i : \t\t\t\t{}".format(hi))
- #calculate the error vector e:
- e = np.array([0, 0, 0, 0, 0, 0, 0], dtype = bool)
- errorIndex = hi[0]+2*hi[1]+4*hi[2]-1
- if errorIndex!=-1:
- e[errorIndex] = 1
- ouputCodeWords7[i][errorIndex] = True^ouputCodeWords7[i][errorIndex]
- print("\tThe error vector is: \t\t{}".format(e*1))
- print("\tCorrected output\t Yn: \t{}, \tEstimated Vn: \t\t{}".format(ouputCodeWords7[i], ouputCodeWords7[i][v4]))
- print("\tInput \t Xn: \t\t\t{}, \tWith Vn \t\t{}".format(ouputCodeWords7[i], ouputCodeWords7[i][v4]))
- print("The decoded output is the same as the decoded input: \t\t\t\t\t",
- np.array_equal((inputCodeWords7[i][v4]),(ouputCodeWords7[i][v4])), "\n")
- print("----------------Now the Hamming (15,11) code \t----------------")
- for i in range(0, 20):
- print("\t\t---------------\tCodeword: \t{}: ---------------".format(i))
- print("\tInput \t Xn: \t\t\t{}".format(inputCodeWords15[i]))
- print("\tOutput\t Yn: \t\t\t{}".format(ouputCodeWords15[i]))
- #Calculate the hi = H*Yn.T
- hi = np.matmul(H1511, ouputCodeWords15[i].T)
- hi = toBits(hi)
- print("\th_i : \t\t\t\t{}".format(hi))
- #calculate the error vector e:
- e = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype = bool)
- errorIndex = hi[0]+2*hi[1]+4*hi[2]+8*hi[3]-1
- #If an error is detected: swap the bit.
- if errorIndex!=-1:
- e[errorIndex] = 1
- ouputCodeWords15[i][errorIndex] = True^ouputCodeWords15[i][errorIndex]
- print("\tThe error vector is: \t\t{}".format(e*1))
- print("\tCorrected output\t Yn: \t{}, \tEstimated Vn: \t\t{}".format(ouputCodeWords15[i], ouputCodeWords15[i][v11]))
- print("\tInput \t Xn: \t\t\t{}, \tWith Vn \t\t{}".format(inputCodeWords15[i], inputCodeWords15[i][v11]))
- print("The decoded output is the same as the decoded input: \t\t\t\t\t",
- np.array_equal((ouputCodeWords15[i][v11]),(inputCodeWords15[i][v11])), "\n")
- if __name__ == "__main__":
- test = input("Would you like to see an explaination for the first 20 words for both channels[Y/n]?\n")
- if test=="Y":
- explainDecodeChannels()
- ouputCodeWords15 = np.array([[outputChannel15[i] for i in range(a, a+15)] for a in range(0,60000,15)])
- ouputCodeWords7 = np.array([[outputChannel7[i] for i in range(a, a+7)] for a in range(0,77000,7)])
- decodeChannels()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement