Example #1
0
 def construct_tg_matrix(self, h_matrix=None):
     if h_matrix.any() != None:
         tG = pyldpc.CodingMatrix(h_matrix)
         #G's shape is actually the tuple (message's length, codewod's length)
         n, k = tG.shape
         self.current_tg_matrix = tG
         return tG
     elif self.current_tg_matrix != None:
         return self.current_tg_matrix
Example #2
0
def __LDPCGenerate__(n, rate=0.5):
    '''
    LDPC Generate
    inputs:
    	n: number of messages to encode. We use integers 0:n-1 to represent the n messages
    	rate: the rate of LDPC code, float number in (0, 1)
    returns:
    	message_length: length (in bits) of the encoded messages
    	code_length: length (in bits) of the codewords
    	codeword_mat: a matrix of size (code_length, n). The i-th column stores the
    				  codeword for the i-th message
    	ldpc_code: an ldpc code object
    	G: ldpc encode matrix
    '''
    dv = 3
    dc = 6
    log_n = int(math.ceil(math.log(n, 2)))
    flag = False
    H = 0
    G = 0
    code_length = 0

    while flag == False:
        code_length = int(math.ceil(log_n / float(rate)))
        if code_length % dc != 0:
            # dc must divide code_length
            code_length += (dc - code_length % dc)
            H = pyldpc.RegularH(code_length, dv, dc)
            G = pyldpc.CodingMatrix(H)
            if G.shape[1] >= log_n:
                flag = True
            else:
                rate = rate / 2.0

    # Now H is the binary check matrix and G is the encoding matrix
    # Size of G is (code_length, message_length)
    message_length = G.shape[1]
    # Convert integers in xrange(n) to binary numbers
    binary_mat = zeros([message_length, n], dtype=int)
    for i in xrange(n):
        bin_str = np.binary_repr(i, log_n)
        for j in xrange(log_n):
            binary_mat[j, i] = ord(bin_str[log_n - j - 1]) - 48

    codeword_mat = (dot(G, binary_mat)) % 2

    check_node_num = code_length * dv / dc
    L = []
    for i in range(check_node_num):
        non_zero_pos = np.where(H[i, :] != 0)
        L.append((non_zero_pos[0]).tolist())

    E = reduce(lambda x, y: x + y, map(lambda z: len(z), L))
    ldpc_code = LDPCCode(code_length, check_node_num, E, L)

    return message_length, code_length, codeword_mat, ldpc_code, G
Example #3
0
def encoding(k=8, N=16, H=None):
    if (k == 8 and N == 16):
        H = [[0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0],
             [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
             [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1],
             [0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0],
             [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1],
             [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
             [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0]]
    tG = pyldpc.CodingMatrix(H)
    return tG
Example #4
0
 def initEncoder(self):
     self.j=3;
     if(self.encoder_type=='hamming'):
         n_hamm = 2**self.j - 1;
         k_hamm = n_hamm-self.j
         r = k_hamm/n_hamm
         self.N = np.int(self.k/r)
     if(self.encoder_type=='ldpc'):
         self.k=6;
         self.N,d_v,d_c = 15,4,5;
         self.H = pyldpc.RegularH(self.N,d_v,d_c)
         self.Gt = pyldpc.CodingMatrix(self.H);            
Example #5
0
def LDPCEncoder(n, rate=0.5):
    dv = 3
    dc = 6
    log_n = int(math.ceil(math.log(n, 2)))
    flag = False
    H = 0
    G = 0
    code_length = 0

    while flag == False:
        code_length = int(math.ceil(log_n / float(rate)))
        if code_length % dc != 0:
            # dc must divide code_length
            code_length += (dc - code_length % dc)
        H = pyldpc.RegularH(code_length, dv, dc)
        G = pyldpc.CodingMatrix(H)
        if G.shape[1] >= log_n:
            flag = True
        else:
            rate = rate / 2.0

    # Now H is the binary check matrix and G is the encoding matrix
    # Size of G is (code_length, message_length)
    message_length = G.shape[1]
    # Convert integers in xrange(n) to binary numbers
    binary_mat = zeros([message_length, n], dtype=int)
    for i in xrange(n):
        bin_str = np.binary_repr(i, log_n)
        for j in xrange(log_n):
            binary_mat[j, i] = ord(bin_str[log_n - j - 1]) - 48

    codeword_mat = (dot(G, binary_mat)) % 2

    check_node_num = code_length * dv / dc
    L = []
    for i in range(check_node_num):
        non_zero_pos = np.where(H[i, :] != 0)
        L.append((non_zero_pos[0]).tolist())

    E = reduce(lambda x, y: x + y, map(lambda z: len(z), L))
    ldpc_code = LDPCCode(code_length, check_node_num, E, L)

    return message_length, code_length, codeword_mat, ldpc_code, G
Example #6
0
def encoding(k=8,N=16,H=None):
    if (k == 2 and N == 4):
        H = [[0, 0, 0, 1],
             [0, 1, 0, 0]]

    elif(k==8 and N == 16):

        H = [[0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0],
             [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
             [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1],
             [0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0],
             [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1],
             [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
             [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0]]

    elif(k==16 and N == 32):

        H = [[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
             [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
             [1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
             [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
             [1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
             [0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
             [0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
             [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
             [0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
             [1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
             [0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0],
             [0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1],
             [0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
             [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
             [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
             [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]]

    tG = pyldpc.CodingMatrix(H)
    #print(tG)
    return tG
Example #7
0
    v_received = pyldpc.DecodedMessage(tG,x_decoded)
    return v_received


## define LDPC parameters

n = 15  # Number of columns
d_v = 3 # Number of ones per column, must be lower than d_c (because H must have more rows than columns)
d_c = 5 # Number of ones per row, must divide n (because if H has m rows: m*d_c = n*d_v (compute number of ones in H))

max_iter = 10

num_block = 100

H = pyldpc.RegularH(n,d_v,d_c)
tG = pyldpc.CodingMatrix(H)

n,k = tG.shape

print 'LDPC has shape', n, k

snrs = [0.5*item for item in range(-3, 5)]

print snrs

train_X, train_Y = [], []
train_snr = 0.0
for _ in range(num_block):
    message  = np.random.randint(2,size=k)
    received = ldpc_regularldpc_encoder(message, tG, train_snr)
    train_Y.append(received)