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 encode(self, U): k = U.shape[1] V = np.zeros((len(U), len(self.pm)), int) V[:,:k] = U for i in range(len(V)): V[i] = gf.polyadd(V[i], gf.polydiv(V[i], self.g, self.pm)[1]) return V
def test_polydivmod(): pm = gf.gen_pow_matrix(37) p1 = np.array([13, 11, 29, 26, 31, 3]) p2 = np.array([19, 25, 31, 3, 14, 29]) right_answer = (np.array([21]), np.array([16, 23, 0, 23, 6])) result = gf.polydiv(p1, p2, pm) assert_equal(right_answer[0], result[0]) assert_equal(right_answer[1], result[1])
def do_correctness_test(self, q, t, get_msg=None): pbar = tqdm(total=8) n = 2 ** q - 1 code = BCH(n, t) pbar.update(1) self.assertTrue(np.logical_or((code.g == [0]), (code.g == [1])).all()) self.assertRaises(BaseException, lambda it: code.decode([np.zeros(n + 1).astype(int)])) pbar.update(1) x = np.zeros(n + 1).astype(int) x[0] = 1 x[-1] = 1 assert_array_equal(gf.polydiv(x, code.g, code.pm)[1], [0]) pbar.update(1) m = code.g.shape[0] - 1 #gf.polydeg(code.g) k = n - m self.assertRaises(BaseException, lambda it: code.encode([np.zeros(k + 1).astype(int)])) if get_msg is None: msg = [random.randint(0, 1) for _ in range(0, k)] else: msg = get_msg(k) coded = code.encode(np.array([msg]))[0] assert_array_equal(coded[0:k], msg) pbar.update(1) self.assertTrue((gf.polyval(coded, code.R, code.pm) == [0]).all()) pbar.update(1) assert_array_equal(gf.polydiv(coded, code.g, code.pm)[1], [0]) pbar.update(1) assert_array_equal(code.decode(np.array([coded]))[0], coded) pbar.update(1) if t >= 1: broken = np.array(coded) goig_to_make_count_mistakes = random.randint(0, t) mistakes = 0 i = 0 while mistakes < goig_to_make_count_mistakes and i < len(broken): if random.randint(0, 1) == 1: broken[i] = 1 - broken[i] mistakes += 1 i += 1 assert_array_equal(code.decode(np.array([broken]), method='pgz')[0], coded) assert_array_equal(code.decode(np.array([broken]), method='euclid')[0], coded) assert_array_equal(code.decode(np.array([broken]))[0], coded) pbar.update(1) pbar.close()
def do_polyprod_and_polydiv_test(self, p1, p2, pm): div = gf.polydiv(p1, p2, pm) mult = gf.polyprod(div[0], p2, pm) assert_array_equal( p1, gf.add( mult, np.concatenate( [np.zeros(len(mult) - len(div[1])).astype(int), div[1]])))
def encode(self, U): res = np.zeros((U.shape[0], U.shape[1] + self.g.shape[0] - 1)) for j in range(U.shape[0]): for i in range(U.shape[1]): res[j][i] = U[j][i] q, r = gf.polydiv(res[j], self.g, self.pm) for i in range(r.shape[0]): res[j][res.shape[1] - i - 1] = r[r.shape[0] - i - 1] return res
def encode(self, U): k = U.shape[1] n = self.pm.shape[0] result = np.empty((U.shape[0], n), int) for i, u in enumerate(U): result[i] = np.hstack((u, np.zeros(n - k))) result[i] = gf.polyadd(result[i], gf.polydiv(result[i], self.g, self.pm)[1]) return result
def encode(self, U): if self.k != U.shape[1]: print("Wrong matrix dimension") return np.nan u = np.concatenate((U, np.zeros((U.shape[0], self.m), int)), axis=1) v = np.zeros((u.shape[0], self.m), int) for i in range(u.shape[0]): v[i] = gf.expand(gf.polydiv(u[i], self.g, self.pm)[1], self.m) V = np.concatenate((U, v), axis=1) return V
def checker(self): for i in self.g: if i != 0 and i != 1: return False a = np.zeros(len(self.pm)+1, int) a[0] = a[-1] = 1 a = gf.polydiv(a, self.g, self.pm)[1] if a.size != 1 or a[0] != 0: return False return self.dist(check=True)
def encode(self, U): num_of_message = U.shape[0] answ = numpy.zeros((num_of_message, self.n), dtype=int) for i in range(num_of_message): for j in range(self.k): answ[i][j] = U[i][j] rem = gf.polydiv(answ[i], self.g, self.pm)[1] rem = numpy.hstack((numpy.zeros((self.m - rem.shape[0]), dtype=int), rem)) answ[i][self.k:] = rem return answ
def encode(self, U): """Кодирование БЧХ""" fi = [] x = np.zeros(self.g.shape[0], dtype=int) x[0] = 1 for word in U: first = polyprod(x, word, self.pm) second = polydiv(first, self.g, self.pm) v = sump(first, second[1]) if v.size < self.pm.shape[0] - 1: v = np.concatenate((np.zeros(self.pm.shape[0] - 1 - v.shape[0], dtype=int), v)) fi.append(v) return np.asarray(fi)
def encode(self, U): # res = x^m * u(x) - (x^m * u(x)) mod g(x) # |________| |________| # ^ ^ # | | # s + s mod g(x) message_number, k = U.shape m = self.g.size - 1 x_m = np.zeros(m + 1, dtype=int) x_m[0] = 1 res = np.zeros((message_number, k + m), dtype=int) for i in range(message_number): s = gf.polyprod(x_m, U[i, :], self.pm) mod = gf.polydiv(s, self.g, self.pm)[1] tmp = gf.polyadd(s, mod) res[i, -tmp.size:] = tmp.copy() return res
def dist(self, check=False): k = len(self.pm) - self.g.size + 1 d = len(self.pm) u = np.arange(k - 1, -1, -1).reshape(1,-1) for i in range(2**k): v = self.encode((i >> u) & 1).reshape(-1) if check: r = gf.polydiv(v, self.g, self.pm)[1] if r.size != 1 or r[0] != 0: return False s = gf.polyval(v, self.R, self.pm) for j in s: if j != 0: return False b = np.sum(v) if b < d and b != 0: d = b if check: return d >= self.R.size + 1 else: return d
def test_polydiv_zero(self): pm = gf.gen_pow_matrix(5391) for elem in pm[:, 1]: self.assertRaises( BaseException, lambda: gf.polydiv(np.array([elem]), np.array([0]), pm))
def test_polydiv_3(self): div = gf.polydiv(np.array([0b10, 0b1]), np.array([0b10, 0b0]), gf.gen_pow_matrix(0b1011)) assert_array_equal(div[0], [0b1]) assert_array_equal(div[1], [0b1])