Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. import math
  2. import random
  3.  
  4. """
  5. Random error generation.
  6. """
  7. def make_errors(string, err_am=1):
  8. err_am = random.randint(0, err_am)
  9. if err_am == 0:
  10. return string
  11. l = list(string)
  12. i = random.randint(0, len(string) - 1)
  13. if l[i] == "1":
  14. l[i] = "0"
  15. else:
  16. l[i] = "1"
  17. return "".join(l)
  18.  
  19. """
  20. Code related to
  21. non-parity check.
  22. """
  23.  
  24.  
  25. """
  26. Code related to
  27. correlations code.
  28. """
  29. def correlations_encode(code):
  30. li = list(code)
  31. for i in range(len(li)):
  32. if (li[i] == "0"):
  33. li[i] = "01"
  34. i += 1
  35. else:
  36. li[i] = "10"
  37. i += 1
  38. return "".join(li)
  39.  
  40. def correlations_decode(code):
  41. if code.find("111") == -1 and code.find("000") == -1:
  42. i = 0
  43. retVal = ""
  44. while(i < len(code)):
  45. symbol = code[i:i + 1]
  46. retVal += symbol
  47. i += 2
  48. return retVal
  49. return "Error"
  50.  
  51. """
  52. Code related to
  53. Berger code.
  54. """
  55. def berge_encode(code):
  56. amount = 0
  57. for i in code:
  58. if i == "1":
  59. amount += 1
  60. code = code + ("0" * (17 - amount)) + str('{0:b}'.format(amount))
  61. return code
  62.  
  63. def berger_decode(code):
  64. check = int(code[-16:],2)
  65. amount = 0
  66. code = code[:-16]
  67. for i in code:
  68. if (i == "1"):
  69. amount += 1
  70. if amount == check:
  71. return code
  72. return "Error"
  73.  
  74. """
  75. Code related to
  76. Bauer code.
  77. """
  78. def count_ones(string):
  79. return str.count(string, "1")
  80.  
  81. def bauer_encode(to_encode):
  82. def append_code(to_encode, inversed=False):
  83. if not inversed:
  84. return to_encode + to_encode
  85. else:
  86. l = list(to_encode)
  87. for el in l:
  88. if el == "1":
  89. to_encode += "0"
  90. else:
  91. to_encode += "1"
  92. return to_encode
  93.  
  94. return append_code(to_encode, count_ones(to_encode) % 2 == 0)
  95.  
  96. def bauer_decode(to_decode):
  97. def code_check(code, actual_code, inversed=False):
  98. # Return 0 if all's alright.
  99. l_c = list(code)
  100. l_t_c = list(actual_code)
  101.  
  102. for i in range(len(l_c)):
  103. if inversed == (l_c == l_t_c):
  104. return i + 1
  105. return 0
  106.  
  107. actual_len = round(len(to_decode) * 0.5)
  108. actual_code = to_decode[:actual_len]
  109.  
  110. wrong_bit = code_check(to_decode, actual_code, count_ones(actual_code) % 2 == 0)
  111. if wrong_bit != 0:
  112. # return "Error at bit " + str(wrong_bit) + "."
  113. return "Error"
  114. return actual_code
  115.  
  116. """
  117. Code related to
  118. Hamming Code.
  119. """
  120. def get_parity_bits_am(code):
  121. parityBits = 0
  122. while(2 ** parityBits < parityBits + len(code) + 1):
  123. parityBits += 1
  124. return parityBits
  125.  
  126. def get_parity_groups(code):
  127. parity_bits_am = get_parity_bits_am(code)
  128.  
  129. groups = {}
  130.  
  131. for group in range(parity_bits_am):
  132. bit = 2 ** group
  133. for pos in range(len(code)):
  134. if (pos + 1) & bit:
  135. if len(groups) >= group + 1:
  136. groups[bit].append(pos)
  137. else:
  138. groups[bit] = [pos]
  139.  
  140. return groups
  141.  
  142. def get_parity(to_encode, group):
  143. gr_str = ""
  144. for el in group:
  145. gr_str += to_encode[el]
  146. return str(str.count(gr_str, "1") % 2)
  147.  
  148. def hammington_encode(to_encode):
  149. def get_code(to_encode, groups):
  150. parity_bits_am = get_parity_bits_am(to_encode)
  151.  
  152. code = []
  153. for i in range(len(to_encode) + parity_bits_am):
  154. code.append("0")
  155.  
  156. for parity_bit in range(parity_bits_am):
  157. parity_val = 2 ** parity_bit
  158. if parity_val > len(to_encode):
  159. break
  160.  
  161. group = groups[parity_val]
  162.  
  163. code[parity_val - 1] = get_parity(to_encode, group)
  164.  
  165.  
  166.  
  167. to_fill_with = list(to_encode)
  168. i = 0
  169. while(len(to_fill_with) > 0):
  170. i += 1
  171. if math.log2(i).is_integer():
  172. continue
  173. el = to_fill_with.pop()
  174. code[i - 1] = el
  175.  
  176. return "".join(code)
  177.  
  178.  
  179. groups = get_parity_groups(to_encode)
  180. code = get_code(to_encode, groups)
  181. return code
  182.  
  183. def hammington_decode(to_decode):
  184. def get_stripped(to_decode):
  185. stripped = ""
  186. parity_bits_am = get_parity_bits_am(to_decode)
  187.  
  188. for i in range(len(to_decode)):
  189. if math.log2(i + 1).is_integer():
  190. continue
  191. stripped += to_decode[i]
  192. return stripped
  193.  
  194. def code_check(to_decode, stripped, groups):
  195. wrong_bit = 0
  196.  
  197. parity_bits_am = get_parity_bits_am(to_decode)
  198. for parity_bit in range(parity_bits_am):
  199. parity_val = 2 ** parity_bit
  200. if parity_val > len(groups):
  201. break
  202.  
  203. group = groups[parity_val]
  204.  
  205. check_val = to_decode[parity_val - 1]
  206. true_val = get_parity(stripped, group)
  207.  
  208. if check_val != true_val:
  209. wrong_bit |= parity_val
  210. return wrong_bit
  211.  
  212. stripped = get_stripped(to_decode)
  213. groups = get_parity_groups(stripped)
  214. wrong_bit = code_check(to_decode, stripped, groups)
  215. if wrong_bit != 0:
  216. new_code = list(to_decode)
  217. print("Wrong bit was", wrong_bit, "inversing it.")
  218. if new_code[wrong_bit - 1] == "1":
  219. new_code[wrong_bit - 1] = "0"
  220. else:
  221. new_code[wrong_bit - 1] = "1"
  222. to_decode = "".join(new_code)
  223. return get_stripped(to_decode)
  224.  
  225. c = hammington_encode("1001")
  226. print(c)
  227. print(hammington_decode(c))
  228.  
  229. c = correlations_encode("1001")
  230. print(c)
  231. print(correlations_decode(c))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement