def test_decoding_did_not_converge(systematic, sparse): n = 15 d_v = 4 d_c = 5 seed = 0 H, G = make_ldpc(n, d_v, d_c, seed=seed, systematic=systematic, sparse=sparse) assert not binaryproduct(H, G).any() n, k = G.shape snr = 1 v = np.arange(k) % 2 y = encode(G, v, snr, seed=seed) with pytest.warns(UserWarning, match="stopped before convergence"): decode(H, y, snr, maxiter=1)
def test_decoding(systematic, sparse): n = 15 d_v = 4 d_c = 5 seed = 0 H, G = make_ldpc(n, d_v, d_c, seed=seed, systematic=systematic, sparse=sparse) assert not binaryproduct(H, G).any() n, k = G.shape snr = 100 v = np.arange(k) % 2 y = encode(G, v, snr, seed) d = decode(H, y, snr) x = get_message(G, d) assert abs(v - x).sum() == 0
H, G = make_ldpc(n, d_v, d_c, seed=seed, systematic=True, sparse=True) n, k = G.shape print("Number of coded bits:", k) ################################################################## # Now we simulate transmission for different levels of noise and # compute the percentage of errors using the bit-error-rate score # To parallelize coding and decoding, simply stack the messages as columns: errors = [] snrs = np.linspace(-2, 10, 20) v = np.arange(k) % 2 # fixed k bits message n_trials = 50 # number of transmissions with different noise V = np.tile(v, (n_trials, 1)).T # stack v in columns for snr in snrs: y = encode(G, V, snr, seed=seed) D = decode(H, y, snr) error = 0. for i in range(n_trials): x = get_message(G, D[:, i]) error += abs(v - x).sum() / (k * n_trials) errors.append(error) plt.figure() plt.plot(snrs, errors, color="indianred") plt.ylabel("Bit error rate") plt.xlabel("SNR") plt.show()
# compute the percentage of errors using the bit-error-rate score # The coding and decoding can be done in parallel by column stacking. bers = [] fers = [] snrs = np.linspace(-2, 20, 20) # snrs = np.linspace(5,10,5) # v = np.arange(k) % 2 # fixed k bits message v = np.zeros(k) n_trials = 50 # number of transmissions with different noise V = np.tile(v, (n_trials, 1)).T # stack v in columns for snr in snrs: x = encode(G, V, snr, add_noise=True, seed=seed) y, weights = channel.AWGN_IS(x, snr, seed) D = decode(H, y, snr) ber = 0. # fer = 0. for i in range(n_trials): x_hat = get_message(G, D[:, i]) ber += abs(v - x_hat).sum() / (k * n_trials) # if abs(v - x).sum() != 0: # fer += 1 / n_trials fer = evaluation.fer_IS(D, x, weights) bers.append(ber) fers.append(fer) print(fers)
import numpy as np from pyldpc import make_ldpc, encode, decode, get_message n = 100 d_v = 2 d_c = 4 snr = 20 H, G = make_ldpc(n, d_v, d_c, systematic=False, sparse=True) print(H) print(G) k = G.shape[1] print(k) v = np.random.randint(2, size=k) print(v) import time start_time = time.time() y = encode(G, v, snr) print(y) d = decode(H, y, snr) print("--- %s seconds ---" % (time.time() - start_time)) print(d) x = get_message(G, d) print(x) assert abs(x - v).sum() == 0
# H and G. H is a regular parity-check matrix with d_v ones per row # and d_c ones per column H, G = make_ldpc(n, d_v, d_c, seed=seed, systematic=True, sparse=True) n, k = G.shape print("Number of coded bits:", k) ################################################################## # Now we simulate transmission for different levels of noise and # compute the percentage of errors using the bit-error-rate score errors = [] snrs = np.linspace(-2, 10, 20) v = np.arange(k) % 2 # fixed k bits message n_trials = 50 # number of transmissions with different noise for snr in snrs: error = 0. for ii in range(n_trials): y = encode(G, v, snr, seed=seed) d = decode(H, y, snr) x = get_message(G, d) error += abs(v - x).sum() / k errors.append(error / n_trials) plt.figure() plt.plot(snrs, errors, color="indianred") plt.ylabel("Bit error rate") plt.xlabel("SNR") plt.show()