def plotCodeDistance(): plt.figure() ax = plt.axes() ax.set_xticks(np.arange(0, 32, 2)) ax.set_yticks(np.arange(0, 64, 4)) x = [t for t in range(0, 32)] y = [2 * t + 1 for t in range(0, 32)] plt.plot(x, y, label='2t + 1') ranges = np.array([[0, 4], [1, 8], [5, 16], [13, 32]]) for q in range(3, 7): n = (1 << q) - 1 x = list() y = list() for t in range(ranges[q - 3][0], ranges[q - 3][1]): print(n, t) code = bch.BCH(n, t) x.append(t) y.append(code.dist()) plt.plot(x, y, label='n = ' + str(n)) plt.grid() plt.legend() plt.title('Code distance') plt.xlabel('t') plt.ylabel('Distance') plt.show()
def test(n, t, errors): result_euc = list() #result_pgz = list() code = bch.BCH(n, t) polynom = np.zeros(n + 1, int) polynom[0] = 1 polynom[-1] = 1 if gf.polydiv(polynom, code.g, code.pm)[1] != []: print('Error!!!') U = np.random.randint(0, 2, (100, n - code.g.size + 1)) V = code.encode(U) for v in V: if gf.polydiv(v, code.g, code.pm)[1] != []: print('Error!!!') if gf.polyval(v, code.R, code.pm).any(): print('Error!!!') for k in code.g: if k != 0 and k != 1: print('Error!!!') for err in errors: correct = 0 wrong = 0 detected = 0 W = noise(V, err) Vx = code.decode(W, 'euclid') for i in range(V.shape[0]): if np.array_equal(V[i], Vx[i]): correct += 1 elif not np.array_equal(Vx[i], Vx[i]): detected += 1 else: wrong += 1 print(correct, detected, wrong) result_euc.append([correct / 100, detected / 100, wrong / 100]) '''correct = 0 wrong = 0 detected = 0 Vx = code.decode(W, 'pgz') for i in range(V.shape[0]): if np.array_equal(V[i], Vx[i]): correct += 1 elif not np.array_equal(Vx[i], Vx[i]): detected += 1 else: wrong += 1 print(correct, detected, wrong) result_pgz.append((correct / 100, detected / 100, wrong / 100))''' return result_euc #, result_pgz
def codeTime(code_params, errors): euclid_time = list() pgz_time = list() for i, params in enumerate(code_params): print(params) code = bch.BCH(params[0], params[1]) U = np.random.randint(0, 2, (100, params[0] - code.g.size + 1)) W = code.encode(U) W = noise(W, errors[i]) start_time = time.process_time() code.decode(W, 'euclid') euclid_time.append(time.process_time() - start_time) start_time = time.process_time() code.decode(W, 'pgz') pgz_time.append(time.process_time() - start_time) return euclid_time, pgz_time
def code_stat(n, t): stat = numpy.empty((3, n)) N = 100 cur_bch = bch.BCH(n, t) block = rand.randint(0, 2, (N, cur_bch.k)) code = cur_bch.encode(block) tmp = numpy.arange(cur_bch.n) for real_err in range(1, n + 1): err_arr = numpy.zeros((N, cur_bch.n), dtype=int) for k in range(N): rand.shuffle(tmp) for i in range(real_err): err_arr[k][tmp[i]] = 1 bad_code = code ^ err_arr res = cur_bch.decode(bad_code, 'euclid') nice, err, rej = 0, 0, 0 for i in range(N): if numpy.array_equal(code[i], res[i]): nice += 1 elif res[i][0] == 2: rej += 1 else: err += 1 stat[0][real_err - 1] = nice / N * 100 stat[1][real_err - 1] = rej / N * 100 stat[2][real_err - 1] = err / N * 100 print(stat) all_err = [int(r) for r in range(1, n + 1)] # l1 = plt.plot(all_err, stat[0], color = 'b', linewidth = 1.2, label = 'decode') # l2 = plt.plot(all_err, stat[1], color = 'c', linewidth = 1.2, label = 'rejection') # l3 = plt.plot(all_err, stat[2], color = 'r', linewidth = 1.2, label = 'error') plt.bar(all_err, stat[0] + stat[1] + stat[2], linewidth=1.2, label='error') plt.bar(all_err, stat[0] + stat[1], linewidth=1.2, label='rejection') plt.bar(all_err, stat[0], linewidth=1.2, label='decode') plt.title('(n = ' + str(n) + ', t = ' + str(t) + ')') plt.legend() ax = plt.axes() ax.set_xticks(numpy.arange(1, n + 1, 3)) plt.ylabel('share of all code words, %') plt.xlabel('number of errors, t') plt.axvline(x=t, color='g', linestyle='-', linewidth=1) plt.tight_layout() plt.show() return
def plotCodeSpeed(): plt.figure() ax = plt.axes() ax.set_xticks(np.arange(0, 32, 2)) ax.set_yticks(np.arange(0, 1.1, 0.1)) for q in range(2, 7): n = (1 << q) - 1 x = list() y = list() for t in range(0, (n - 1 >> 1) + 1): code = bch.BCH(n, t) x.append(t) y.append((code.pm.shape[0] - code.g.size + 1.0) / n) plt.plot(x, y, label='n = ' + str(n)) plt.grid() plt.legend() plt.title('Code speed') plt.xlabel('t') plt.ylabel('Speed (k/n)') plt.show()
def codetime(r): n_t_arr = numpy.array( [[7, 15, 31, 63, 63, 127, 127], [1, 3, 5, 4, 11, 12, 21]], dtype=int) time_Euclid = numpy.empty(n_t_arr.shape[1]) time_pgz = numpy.empty(n_t_arr.shape[1]) N = 150 for i in range(n_t_arr.shape[1]): cur_bch = bch.BCH(n_t_arr[0][i], n_t_arr[1][i]) block = rand.randint(0, 2, (N, cur_bch.k)) code = cur_bch.encode(block) if r == 1: err_index = rand.randint(0, cur_bch.n, N) err_arr = numpy.zeros((N, cur_bch.n), dtype=int) for k, j in enumerate(err_index): err_arr[k][j] = 1 else: tmp = numpy.arange(cur_bch.n) err_index = numpy.empty((N, n_t_arr[1][i]), dtype=int) for k in range(N): rand.shuffle(tmp) err_index[k] = tmp[0:n_t_arr[1][i]] err_arr = numpy.zeros((N, cur_bch.n), dtype=int) for k in range(N): for j in err_index[k]: err_arr[k][j] = 1 bad_code = code ^ err_arr time_Euclid[i] = time.clock() res = cur_bch.decode(bad_code, 'euclid') time_Euclid[i] = (time.clock() - time_Euclid[i]) if not numpy.array_equal(res, code): print("AVOST") time_pgz[i] = time.clock() res = cur_bch.decode(bad_code, 'pgz') time_pgz[i] = (time.clock() - time_pgz[i]) if not numpy.array_equal(res, code): print("AVOST") return print(time_Euclid) print(time_pgz) return
#ber_list.append(ber_triple(lst, hamming_decoded)) sum2 += ber_triple(lst, hamming_decoded) #plt.plot(ber_list, [2], label='Kodowanie Hamminga(8, 4)', marker='o') # ======================================================================== packet_size = 1007 t = 10 redundancy = 1120 / 1007 ber_list = [] chunks = [ lst[x:x + packet_size] for x in range(0, len(lst), packet_size) ] bch_decoded_all2 = [] bch_obj = bch.BCH(8219, t) # bch_polynomial, bch_bits for each in chunks: bch_encoded2 = bch_obj.encode(each) bch_output2 = gilbert_lists(bch_encoded2, *parameter_list) bch_decoded2 = bch_obj.decode(bch_output2) bch_decoded_all2.append(bch_decoded2) bch_decoded_all_united2 = [] for each in bch_decoded_all2: bch_decoded_all_united2 += each #ber_list.append(ber_triple(lst, bch_decoded_all_united2)) sum3 += ber_triple(lst, bch_decoded_all_united2) #plt.plot(ber_list, [redundancy], label='Kodowanie BCH(1120, 1007)', marker='o') # ======================================================================== packet_size = 864