Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 44
#---------------------------------------------------------------- # Sample code for UEET2533 Information Theory and Coding # by Chong Zan Kai # Email [email protected] # Universiti Tunku Abdul Rahman, Malaysia. # # We demonstrate how to use repetition code to secure an image # against a BSC channel. # # Last modified on 20-Jan-2015. #---------------------------------------------------------------- #---------------------------------------------------------------- # Functions to convert image to matrix and vice versa. #---------------------------------------------------------------- import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np # # Convert image 2darray to GF2 matrix. # def img_arr_to_matrix(img_2darr): #img_2darr = mpimg.imread(bmp_file) img_2dlist = [] # # Convert to 0 and 1 first. for p4_arr in img_2darr: a_list = [int(p[0]>0) for p in p4_arr] img_2dlist.append(a_list) # img_mat = matrix(GF(2), img_2dlist) return img_mat # # Convert GF2 matrix back to image 2darr. # def img_matrix_to_arr(img_mat): img_2darr = [] for row in img_mat.rows(): pixel_list = [ np.uint8([int(r) * 255, int(r) * 255, int(r) * 255, 255 ]) for r in row] img_2darr.append(pixel_list) return np.array(img_2darr) # # Introduce noise to the message in BSC channel. # def bsc_channel(msg_mat, p_flip): new_msg_mat = copy (msg_mat) total_row, total_col = new_msg_mat.dimensions() for r in range(total_row): for c in range(total_col): rand_val = random() if rand_val <= p_flip: new_msg_mat[r, c] += 1 return new_msg_mat # # Repetition Code Encoder # def repetition_code_encoder(msg_mat, repetition): output_mat = msg_mat for i in range(repetition -1): output_mat = output_mat.augment(msg_mat) return output_mat # # Repetition Code Decoder # def repetition_code_decoder(coded_msg_mat, repetition): col_dim = coded_msg_mat.ncols() / repetition row_dim = coded_msg_mat.nrows() new_mat = zero_matrix(GF(2), row_dim, col_dim) for r in range(row_dim): for c in range(col_dim): val_list = [] for i in range(repetition): val = coded_msg_mat[r, c + (col_dim * i) ] val_list.append(val) #print 'val_list', val_list if val_list.count(0) > val_list.count(1): new_mat[r, c] = 0 else: new_mat[r, c] = 1 # return new_mat #---------------------------------------------------------------- #---------------------------------------------------------------- # Main #---------------------------------------------------------------- #---------------------------------------------------------------- # Main #---------------------------------------------------------------- p_flip = 0.1 # Noise probability sample_file = 'sample01.bmp' repetition = 3 # # Orignial image # # Read the image file as array img_arr = mpimg.imread(sample_file) # Display the image print 'Original image:' im = plt.imshow(img_arr) plt.show() print # Convert image array to GF2 matrix img_mat = img_arr_to_matrix (img_arr) print # # Encode with Repetition code. # encoded_img_mat = repetition_code_encoder(img_mat, repetition) # Display the image print 'Encoded image:' encoded_img_arr = img_matrix_to_arr(encoded_img_mat) im = plt.imshow(encoded_img_arr) plt.show() print # # Introduce noise to the image # recv_img_mat = bsc_channel(encoded_img_mat, p_flip) # # Present the received image # # Display image print 'Received image after BSC channel:' recv_img_arr = img_matrix_to_arr (recv_img_mat) im = plt.imshow(recv_img_arr) plt.show() print # # Without Decoder, we have ... # print 'Without repetition code, we get:' noise_img_mat = recv_img_mat.matrix_from_columns(range(recv_img_mat.ncols() / repetition)) noise_img_arr = img_matrix_to_arr (noise_img_mat) im = plt.imshow(noise_img_arr) plt.show() print # # Decoding with Repetition code # decoded_img_mat = repetition_code_decoder(recv_img_mat, repetition) # Display image print 'After decoded with repetition code:' decoded_img_arr = img_matrix_to_arr (decoded_img_mat) im = plt.imshow(decoded_img_arr) plt.show() print
Original image:
Encoded image:
Received image after BSC channel:
Without repetition code, we get:
After decoded with repetition code:
︠e42dc520-1947-4282-9b19-24341febe5fbi︠