-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannelcoding_ldpc.py
More file actions
67 lines (53 loc) · 2.41 KB
/
channelcoding_ldpc.py
File metadata and controls
67 lines (53 loc) · 2.41 KB
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
# Authors: Veeresh Taranalli <veeresht@gmail.com>
# License: BSD 3-Clause
# source: https://pydoc.net/scikit-commpy/0.3.0/commpy.channelcoding.ldpc/
""" LDPC Codes """
from numpy import zeros, shape, tanh, arctanh, array, delete, prod, dot
def ldpc_decode(pcheck_matrix, rx_codeword, n_iters):
"""
LDPC Decoder using belief propagation for an AWGN channel.
Parameters
----------
pcheck_matrix : 2D array of 0's and 1's
Parity Check Matrix of the linear (LDPC) code.
rx_codeword : 1D array of float
Received codeword from an AWGN channel.
n_iter : int
Max. number of iterations of decoding to be done.
Returns
-------
decoded_bits : 1D array of 0's and 1's
The codeword after decoding.
"""
[n_c_nodes, n_v_nodes] = shape(pcheck_matrix)
comp_mat = zeros([n_c_nodes, n_v_nodes])
llr_vals = rx_codeword
for i in range(n_iters):
# Check Node Update
for v_node in range(n_v_nodes):
for c_node in range(n_c_nodes):
if pcheck_matrix[c_node, v_node] == 1:
prev_llrs = delete(llr_vals, v_node)
prev_llrs = prev_llrs[delete(pcheck_matrix[c_node, :], v_node) == 1]
prev_compvals = delete(comp_mat[c_node, :], v_node)
prev_compvals = prev_compvals[delete(pcheck_matrix[c_node, :], v_node) == 1]
llr_prod = prod(tanh(-(prev_llrs - prev_compvals) / 2))
comp_mat[c_node, v_node] = -2 * arctanh(llr_prod)
# Variable Node Update
for v_node in range(n_v_nodes):
llr_vals[v_node] = llr_vals[v_node] + sum(comp_mat[:, v_node][pcheck_matrix[:, v_node] == 1])
decoded_bits = array(llr_vals > 0, dtype=int)
if not (dot(pcheck_matrix, decoded_bits) % 2).any():
#print(f"Perfect Decoding, # Iterations: {i + 1}")
break
return decoded_bits
if __name__ == '__main__':
pcheck_matrix = array([[1, 1, 1, 0, 0, 1, 1, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 1, 0, 1, 1],
[0, 1, 0, 1, 1, 1, 0, 1, 0, 1],
[1, 1, 0, 1, 0, 0, 1, 1, 1, 0]])
rx_codeword = array([-1.3, -1.7, -1.5, -0.08, 0.2, 1.9, -1.5, 1.3, -1.1, 1.2])
n_iters = 10
decoded_bits = ldpc_decode(pcheck_matrix, rx_codeword, n_iters)
print(decoded_bits)