def test_1024_rate_1_code(): # effectively a Monte-Carlo simulation for channel polarization. ntests = 10000 n = 256 k = n transition_prob = 0.11 num_transitions = int(k * transition_prob) frozenbits = np.zeros(n - k) frozenbitposition = np.array((), dtype=int) encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) channel_counter = np.zeros(k) possible_indices = np.arange(n, dtype=int) for i in range(ntests): bits = np.random.randint(2, size=k) tx = encoder.encode(bits) np.random.shuffle(possible_indices) tx[possible_indices[0:num_transitions]] = (tx[possible_indices[0:num_transitions]] + 1) % 2 rx = tx recv = decoder.decode(rx) channel_counter += (bits == recv) print channel_counter print(np.min(channel_counter), np.max(channel_counter)) np.save('channel_counter_' + str(ntests) + '.npy', channel_counter)
def test_reverse_enc_dec(): n = 16 k = 8 frozenbits = np.zeros(n - k) frozenbitposition = np.array((0, 1, 2, 3, 4, 5, 8, 9), dtype=int) bits = np.random.randint(2, size=k) encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) encoded = encoder.encode(bits) print 'encoded:', encoded rx = decoder.decode(encoded) print 'bits:', bits print 'rx :', rx print (bits == rx).all()
def test_reverse_enc_dec(): n = 16 k = 8 frozenbits = np.zeros(n - k) frozenbitposition = np.array((0, 1, 2, 3, 4, 5, 8, 9), dtype=int) bits = np.random.randint(2, size=k) encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) encoded = encoder.encode(bits) print 'encoded:', encoded rx = decoder.decode(encoded) print 'bits:', bits print 'rx :', rx print(bits == rx).all()
def test_enc_dec_chain(): ntests = 100 n = 256 k = n // 2 frozenbits = np.zeros(n - k) frozenbitposition = get_frozen_bit_position() for i in range(ntests): bits = np.random.randint(2, size=k) encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) encoded = encoder.encode(bits) rx = decoder.decode(encoded) if not is_equal(bits, rx): raise ValueError('Test #', i, 'failed, input and output differ', bits, '!=', rx) return
def compare_decoder_impls(): print '\nthis is decoder test' n = 8 k = 4 frozenbits = np.zeros(n - k) # frozenbitposition16 = np.array((0, 1, 2, 3, 4, 5, 8, 9), dtype=int) frozenbitposition = np.array((0, 1, 2, 4), dtype=int) bits = np.random.randint(2, size=k) print 'bits:', bits encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) encoded = encoder.encode(bits) print 'encoded:', encoded rx_st = decoder._lr_sc_decoder(encoded) rx_eff = decoder._lr_sc_decoder_efficient(encoded) print 'standard :', rx_st print 'efficient:', rx_eff print (rx_st == rx_eff).all()
def compare_decoder_impls(): print '\nthis is decoder test' n = 8 k = 4 frozenbits = np.zeros(n - k) # frozenbitposition16 = np.array((0, 1, 2, 3, 4, 5, 8, 9), dtype=int) frozenbitposition = np.array((0, 1, 2, 4), dtype=int) bits = np.random.randint(2, size=k) print 'bits:', bits encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) encoded = encoder.encode(bits) print 'encoded:', encoded rx_st = decoder._lr_sc_decoder(encoded) rx_eff = decoder._lr_sc_decoder_efficient(encoded) print 'standard :', rx_st print 'efficient:', rx_eff print(rx_st == rx_eff).all()
def main(): power = 3 n = 2 ** power k = 4 frozenbits = np.zeros(n - k, dtype=int) frozenbitposition = np.array((0, 1, 2, 4), dtype=int) frozenbitposition4 = np.array((0, 1), dtype=int) encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) bits = np.ones(k, dtype=int) print "bits: ", bits evec = encoder.encode(bits) print "froz: ", encoder._insert_frozen_bits(bits) print "evec: ", evec evec[1] = 0 deced = decoder._lr_sc_decoder(evec) print "SC decoded:", deced test_reverse_enc_dec() compare_decoder_impls()
def main(): power = 3 n = 2**power k = 4 frozenbits = np.zeros(n - k, dtype=int) frozenbitposition = np.array((0, 1, 2, 4), dtype=int) frozenbitposition4 = np.array((0, 1), dtype=int) encoder = PolarEncoder(n, k, frozenbitposition, frozenbits) decoder = PolarDecoder(n, k, frozenbitposition, frozenbits) bits = np.ones(k, dtype=int) print "bits: ", bits evec = encoder.encode(bits) print "froz: ", encoder._insert_frozen_bits(bits) print "evec: ", evec evec[1] = 0 deced = decoder._lr_sc_decoder(evec) print 'SC decoded:', deced test_reverse_enc_dec() compare_decoder_impls()