MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / emitter_converter

Function emitter_converter

hashes/hamming_code.py:71–144  ·  view source on GitHub ↗

:param size_par: how many parity bits the message must have :param data: information bits :return: message to be transmitted by unreliable medium - bits of information merged with parity bits >>> emitter_converter(4, "101010111111") ['1', '1', '1', '1', '0', '1', '

(size_par, data)

Source from the content-addressed store, hash-verified

69
70# Functions of hamming code-------------------------------------------
71def emitter_converter(size_par, data):
72 """
73 :param size_par: how many parity bits the message must have
74 :param data: information bits
75 :return: message to be transmitted by unreliable medium
76 - bits of information merged with parity bits
77
78 >>> emitter_converter(4, "101010111111")
79 ['1', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1']
80 >>> emitter_converter(5, "101010111111")
81 Traceback (most recent call last):
82 ...
83 ValueError: size of parity don't match with size of data
84 """
85 if size_par + len(data) <= 2**size_par - (len(data) - 1):
86 raise ValueError("size of parity don't match with size of data")
87
88 data_out = []
89 parity = []
90 bin_pos = [bin(x)[2:] for x in range(1, size_par + len(data) + 1)]
91
92 # sorted information data for the size of the output data
93 data_ord = []
94 # data position template + parity
95 data_out_gab = []
96 # parity bit counter
97 qtd_bp = 0
98 # counter position of data bits
99 cont_data = 0
100
101 for x in range(1, size_par + len(data) + 1):
102 # Performs a template of bit positions - who should be given,
103 # and who should be parity
104 if qtd_bp < size_par:
105 if (np.log(x) / np.log(2)).is_integer():
106 data_out_gab.append("P")
107 qtd_bp = qtd_bp + 1
108 else:
109 data_out_gab.append("D")
110 else:
111 data_out_gab.append("D")
112
113 # Sorts the data to the new output size
114 if data_out_gab[-1] == "D":
115 data_ord.append(data[cont_data])
116 cont_data += 1
117 else:
118 data_ord.append(None)
119
120 # Calculates parity
121 for bp in range(1, size_par + 1):
122 # Bit counter one for a given parity
123 cont_bo = 0
124 # counter to control the loop reading
125 for cont_loop, x in enumerate(data_ord):
126 if x is not None:
127 try:
128 aux = (bin_pos[cont_loop])[-1 * (bp)]

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected