def test_image_rgb(systematic, log, sparse): n = 26 d_v = 1 d_c = 2 seed = 0 rnd = np.random.RandomState(seed) 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 img = rnd.randint(0, 255, size=(3, 3, 3)) img_bin = rgb2bin(img) coded, noisy = ldpc_images.encode_img(G, img_bin, snr, seed) x = ldpc_images.decode_img(G, H, coded, snr, maxiter=100, log=log) assert ldpc_images.ber_img(img_bin, rgb2bin(x)) == 0
def test_image_gray(systematic, sparse): n = 100 d_v = 3 d_c = 4 seed = 0 rnd = np.random.RandomState(seed) 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 = 10 img = rnd.randint(0, 255, size=(3, 3)) img_bin = gray2bin(img) img_shape = img_bin.shape coded, noisy = ldpc_images.encode_img(G, img_bin, snr, seed) x = ldpc_images.decode_img(G, H, coded, snr, img_shape=img_shape) assert ldpc_images.ber_img(img_bin, gray2bin(x)) == 0
d_c = 4 seed = 42 ################################################################## # First we create a small LDPC code i.e a pair of decoding and coding matrices # 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) ################################################################## # Now we simulate the transmission with Gaussian white noise # and recover the original image via belief-propagation. snr = 8 eye_coded, eye_noisy = ldpc_images.encode_img(G, eye_bin, snr, seed=seed) print("Coded eye shape", eye_coded.shape) t = time() eye_decoded = ldpc_images.decode_img(G, H, eye_coded, snr, eye_bin.shape) t = time() - t print("Eye | Decoding time: ", t) error_decoded_eye = abs(eye - eye_decoded).mean() error_noisy_eye = abs(eye_noisy - eye).mean() ################################################################## # With RGB images, we proceed similarly print("\n\n") tiger = np.asarray(Image.open("data/tiger.jpg"))
d_c = 4 seed = 42 ################################################################## # First we create a small LDPC code i.e a pair of decoding and coding matrices # 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) ################################################################## # Now we simulate the transmission with Gaussian white noise # and recover the original image via belief-propagation. snr = 8 tree_coded, tree_noisy = ldpc_images.encode_img(G, tree_bin, snr, seed=seed) print("Coded tree shape", tree_coded.shape) t = time() tree_decoded = ldpc_images.decode_img(G, H, tree_coded, snr, tree_bin.shape) t = time() - t print("tree | Decoding time: ", t) error_decoded_tree = abs(tree - tree_decoded).mean() error_noisy_tree = abs(tree_noisy - tree).mean() plt.imshow(tree, 'gray') plt.show() plt.imshow(tree_noisy, 'gray') plt.show() plt.imshow(tree_decoded, 'gray')