• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Untitled

a guest Mar 25th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #Import numpy for linear algebra
2. import numpy as np
3.
4. """
5. open and read both the input of the channel and the output
6. Assuming the files are stored in a folder called: "Channelfiles"
7. """
8. with open("ChannelFiles/chout74.bin", "rb") as binary_file:
10. with open("ChannelFiles/code74.bin", "rb") as binary_file:
12. with open("ChannelFiles/chout1511.bin", "rb") as binary_file:
14. with open("ChannelFiles/code1511.bin", "rb") as binary_file:
16. """
17. Defining the parity-check matrix for both the (7,4) and the (15,11) Hamming codes.
18. They are both stored in a numpy array.
19. """
20. H73 = np.array([[1, 0, 1, 0, 1, 0, 1],
21.                [0, 1, 1, 0, 0, 1, 1],
22.                [0, 0, 0, 1, 1, 1, 1]],
23.                dtype= bool)
24.
25. H1511 = np.array([[1, 0, 1, 0, 1, 0, 1 ,0, 1, 0, 1, 0, 1, 0, 1],
26.                   [0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
27.                   [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
28.                   [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]],
29.                  dtype = bool)
30. v4 = [2, 4, 5, 6]
31. v11 = [2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14]
32.
33. """
34. This lambda function wil be used for rewritting the error vector:
35. """
36. toBits = lambda x: x%2
37.
38. """
39. The codewords for the (7,4) hamming code will be stored in a numpy array of size (7, 11,000)
40. And the codewords for the (15, 11) Hamming code will be stored in a numpy array of size (15, 4,000)
41. """
42. inputCodeWords7 = np.array([[inputChannel7[i] for i in range(a, a+7)] for a in range(0,77000,7)])
43. ouputCodeWords7 = np.array([[outputChannel7[i] for i in range(a, a+7)] for a in range(0,77000,7)])
44. inputCodeWords15 = np.array([[inputChannel15[i] for i in range(a, a+15)] for a in range(0,60000,15)])
45. ouputCodeWords15 = np.array([[outputChannel15[i] for i in range(a, a+15)] for a in range(0,60000,15)])
46.
47. def decodeChannels():
48.     """
49.    When called, this function will decode all words of both channels and print the result
50.    For both the  Hamming (7,4) code and the Hamming (15,11) code
51.
52.    """
53.     correct74=0
54.     correctedWords74 = 0
55.     correct1511=0
56.     correctedWords1511 = 0
57.
58.     for i in range(0, len(inputCodeWords7)):
59.
60.         #Calculate the hi = H*Yn.T
61.         hi = np.matmul(H73, ouputCodeWords7[i].T)
62.         hi = toBits(hi)
63.
64.         #calculate the error vector e:
65.         e = np.array([0, 0, 0, 0, 0, 0, 0], dtype = bool)
66.         errorIndex = hi[0]+2*hi[1]+4*hi[2]-1
67.
68.         #If an error is made, correct it, this is done by swapping the element at with index errorIndex
69.         if errorIndex!=-1:
70.             correctedWords74 +=1
71.             e[errorIndex] = 1
72.             ouputCodeWords7[i][errorIndex] = True^ouputCodeWords7[i][errorIndex]
73.
74.         #If the algorithm choose the correct word
75.         if np.array_equal((inputCodeWords7[i][v4]),(ouputCodeWords7[i][v4])):
76.             correct74 += 1
77.
78.     for i in range(0, len(inputCodeWords15)):
79.
80.         #Calculate the hi = H*Yn.T
81.         hi = np.matmul(H1511, ouputCodeWords15[i].T)
82.         hi = toBits(hi)
83.
84.         #calculate the error vector e:
85.         e = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype = bool)
86.         errorIndex = hi[0]+2*hi[1]+4*hi[2]+8*hi[3]-1
87.
88.         #If an error is detected: swap the bit:
89.         if errorIndex!=-1:
90.             correctedWords1511 += 1
91.             e[errorIndex] = 1
92.             ouputCodeWords15[i][errorIndex] = True^ouputCodeWords15[i][errorIndex]
93.
94.         if np.array_equal((ouputCodeWords15[i][v11]),(inputCodeWords15[i][v11])):
95.             correct1511 += 1
96.
97.
98.
99.     print("The hamming (7, 4) code is decoded, {} errors are detected, {}/{} words where correct".format(correctedWords74, correct74, len(inputCodeWords7)))
100.     print("Thus the bit error rate: {}".format(correctedWords74/len(inputCodeWords7)))
101.     print("And the decoded bit error rate: {}".format(1-correct74/len(inputCodeWords7)))
102.     print("The hamming (15, 11) code is decoded, {} errors are detected, {}/{} words where correct".format(correctedWords1511, correct1511, len(inputCodeWords15)))
103.     print("Thus the bit error rate: {}".format(correctedWords1511/len(inputCodeWords15)))
104.     print("And the decoded bit error rate: {}".format(1-correct1511/len(inputCodeWords15)))
105.
106.
107.
108. def explainDecodeChannels():
109.     """
110.    When called, this function will decode  the rirst 20 wordsd of both channels.
111.    Iit will print the intermidiate results of the decoding procedure of the first 20 code words
112.    For both the  Hamming (7,4) code and the Hamming (15,11) code.
113.    """
114.     print("----------------First the Hamming (7,4) code \t------------")
115.     for i in range(0, 20):
116.         print("\t\t---------------\tCodeword: \t{}: ---------------".format(i))
117.         print("\tInput \t  Xn: \t\t\t{}".format(inputCodeWords7[i]))
118.         print("\tOutput\t  Yn: \t\t\t{}".format(ouputCodeWords7[i]))
119.
120.         #Calculate the hi = H*Yn.T
121.         hi = np.matmul(H73, ouputCodeWords7[i].T)
122.         hi = toBits(hi)
123.         print("\th_i : \t\t\t\t{}".format(hi))
124.
125.         #calculate the error vector e:
126.         e = np.array([0, 0, 0, 0, 0, 0, 0], dtype = bool)
127.         errorIndex = hi[0]+2*hi[1]+4*hi[2]-1
128.
129.         if errorIndex!=-1:
130.             e[errorIndex] = 1
131.             ouputCodeWords7[i][errorIndex] = True^ouputCodeWords7[i][errorIndex]
132.
133.         print("\tThe error vector is: \t\t{}".format(e*1))
134.         print("\tCorrected output\t  Yn: \t{}, \tEstimated Vn: \t\t{}".format(ouputCodeWords7[i], ouputCodeWords7[i][v4]))
135.         print("\tInput \t  Xn: \t\t\t{}, \tWith Vn \t\t{}".format(ouputCodeWords7[i], ouputCodeWords7[i][v4]))
136.         print("The decoded output is the same as the decoded input: \t\t\t\t\t",
137.               np.array_equal((inputCodeWords7[i][v4]),(ouputCodeWords7[i][v4])), "\n")
138.     print("----------------Now the Hamming (15,11) code \t----------------")
139.
140.     for i in range(0, 20):
141.         print("\t\t---------------\tCodeword: \t{}: ---------------".format(i))
142.         print("\tInput \t  Xn: \t\t\t{}".format(inputCodeWords15[i]))
143.         print("\tOutput\t  Yn: \t\t\t{}".format(ouputCodeWords15[i]))
144.
145.         #Calculate the hi = H*Yn.T
146.         hi = np.matmul(H1511, ouputCodeWords15[i].T)
147.         hi = toBits(hi)
148.         print("\th_i : \t\t\t\t{}".format(hi))
149.
150.         #calculate the error vector e:
151.         e = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype = bool)
152.         errorIndex = hi[0]+2*hi[1]+4*hi[2]+8*hi[3]-1
153.
154.         #If an error is detected: swap the bit.
155.         if errorIndex!=-1:
156.             e[errorIndex] = 1
157.             ouputCodeWords15[i][errorIndex] = True^ouputCodeWords15[i][errorIndex]
158.
159.         print("\tThe error vector is: \t\t{}".format(e*1))
160.         print("\tCorrected output\t  Yn: \t{}, \tEstimated Vn: \t\t{}".format(ouputCodeWords15[i], ouputCodeWords15[i][v11]))
161.         print("\tInput \t  Xn: \t\t\t{}, \tWith Vn \t\t{}".format(inputCodeWords15[i], inputCodeWords15[i][v11]))
162.         print("The decoded output is the same as the decoded input: \t\t\t\t\t",
163.               np.array_equal((ouputCodeWords15[i][v11]),(inputCodeWords15[i][v11])), "\n")
164.
165.
166. if __name__ == "__main__":
167.     test = input("Would you like to see an explaination for the first 20 words for both channels[Y/n]?\n")
168.     if test=="Y":
169.         explainDecodeChannels()
170.         ouputCodeWords15 = np.array([[outputChannel15[i] for i in range(a, a+15)] for a in range(0,60000,15)])
171.         ouputCodeWords7 = np.array([[outputChannel7[i] for i in range(a, a+7)] for a in range(0,77000,7)])
172.
173.     decodeChannels()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top