WHCSRL 技术网

python 汉明码

  1. import numpy as np
  2. def func(X):
  3. '''
  4. 返回G * H_T
  5. '''
  6. H = np.array(
  7. [[0, 0, 0, 1, 1, 1, 1],
  8. [0, 1, 1, 0, 0, 1, 1],
  9. [1, 0, 1, 0, 1, 0, 1]])
  10. G1 = np.eye(4)
  11. G2 = [[X[0], X[1], X[2]]
  12. , [X[3], X[4], X[5]]
  13. , [X[6], X[7], X[8]]
  14. , [X[9], X[10], X[11]]]
  15. G = np.concatenate((G1, G2), axis=1)
  16. GH_T = np.dot(G, np.transpose(H))
  17. m, n = GH_T.shape[0], GH_T.shape[1]
  18. for i in range(m):
  19. for j in range(n):
  20. GH_T[i][j] %%= 2 #不进位
  21. GH_T = np.array(GH_T)
  22. GH_T = GH_T.reshape(1,12)
  23. return GH_T
  24. def shuzu(j,len):
  25. '''
  26. 将字符串转换为数组
  27. '''
  28. C = np.zeros((len,))
  29. for k in range(0,len):
  30. C[k] = int(j[k])
  31. return C
  32. def func2(e):
  33. '''
  34. 返回伴随式s = e * H_T
  35. '''
  36. H = np.array(
  37. [[0, 0, 0, 1, 1, 1, 1],
  38. [0, 1, 1, 0, 0, 1, 1],
  39. [1, 0, 1, 0, 1, 0, 1]])
  40. s_ = np.dot(e,np.transpose(H))
  41. k = s_.shape[0]
  42. for i in range(k):
  43. s_ %%= 2 #不进位
  44. return s_
  45. def siranguji(e_dataset):
  46. '''
  47. 求每一个满足条件的e的概率
  48. '''
  49. p_mat = np.array([[6/7,1/7]
  50. ,[1/7,6/7]])
  51. P_ = []
  52. length = len(e_dataset)
  53. for i in range(0, length):
  54. P = 1
  55. for j in range(0,7):
  56. if e_dataset[i][j] == 1:
  57. P = P * p_mat[0][1]
  58. elif e_dataset[i][j] == 0:
  59. P = P * p_mat[0][0]
  60. P_.append(P)
  61. return P_
  62. C = np.zeros((12,))
  63. for i in range(0,pow(2,12)):
  64. D = np.zeros((1,12))
  65. i = bin(i)
  66. j = i[2:]
  67. j = j.zfill(12)
  68. C = shuzu(j,12)
  69. G = func(C)
  70. if (G== D).all():
  71. X = C #X为满足G * H_T = 0的解
  72. H = np.array(
  73. [[0, 0, 0, 1, 1, 1, 1],
  74. [0, 1, 1, 0, 0, 1, 1],
  75. [1, 0, 1, 0, 1, 0, 1]])
  76. print('构造的固定的校验矩阵H为:')
  77. print(H)
  78. G1 = np.eye(4)
  79. G2 = [[X[0], X[1], X[2]]
  80. , [X[3], X[4], X[5]]
  81. , [X[6], X[7], X[8]]
  82. , [X[9], X[10], X[11]]]
  83. G = np.concatenate((G1, G2), axis=1)
  84. print('相应的输出矩阵G为:')
  85. print(G)
  86. X = [1,0,0,0,1,0,0,1] #输入8位的x
  87. print('输入的x为:')
  88. print(X)
  89. X1 = X[0:4] #其中前4位做一次汉明编码
  90. X2 = X[4:8] #其中后4位做一次汉明编码
  91. C1 = np.dot(X1,G) #C = XG
  92. i = C1.shape[0]
  93. for j in range(i):
  94. C1[j] %%= 2 #不进位
  95. C2 = np.dot(X2,G)
  96. i = C2.shape[0]
  97. for j in range(i):
  98. C2[j] %%= 2 #不进位
  99. C = np.concatenate((C1,C2),axis=0)
  100. print('生成的C为:')
  101. print(C)
  102. r = [0,0,0,0,0,1,1,1,0,0,1,1,0,1]
  103. r1 = r[0:7] #前7位求一次伴随式
  104. r2 = r[7:] #后7位求一次伴随式
  105. s1 = np.dot(r1,np.transpose(H))
  106. i = s1.shape[0]
  107. for j in range(i):
  108. s1[j] %%= 2 #不进位
  109. s2 = np.dot(r2, np.transpose(H))
  110. i = s2.shape[0]
  111. for j in range(i):
  112. s2[j] %%= 2 #不进位
  113. s = np.concatenate((s1,s2),axis=0)
  114. print('s为:')
  115. print(s)
  116. e_dataset1 = []
  117. e_dataset2 = []
  118. for i in range(0,pow(2,7)):
  119. i = bin(i)
  120. j = i[2:]
  121. j = j.zfill(7)
  122. e = shuzu(j,7)
  123. s_ = func2(e)
  124. if (s_ == s1).all():
  125. e_dataset1.append(e) #求满足条件的e
  126. if (s_ == s2).all():
  127. e_dataset2.append(e)
  128. P_1 = siranguji(e_dataset1)
  129. index1 = np.argmax(P_1)
  130. e_1 = np.array(e_dataset1[index1],dtype=int) #求概率最大的e 前面的
  131. P_2 = siranguji(e_dataset2)
  132. index2 = np.argmax(P_2)
  133. e_2 = np.array(e_dataset2[index2],dtype=int) #求概率最大的e 后面的
  134. e_ = np.concatenate((e_1,e_2),axis=0)
  135. print('r为:')
  136. print(r)
  137. print('e为:')
  138. print(e_)
  139. for j in range(0,7):
  140. r_ = np.bitwise_xor(r,e_)
  141. print('c的估计为:')
  142. print(r_)

推荐阅读