-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
73 lines (59 loc) · 2.36 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numbers
import numpy as np
# noinspection PyProtectedMember
def check_random_state(seed):
"""Turn seed into a np.random.RandomState instance.
Parameters
----------
seed : None | int | instance of RandomState
If seed is None, return the RandomState singleton used by np.random.
If seed is an int, return a new RandomState instance seeded with seed.
If seed is already a RandomState instance, return it.
Otherwise raise ValueError.
"""
if seed is None or seed is np.random:
return np.random.mtrand._rand
if isinstance(seed, numbers.Integral):
return np.random.RandomState(seed)
if isinstance(seed, np.random.RandomState):
return seed
raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
' instance' % seed)
def parity_check_matrix(n_code, d_v, d_c, seed=None):
"""Build a binary regular Parity-Check Matrix H following
Gallager's algorithm.
Parameters
----------
n_code: int, Length of the codewords.
d_v: int, Number of parity-check equations including a certain bit.
Must be greater or equal to 2.
d_c: int, Number of bits in the same parity-check equation. d_c Must be
greater or equal to d_v and must divide n.
seed: int, seed of the random generator.
Returns
-------
H: array (m_checks, n_code). LDPC regular matrix H.
Where m_checks = d_v * n / d_c, the total number of parity-check
equations.
"""
rng = check_random_state(seed)
if d_v <= 1:
raise ValueError("""d_v must be at least 2.""")
if d_c <= d_v:
raise ValueError("""d_c must be greater than d_v.""")
if n_code % d_c:
raise ValueError("""d_c must divide n for a regular LDPC matrix H.""")
m_checks = (n_code * d_v) // d_c
block = np.zeros((m_checks // d_v, n_code), dtype=int)
H = np.empty((m_checks, n_code))
block_size = m_checks // d_v
# Filling the first block with consecutive ones in each row of the block
for i in range(block_size):
for j in range(i * d_c, (i+1) * d_c):
block[i, j] = 1
H[:block_size] = block
# Create remaining blocks by permutations of the first block's columns:
for i in range(1, d_v):
H[i * block_size: (i + 1) * block_size] = rng.permutation(block.T).T
H = H.astype(int)
return H