def test_003_depuncture_bits(self): N = 2**6 K = N // 2 eta = design_snr_to_bec_eta(0.0, 1.0) polar_capacities = calculate_bec_channel_capacities(eta, N) f = np.sort(get_frozenBitPositions(polar_capacities, N - K)) punc = pypolar.Puncturer(N - (N // 4), f) outputPositions = get_diff_positions(N, f[0:N // 4]) vec = np.random.normal(0.0, 1.0, N - (N // 4)).astype(np.float32) res = punc.depuncture(vec) ref = np.zeros(N, dtype=res.dtype) ref[outputPositions] = vec self.assertListEqual(ref.tolist(), res.tolist()) vec = np.random.normal(0.0, 1.0, N - (N // 4)).astype(np.float64) res = punc.depuncture(vec) ref = np.zeros(N, dtype=res.dtype) ref[outputPositions] = vec self.assertListEqual(ref.tolist(), res.tolist()) vec = np.random.randint(0, 256, N - (N // 4), dtype=np.uint8) res = punc.depuncture(vec) ref = np.zeros(N, dtype=res.dtype) ref[outputPositions] = vec self.assertListEqual(ref.tolist(), res.tolist())
def plot_capacity_histogram(design_snr, save_file=None): eta = design_snr_to_bec_eta(design_snr) # capacities = calculate_bec_channel_capacities(eta, block_size) try: import matplotlib.pyplot as plt # FUN with matplotlib LaTeX fonts! http://matplotlib.org/users/usetex.html plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.rc('figure', autolayout=True) block_sizes = [32, 128, 512] for b in block_sizes: capacities = calculate_bec_channel_capacities(eta, b) w = 1. / float(len(capacities)) weights = [ w, ] * b plt.hist(capacities, bins=b, weights=weights, range=(0.95, 1.0)) plt.grid() plt.xlabel('synthetic channel capacity') plt.ylabel('normalized item count') print(plt.gcf().get_size_inches()) plt.gcf().set_size_inches(plt.gcf().get_size_inches() * .5) if save_file: plt.savefig(save_file) plt.show() except ImportError: pass
def test_002_puncture_bits(self): N = 2**6 K = N // 2 eta = design_snr_to_bec_eta(0.0, 1.0) polar_capacities = calculate_bec_channel_capacities(eta, N) f = np.sort(get_frozenBitPositions(polar_capacities, N - K)) outputPositions = get_diff_positions(N, f[0:N // 4]) punc = pypolar.Puncturer(N - (N // 4), f) vec = np.random.randint(0, 256, N // 8, dtype=np.uint8) unpvec = np.unpackbits(vec) unpres = punc.puncture(unpvec) res = punc.puncturePacked(vec) self.assertListEqual(np.unpackbits(res).tolist(), unpres.tolist()) ref = unpvec[outputPositions] self.assertListEqual(ref.tolist(), unpres.tolist()) fvec = np.arange(N, dtype=np.float32) fres = punc.puncture(fvec) fref = fvec[outputPositions] self.assertListEqual(fref.tolist(), fres.tolist()) dvec = np.arange(N, dtype=np.float64) dres = punc.puncture(dvec) dref = dvec[outputPositions] self.assertListEqual(dref.tolist(), dres.tolist())
def main(): # plot_channel_coding_bounds() # plot_capacity_rate() # plot_r0_fer() # dsnr_db = 0.0 # eta = design_snr_to_bec_eta(dsnr_db, .5) eta = .5 capacities = calculate_bec_channel_capacities(eta, 2**5) # plot_channel_capacities(capacities) plot_channel_capacities(capacities, 'graph_polarized_channel.pgf')
def test_002_frozen_bit_positions(self): for snr in np.arange(-1.5, 3.5, .25): for inv_coderate in np.array([8, 6, 5, 4, 3, 2, 1.5, 1.2]): for n in range(6, 11): N = 2**n K = int(N / inv_coderate) # print(N, K, inv_coderate) cf = pypolar.frozen_bits(N, K, snr) eta = design_snr_to_bec_eta(snr, 1. * K / N) polar_capacities = calculate_bec_channel_capacities(eta, N) pf = get_frozenBitPositions(polar_capacities, N - K) pf = np.sort(pf) decoder = pypolar.PolarDecoder(N, 1, cf, "mixed") pd = decoder.frozenBits() self.assertListEqual(cf, pd) self.assertListEqual(cf, list(pf))
def plot_average_channel_distance(save_file=None): eta = 0.5 # design_snr_to_bec_eta(-1.5917) powers = np.arange(4, 26) try: import matplotlib.pyplot as plt import matplotlib # FUN with matplotlib LaTeX fonts! http://matplotlib.org/users/usetex.html plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.rc('figure', autolayout=True) dist = [] medians = [] initial_channel = 1 - eta for p in powers: bs = int(2**p) capacities = calculate_bec_channel_capacities(eta, bs) avg_capacity = np.repeat(initial_channel, len(capacities)) averages = np.abs(capacities - avg_capacity) avg_distance = np.sum(averages) / float(len(capacities)) dist.append(avg_distance) variance = np.std(averages) medians.append(variance) plt.errorbar(powers, dist, yerr=medians) plt.grid() plt.xlabel(r'block size $N$') plt.ylabel(r'$\frac{1}{N} \sum_i |I(W_N^{(i)}) - 0.5|$') axes = plt.axes() tick_values = np.array(axes.get_xticks().tolist()) tick_labels = np.array(tick_values, dtype=int) tick_labels = ['$2^{' + str(i) + '}$' for i in tick_labels] plt.xticks(tick_values, tick_labels) plt.xlim((powers[0], powers[-1])) plt.ylim((0.2, 0.5001)) plt.gcf().set_size_inches(plt.gcf().get_size_inches() * .5) if save_file: plt.savefig(save_file) plt.show() except ImportError: pass
def test_001_encode_systematic(self): frozenBitMap = np.array([0, -1, 0, -1, 0, -1, -1, -1], dtype=int) for i in range(100): u = np.random.randint(0, 2, 5) Y, X = polar_encode_systematic(u, 8, frozenBitMap) xm = encode_systematic_matrix(u, 8, frozenBitMap) self.assertTrue(np.all(X == xm)) N = 2 ** 6 K = N // 2 eta = design_snr_to_bec_eta(-1.59, 1.0) polar_capacities = calculate_bec_channel_capacities(eta, N) frozenBitMap = get_frozenBitMap(polar_capacities, N - K) # print(frozenBitMap) for i in range(100): u = np.random.randint(0, 2, K) Y, X = polar_encode_systematic(u, N, frozenBitMap) xm = encode_systematic_matrix(u, N, frozenBitMap) self.assertTrue(np.all(X == xm))
def validate_config(self, N, K, snr): eta = design_snr_to_bec_eta(snr, 1.0) polar_capacities = calculate_bec_channel_capacities(eta, N) f = get_frozenBitPositions(polar_capacities, N - K) f = np.sort(f) frozenBitMap = get_frozenBitMap(polar_capacities, N - K) info_pos = np.setdiff1d(np.arange(N, dtype=f.dtype), f) self.assertEqual(info_pos.size, K) self.assertEqual(f.size, N - K) self.assertEqual(np.sum(frozenBitMap), -K) p = pypolar.PolarEncoder(N, f) self.assertEqual(p.blockLength(), N) self.assertTrue(np.all(f == p.frozenBits())) self.assertTrue(np.all(f == np.arange(N)[np.where(frozenBitMap == 0)])) self.assertTrue(p.isSystematic()) p.setSystematic(False) self.assertFalse(p.isSystematic()) p.setSystematic(True) self.assertTrue(p.isSystematic()) self.check_matrix_domination_contiguity(N, p.frozenBits())
def test_001_setup(self): for n in range(5, 11): N = 2**n K = N // 2 eta = design_snr_to_bec_eta(0.0, 1.0) polar_capacities = calculate_bec_channel_capacities(eta, N) f = np.sort(get_frozenBitPositions(polar_capacities, N - K)) outputPositions0 = get_diff_positions(N, f[0:N // 4]) punc0 = pypolar.Puncturer(N - (N // 4), f) self.assertEqual(punc0.parentBlockLength(), N) self.assertEqual(punc0.blockLength(), N - (N // 4)) self.assertListEqual(punc0.blockOutputPositions(), outputPositions0.tolist()) outputPositions1 = get_diff_positions(N, f[0:N // 8]) punc1 = pypolar.Puncturer(N - (N // 8), f) self.assertEqual(punc1.parentBlockLength(), N) self.assertEqual(punc1.blockLength(), N - (N // 8)) self.assertListEqual(punc1.blockOutputPositions(), outputPositions1.tolist())
def matrix_validation(self, N, K, snr): eta = design_snr_to_bec_eta(snr, 1.0) polar_capacities = calculate_bec_channel_capacities(eta, N) f = np.sort(get_frozenBitPositions(polar_capacities, N - K)) ip = np.setdiff1d(np.arange(N, dtype=f.dtype), f) frozenBitMap = get_frozenBitMap(polar_capacities, N - K) n = int(np.log2(N)) G = get_polar_generator_matrix(n) Gs = get_polar_encoder_matrix_systematic(N, f) for i in range(10): u = np.random.randint(0, 2, K).astype(dtype=np.uint8) x = np.zeros(N, dtype=np.uint8) x[ip] = u xref = np.copy(frozenBitMap) xref[np.where(frozenBitMap == -1)] = u self.assertTrue(np.all(x == xref)) x = x.dot(G) % 2 x[f] = 0 x = x.dot(G) % 2 xs = u.dot(Gs) % 2 self.assertTrue(np.all(x == xs)) self.matrix_gen_check_validation(Gs, f)
def get_polar_capacities(N, snr): eta = design_snr_to_bec_eta(snr, 1.0) return calculate_bec_channel_capacities(eta, N)
def calculate_code_properties(N, K, design_snr_db): eta = design_snr_to_bec_eta(design_snr_db, 1.0 * K / N) polar_capacities = calculate_bec_channel_capacities(eta, N) frozenBitMap = get_frozenBitMap(polar_capacities, N - K) f = pypolar.frozen_bits(N, K, design_snr_db) p = pypolar.PolarEncoder(N, f) Gp = get_polar_generator_matrix(int(np.log2(N))) print(Gp) assert np.all(np.where(frozenBitMap > -1) == f) numInfoWords = 2 ** K n_prepend_bits = int(8 * np.ceil(K / 8.) - K) print(n_prepend_bits) weights = {} for i in range(numInfoWords): # b = np.binary_repr(i, K + n_prepend_bits) b = np.binary_repr(i, K) u = np.array([int(l) for l in b], dtype=np.uint8) # nb = np.concatenate((np.zeros(n_prepend_bits, dtype=nb.dtype), nb)) nbp = np.packbits(u) cw = p.encode_vector(nbp) # xm = encode_systematic_matrix(u, N, frozenBitMap) c = np.unpackbits(cw) # assert np.all(xm == c) weight = np.sum(c) if weight in weights: weights[weight] += 1 else: weights[weight] = 1 # nb = bin(i) # print(i, b, u, nbp, c) print(f) print(frozenBitMap) # print(n_prepend_bits) weights.pop(0) print(weights) dmin_ext_search = np.min(weights.keys()) print(dmin_ext_search) # validate_systematic_matrix(N, f, frozenBitMap) Gs = get_polar_encoder_matrix_systematic(N, f) P = Gs[:, f] # print(P) G = np.hstack((np.identity(K, dtype=Gs.dtype), P)) H = np.hstack((P.T, np.identity(N - K, dtype=Gs.dtype))) # print(P) print(H) # print(G.dot(H.T) % 2) # # print(Gs.dot(Gs.T) % 2) print(np.linalg.matrix_rank(H)) dmin_H = np.min(np.sum(H, axis=1)) dmin_P = 1 + np.min(np.sum(P, axis=1)) print(np.sum(H, axis=1)) print(np.sum(P, axis=1)) print('search {} vs {} H, P{}'.format(dmin_ext_search, dmin_H, dmin_P)) assert dmin_ext_search == dmin_P