Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- """
- Random error generation.
- """
- def make_errors(string, err_am=1):
- err_am = random.randint(0, err_am)
- if err_am == 0:
- return string
- l = list(string)
- i = random.randint(0, len(string) - 1)
- if l[i] == "1":
- l[i] = "0"
- else:
- l[i] = "1"
- return "".join(l)
- """
- Code related to
- non-parity check.
- """
- """
- Code related to
- correlations code.
- """
- def correlations_encode(code):
- li = list(code)
- for i in range(len(li)):
- if (li[i] == "0"):
- li[i] = "01"
- i += 1
- else:
- li[i] = "10"
- i += 1
- return "".join(li)
- def correlations_decode(code):
- if code.find("111") == -1 and code.find("000") == -1:
- i = 0
- retVal = ""
- while(i < len(code)):
- symbol = code[i:i + 1]
- retVal += symbol
- i += 2
- return retVal
- return "Error"
- """
- Code related to
- Berger code.
- """
- def berge_encode(code):
- amount = 0
- for i in code:
- if i == "1":
- amount += 1
- code = code + ("0" * (17 - amount)) + str('{0:b}'.format(amount))
- return code
- def berger_decode(code):
- check = int(code[-16:],2)
- amount = 0
- code = code[:-16]
- for i in code:
- if (i == "1"):
- amount += 1
- if amount == check:
- return code
- return "Error"
- """
- Code related to
- Bauer code.
- """
- def count_ones(string):
- return str.count(string, "1")
- def bauer_encode(to_encode):
- def append_code(to_encode, inversed=False):
- if not inversed:
- return to_encode + to_encode
- else:
- l = list(to_encode)
- for el in l:
- if el == "1":
- to_encode += "0"
- else:
- to_encode += "1"
- return to_encode
- return append_code(to_encode, count_ones(to_encode) % 2 == 0)
- def bauer_decode(to_decode):
- def code_check(code, actual_code, inversed=False):
- # Return 0 if all's alright.
- l_c = list(code)
- l_t_c = list(actual_code)
- for i in range(len(l_c)):
- if inversed == (l_c == l_t_c):
- return i + 1
- return 0
- actual_len = round(len(to_decode) * 0.5)
- actual_code = to_decode[:actual_len]
- wrong_bit = code_check(to_decode, actual_code, count_ones(actual_code) % 2 == 0)
- if wrong_bit != 0:
- # return "Error at bit " + str(wrong_bit) + "."
- return "Error"
- return actual_code
- """
- Code related to
- Hamming Code.
- """
- def get_parity_bits_am(code):
- parityBits = 0
- while(2 ** parityBits < parityBits + len(code) + 1):
- parityBits += 1
- return parityBits
- def get_parity_groups(code):
- parity_bits_am = get_parity_bits_am(code)
- groups = {}
- for group in range(parity_bits_am):
- bit = 2 ** group
- for pos in range(len(code)):
- if (pos + 1) & bit:
- if len(groups) >= group + 1:
- groups[bit].append(pos)
- else:
- groups[bit] = [pos]
- return groups
- def get_parity(to_encode, group):
- gr_str = ""
- for el in group:
- gr_str += to_encode[el]
- return str(str.count(gr_str, "1") % 2)
- def hammington_encode(to_encode):
- def get_code(to_encode, groups):
- parity_bits_am = get_parity_bits_am(to_encode)
- code = []
- for i in range(len(to_encode) + parity_bits_am):
- code.append("0")
- for parity_bit in range(parity_bits_am):
- parity_val = 2 ** parity_bit
- if parity_val > len(to_encode):
- break
- group = groups[parity_val]
- code[parity_val - 1] = get_parity(to_encode, group)
- to_fill_with = list(to_encode)
- i = 0
- while(len(to_fill_with) > 0):
- i += 1
- if math.log2(i).is_integer():
- continue
- el = to_fill_with.pop()
- code[i - 1] = el
- return "".join(code)
- groups = get_parity_groups(to_encode)
- code = get_code(to_encode, groups)
- return code
- def hammington_decode(to_decode):
- def get_stripped(to_decode):
- stripped = ""
- parity_bits_am = get_parity_bits_am(to_decode)
- for i in range(len(to_decode)):
- if math.log2(i + 1).is_integer():
- continue
- stripped += to_decode[i]
- return stripped
- def code_check(to_decode, stripped, groups):
- wrong_bit = 0
- parity_bits_am = get_parity_bits_am(to_decode)
- for parity_bit in range(parity_bits_am):
- parity_val = 2 ** parity_bit
- if parity_val > len(groups):
- break
- group = groups[parity_val]
- check_val = to_decode[parity_val - 1]
- true_val = get_parity(stripped, group)
- if check_val != true_val:
- wrong_bit |= parity_val
- return wrong_bit
- stripped = get_stripped(to_decode)
- groups = get_parity_groups(stripped)
- wrong_bit = code_check(to_decode, stripped, groups)
- if wrong_bit != 0:
- new_code = list(to_decode)
- print("Wrong bit was", wrong_bit, "inversing it.")
- if new_code[wrong_bit - 1] == "1":
- new_code[wrong_bit - 1] = "0"
- else:
- new_code[wrong_bit - 1] = "1"
- to_decode = "".join(new_code)
- return get_stripped(to_decode)
- c = hammington_encode("1001")
- print(c)
- print(hammington_decode(c))
- c = correlations_encode("1001")
- print(c)
- print(correlations_decode(c))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement