scripts init

This commit is contained in:
Jinghao Shi
2017-04-03 12:52:21 -04:00
parent 9edf1899bd
commit 20a33eb560
31 changed files with 4729 additions and 0 deletions

View File

@ -0,0 +1,21 @@
# Authors: Veeresh Taranalli <veeresht@gmail.com>
# License: BSD 3-Clause
from numpy import array
from numpy.testing import assert_array_equal
from commpy.channelcoding.algcode import cyclic_code_genpoly
class TestAlgebraicCoding(object):
def test_cyclic_code_gen_poly(self):
code_lengths = array([15, 31])
code_dims = array([4, 21])
desired_genpolys = array([[2479, 3171, 3929],
[1653, 1667, 1503, 1207, 1787, 1561, 1903,
1219, 1137, 2013, 1453, 1897, 1975, 1395, 1547]])
count = 0
for n, k in zip(code_lengths, code_dims):
genpolys = cyclic_code_genpoly(n, k)
assert_array_equal(genpolys, desired_genpolys[count])
count += 1

View File

@ -0,0 +1,87 @@
# Authors: Veeresh Taranalli <veeresht@gmail.com>
# License: BSD 3-Clause
from numpy import array
from numpy.random import randint
from numpy.testing import assert_array_equal
from commpy.channelcoding.convcode import Trellis, conv_encode, viterbi_decode
class TestConvCode(object):
@classmethod
def setup_class(cls):
# Convolutional Code 1: G(D) = [1+D^2, 1+D+D^2]
memory = array([2])
g_matrix = array([[0o5, 0o7]])
cls.code_type_1 = 'default'
cls.trellis_1 = Trellis(memory, g_matrix, 0, cls.code_type_1)
cls.desired_next_state_table_1 = array([[0, 2],
[0, 2],
[1, 3],
[1, 3]])
cls.desired_output_table_1 = array([[0, 3],
[3, 0],
[1, 2],
[2, 1]])
# Convolutional Code 2: G(D) = [1 1+D+D^2/1+D]
memory = array([2])
g_matrix = array([[0o1, 0o7]])
feedback = 0o5
cls.code_type_2 = 'rsc'
cls.trellis_2 = Trellis(memory, g_matrix, feedback, cls.code_type_2)
cls.desired_next_state_table_2 = array([[0, 2],
[2, 0],
[1, 3],
[3, 1]])
cls.desired_output_table_2 = array([[0, 3],
[0, 3],
[1, 2],
[1, 2]])
@classmethod
def teardown_class(cls):
pass
def test_next_state_table(self):
assert_array_equal(self.trellis_1.next_state_table, self.desired_next_state_table_1)
assert_array_equal(self.trellis_2.next_state_table, self.desired_next_state_table_2)
def test_output_table(self):
assert_array_equal(self.trellis_1.output_table, self.desired_output_table_1)
assert_array_equal(self.trellis_2.output_table, self.desired_output_table_2)
def test_conv_encode(self):
pass
def test_viterbi_decode(self):
pass
def test_conv_encode_viterbi_decode(self):
niters = 10
blocklength = 1000
for i in range(niters):
msg = randint(0, 2, blocklength)
coded_bits = conv_encode(msg, self.trellis_1)
decoded_bits = viterbi_decode(coded_bits.astype(float), self.trellis_1, 15)
assert_array_equal(decoded_bits[:-2], msg)
coded_bits = conv_encode(msg, self.trellis_1)
coded_syms = 2.0*coded_bits - 1
decoded_bits = viterbi_decode(coded_syms, self.trellis_1, 15, 'unquantized')
assert_array_equal(decoded_bits[:-2], msg)
coded_bits = conv_encode(msg, self.trellis_2)
decoded_bits = viterbi_decode(coded_bits.astype(float), self.trellis_2, 15)
assert_array_equal(decoded_bits[:-2], msg)
coded_bits = conv_encode(msg, self.trellis_2)
coded_syms = 2.0*coded_bits - 1
decoded_bits = viterbi_decode(coded_syms, self.trellis_2, 15, 'unquantized')
assert_array_equal(decoded_bits[:-2], msg)

View File

@ -0,0 +1,68 @@
# Authors: Veeresh Taranalli <veeresht@gmail.com>
# License: BSD 3 clause
from numpy import array, ones_like, arange
from numpy.testing import assert_array_almost_equal, assert_array_equal, assert_, assert_equal
from commpy.channelcoding.gfields import GF
class TestGaloisFields(object):
def test_closure(self):
for m in arange(1, 9):
x = GF(arange(2**m), m)
for a in x.elements:
for b in x.elements:
assert_((GF(array([a]), m) + GF(array([b]), m)).elements[0] in x.elements)
assert_((GF(array([a]), m) * GF(array([b]), m)).elements[0] in x.elements)
def test_addition(self):
m = 3
x = GF(arange(2**m), m)
y = GF(array([6, 4, 3, 1, 2, 0, 5, 7]), m)
z = GF(array([6, 5, 1, 2, 6, 5, 3, 0]), m)
assert_array_equal((x+y).elements, z.elements)
def test_multiplication(self):
m = 3
x = GF(array([7, 6, 5, 4, 3, 2, 1, 0]), m)
y = GF(array([6, 4, 3, 1, 2, 0, 5, 7]), m)
z = GF(array([4, 5, 4, 4, 6, 0, 5, 0]), m)
assert_array_equal((x*y).elements, z.elements)
def test_tuple_form(self):
m = 3
x = GF(arange(0, 2**m-1), m)
y = x.power_to_tuple()
z = GF(array([1, 2, 4, 3, 6, 7, 5]), m)
assert_array_equal(y.elements, z.elements)
def test_power_form(self):
m = 3
x = GF(arange(1, 2**m), m)
y = x.tuple_to_power()
z = GF(array([0, 1, 3, 2, 6, 4, 5]), m)
assert_array_equal(y.elements, z.elements)
m = 4
x = GF(arange(1, 2**m), m)
y = x.tuple_to_power()
z = GF(array([0, 1, 4, 2, 8, 5, 10, 3, 14, 9, 7, 6, 13, 11, 12]), m)
assert_array_equal(y.elements, z.elements)
def test_order(self):
m = 4
x = GF(arange(1, 2**m), m)
y = x.order()
z = array([1, 15, 15, 15, 15, 3, 3, 5, 15, 5, 15, 5, 15, 15, 5])
assert_array_equal(y, z)
def test_minpols(self):
m = 4
x = GF(arange(2**m), m)
z = array([2, 3, 19, 19, 19, 19, 7, 7, 31, 25, 31, 25, 31, 25, 25, 31])
assert_array_equal(x.minpolys(), z)
m = 6
x = GF(array([2, 8, 32, 6, 24, 35, 10, 40, 59, 41, 14, 37]), m)
z = array([67, 87, 103, 73, 13, 109, 91, 117, 7, 115, 11, 97])
assert_array_equal(x.minpolys(), z)

View File

@ -0,0 +1,62 @@
# Authors: Veeresh Taranalli <veeresht@gmail.com>
# License: BSD 3-Clause
from numpy import array, sqrt, zeros
from numpy.random import randn
from numpy.testing import assert_allclose
from commpy.channelcoding.ldpc import get_ldpc_code_params, ldpc_bp_decode
from commpy.utilities import hamming_dist
import os
from nose.plugins.attrib import attr
@attr('slow')
class TestLDPCCode(object):
@classmethod
def setup_class(cls):
dir = os.path.dirname(__file__)
ldpc_design_file_1 = os.path.join(dir, '../designs/ldpc/gallager/96.33.964.txt')
#ldpc_design_file_1 = "../designs/ldpc/gallager/96.33.964.txt"
cls.ldpc_code_params = get_ldpc_code_params(ldpc_design_file_1)
@classmethod
def teardown_class(cls):
pass
def test_ldpc_bp_decode(self):
N = 96
k = 48
rate = 0.5
Es = 1.0
snr_list = array([2.0, 2.5])
niters = 10000000
tx_codeword = zeros(N, int)
ldpcbp_iters = 100
fer_array_ref = array([200.0/1000, 200.0/2000])
fer_array_test = zeros(len(snr_list))
for idx, ebno in enumerate(snr_list):
noise_std = 1/sqrt((10**(ebno/10.0))*rate*2/Es)
fer_cnt_bp = 0
for iter_cnt in range(niters):
awgn_array = noise_std * randn(N)
rx_word = 1-(2*tx_codeword) + awgn_array
rx_llrs = 2.0*rx_word/(noise_std**2)
[dec_word, out_llrs] = ldpc_bp_decode(rx_llrs, self.ldpc_code_params, 'SPA',
ldpcbp_iters)
num_bit_errors = hamming_dist(tx_codeword, dec_word)
if num_bit_errors > 0:
fer_cnt_bp += 1
if fer_cnt_bp >= 200:
fer_array_test[idx] = float(fer_cnt_bp)/(iter_cnt+1)
break
assert_allclose(fer_array_test, fer_array_ref, rtol=2e-1, atol=0)