Skip to content

Commit 07923d8

Browse files
committed
Function to build LDPC parity check matrix.
1 parent 70a9caf commit 07923d8

File tree

1 file changed

+58
-21
lines changed

1 file changed

+58
-21
lines changed

commpy/channelcoding/ldpc.py

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,43 @@
33

44
import numpy as np
55

6-
__all__ = ['get_ldpc_code_params', 'ldpc_bp_decode']
6+
__all__ = ['build_matrix', 'get_ldpc_code_params', 'ldpc_bp_decode']
77

88

9-
def get_ldpc_code_params(ldpc_design_filename):
9+
def build_matrix(ldpc_code_params):
10+
"""
11+
Build the parity check matrix from parameters dictionary and add the result in this dictionary.
12+
13+
Parameters
14+
----------
15+
ldpc_code_params: dictionary
16+
Parameters of the LDPC code:
17+
n_vnodes (int) - number of variable nodes.
18+
n_cnodes (int) - number of check nodes.
19+
max_vnode_deg (int) - maximal degree of a variable node.
20+
max_cnode_deg (int) - maximal degree of a check node.
21+
vnode_adj_list (1D-ndarray of ints) - flatten array so that
22+
vnode_adj_list.reshape((n_vnodes, max_vnode_deg)) gives for each variable node the adjacent check nodes.
23+
cnode_adj_list (1D-ndarray of ints) - flatten array so that
24+
cnode_adj_list.reshape((n_cnodes, max_cnode_deg)) gives for each check node the adjacent variable nodes.
25+
vnode_cnode_map (1D-ndarray of ints) - flatten array providing the mapping between vnode and cnode indexes.
26+
cnode_vnode_map (1D-ndarray of ints) - flatten array providing the mapping between vnode and cnode indexes.
27+
vnode_deg_list (1D-ndarray of ints) - degree of each variable node.
28+
cnode_deg_list (1D-ndarray of ints) - degree of each check node.
29+
parity_check_matrix (2D-ndarray of bool or None) - parity check matrix computed only if `compute_matrix`.
30+
"""
31+
n_cnodes = ldpc_code_params['n_cnodes']
32+
cnode_deg_list = ldpc_code_params['cnode_deg_list']
33+
cnode_adj_list = ldpc_code_params['cnode_adj_list'].reshape((n_cnodes, ldpc_code_params['max_cnode_deg']))
34+
35+
parity_check_matrix = np.zeros((n_cnodes, ldpc_code_params['n_vnodes']), bool)
36+
for cnode_idx in range(n_cnodes):
37+
parity_check_matrix[cnode_idx, cnode_adj_list[cnode_idx, :cnode_deg_list[cnode_idx]]] = True
38+
39+
ldpc_code_params['parity_check_matrix'] = parity_check_matrix
40+
41+
42+
def get_ldpc_code_params(ldpc_design_filename, compute_matrix=False):
1043
"""
1144
Extract parameters from LDPC code design file.
1245
@@ -23,6 +56,10 @@ def get_ldpc_code_params(ldpc_design_filename):
2356
ldpc_design_filename : string
2457
Filename of the LDPC code design file.
2558
59+
compute_matrix : boolean
60+
Specify if the parity check matrix must be computed.
61+
*Default* is False.
62+
2663
Returns
2764
-------
2865
ldpc_code_params : dictionary
@@ -39,27 +76,26 @@ def get_ldpc_code_params(ldpc_design_filename):
3976
cnode_vnode_map (1D-ndarray of ints) - flatten array providing the mapping between vnode and cnode indexes.
4077
vnode_deg_list (1D-ndarray of ints) - degree of each variable node.
4178
cnode_deg_list (1D-ndarray of ints) - degree of each check node.
79+
parity_check_matrix (2D-ndarray of bool or None) - parity check matrix computed only if `compute_matrix`.
4280
"""
4381

44-
ldpc_design_file = open(ldpc_design_filename)
82+
with open(ldpc_design_filename) as ldpc_design_file:
4583

46-
ldpc_code_params = {}
84+
[n_vnodes, n_cnodes] = [int(x) for x in ldpc_design_file.readline().split(' ')]
85+
[max_vnode_deg, max_cnode_deg] = [int(x) for x in ldpc_design_file.readline().split(' ')]
86+
vnode_deg_list = np.array([int(x) for x in ldpc_design_file.readline().split(' ')[:-1]], np.int32)
87+
cnode_deg_list = np.array([int(x) for x in ldpc_design_file.readline().split(' ')[:-1]], np.int32)
4788

48-
[n_vnodes, n_cnodes] = [int(x) for x in ldpc_design_file.readline().split(' ')]
49-
[max_vnode_deg, max_cnode_deg] = [int(x) for x in ldpc_design_file.readline().split(' ')]
50-
vnode_deg_list = np.array([int(x) for x in ldpc_design_file.readline().split(' ')[:-1]], np.int32)
51-
cnode_deg_list = np.array([int(x) for x in ldpc_design_file.readline().split(' ')[:-1]], np.int32)
89+
cnode_adj_list = -np.ones([n_cnodes, max_cnode_deg], int)
90+
vnode_adj_list = -np.ones([n_vnodes, max_vnode_deg], int)
5291

53-
cnode_adj_list = -np.ones([n_cnodes, max_cnode_deg], int)
54-
vnode_adj_list = -np.ones([n_vnodes, max_vnode_deg], int)
55-
56-
for vnode_idx in range(n_vnodes):
57-
vnode_adj_list[vnode_idx, 0:vnode_deg_list[vnode_idx]] = \
58-
np.array([int(x)-1 for x in ldpc_design_file.readline().split('\t')])
92+
for vnode_idx in range(n_vnodes):
93+
vnode_adj_list[vnode_idx, 0:vnode_deg_list[vnode_idx]] = \
94+
np.array([int(x)-1 for x in ldpc_design_file.readline().split('\t')])
5995

60-
for cnode_idx in range(n_cnodes):
61-
cnode_adj_list[cnode_idx, 0:cnode_deg_list[cnode_idx]] = \
62-
np.array([int(x)-1 for x in ldpc_design_file.readline().split('\t')])
96+
for cnode_idx in range(n_cnodes):
97+
cnode_adj_list[cnode_idx, 0:cnode_deg_list[cnode_idx]] = \
98+
np.array([int(x)-1 for x in ldpc_design_file.readline().split('\t')])
6399

64100
cnode_vnode_map = -np.ones([n_cnodes, max_cnode_deg], int)
65101
vnode_cnode_map = -np.ones([n_vnodes, max_vnode_deg], int)
@@ -77,9 +113,7 @@ def get_ldpc_code_params(ldpc_design_filename):
77113
cnode_vnode_map_1d = cnode_vnode_map.flatten().astype(np.int32)
78114
vnode_cnode_map_1d = vnode_cnode_map.flatten().astype(np.int32)
79115

80-
pmat = np.zeros([n_cnodes, n_vnodes], int)
81-
for cnode_idx in range(n_cnodes):
82-
pmat[cnode_idx, cnode_adj_list[cnode_idx, :]] = 1
116+
ldpc_code_params = {}
83117

84118
ldpc_code_params['n_vnodes'] = n_vnodes
85119
ldpc_code_params['n_cnodes'] = n_cnodes
@@ -92,7 +126,10 @@ def get_ldpc_code_params(ldpc_design_filename):
92126
ldpc_code_params['cnode_deg_list'] = cnode_deg_list
93127
ldpc_code_params['vnode_deg_list'] = vnode_deg_list
94128

95-
ldpc_design_file.close()
129+
if compute_matrix:
130+
build_matrix(ldpc_code_params)
131+
else:
132+
ldpc_code_params['parity_check_matrix'] = None
96133

97134
return ldpc_code_params
98135

0 commit comments

Comments
 (0)