Reformatted all code

This commit is contained in:
2022-11-22 17:35:51 +01:00
parent 3c27a0cc18
commit 8913246600
11 changed files with 164 additions and 94 deletions

View File

@@ -1 +1,2 @@
"""This package contains a number of different decoder implementations for LDPC codes."""
"""This package contains a number of different decoder implementations for
LDPC codes."""

View File

@@ -3,9 +3,9 @@ import itertools
class MLDecoder:
"""This class naively implements a soft decision decoder. The decoder calculates
the correlation between the received signal and each codeword and then chooses the
one with the largest correlation.
"""This class naively implements a soft decision decoder. The decoder
calculates the correlation between the received signal and each codeword
and then chooses the one with the largest correlation.
"""
def __init__(self, G: np.array, H: np.array):
@@ -17,13 +17,15 @@ class MLDecoder:
self._G = G
self._H = H
self._datawords, self._codewords = self._gen_codewords()
self._codewords_bpsk = 1 - 2 * self._codewords # The codewords, but mapped to [-1, 1]^n
# The codewords, but mapped to [-1, 1]^n
self._codewords_bpsk = 1 - 2 * self._codewords
def _gen_codewords(self) -> np.array:
"""Generate a list of all possible codewords.
:return: Numpy array of the form [[codeword_1], [codeword_2], ...]
(Each generated codeword is an element of [0, 1]^n)
(Each generated codeword is an element of [0, 1]^n)
"""
k, n = self._G.shape
@@ -41,8 +43,8 @@ class MLDecoder:
This function assumes a BPSK modulated signal.
:param y: Vector of received values. (y = x + w, where 'x' is element of [-1, 1]^n
and 'w' is noise)
:param y: Vector of received values. (y = x + w, where 'x' is
element of [-1, 1]^n and 'w' is noise)
:return: Most probably sent codeword (element of [0, 1]^k)
"""
correlations = np.dot(self._codewords_bpsk, y)

View File

@@ -2,7 +2,8 @@ import numpy as np
class ProximalDecoder:
"""Class implementing the Proximal Decoding algorithm. See "Proximal Decoding for LDPC Codes"
"""Class implementing the Proximal Decoding algorithm. See "Proximal
Decoding for LDPC Codes"
by Tadashi Wadayama, and Satoshi Takabe.
"""
@@ -13,7 +14,8 @@ class ProximalDecoder:
:param H: Parity Check Matrix
:param K: Max number of iterations to perform when decoding
:param omega: Step size for the gradient descent process
:param gamma: Positive constant. Arises in the approximation of the prior PDF
:param gamma: Positive constant. Arises in the approximation of the
prior PDF
:param eta: Positive constant slightly larger than one. See 3.2, p. 3
"""
self._H = H
@@ -27,8 +29,8 @@ class ProximalDecoder:
@staticmethod
def _L_awgn(s: np.array, y: np.array) -> np.array:
"""Variation of the negative log-likelihood for the special case of AWGN noise.
See 4.1, p. 4.
"""Variation of the negative log-likelihood for the special case of
AWGN noise. See 4.1, p. 4.
"""
return s - y
@@ -41,15 +43,15 @@ class ProximalDecoder:
# Calculate gradient
sums = np.dot(A_prods**2 - A_prods, self._H)
sums = np.dot(A_prods ** 2 - A_prods, self._H)
result = 4 * (x**2 - 1) * x + (2 / x) * sums
result = 4 * (x ** 2 - 1) * x + (2 / x) * sums
return result
def _projection(self, v):
"""Project a vector onto [-eta, eta]^n in order to avoid numerical instability.
Detailed in 3.2, p. 3 (Equation (15)).
"""Project a vector onto [-eta, eta]^n in order to avoid numerical
instability. Detailed in 3.2, p. 3 (Equation (15)).
:param v: Vector to project
:return: x clipped to [-eta, eta]^n
@@ -60,7 +62,8 @@ class ProximalDecoder:
"""Perform a parity check for a given codeword.
:param x_hat: codeword to be checked (element of [0, 1]^n)
:return: True if the parity check passes, i.e. the codeword is valid. False otherwise
:return: True if the parity check passes, i.e. the codeword is
valid. False otherwise
"""
syndrome = np.dot(self._H, x_hat) % 2
return not np.any(syndrome)
@@ -70,8 +73,8 @@ class ProximalDecoder:
This function assumes a BPSK modulated signal and an AWGN channel.
:param y: Vector of received values. (y = x + w, where 'x' is element of [-1, 1]^n
and 'w' is noise)
:param y: Vector of received values. (y = x + w, where 'x' is
element of [-1, 1]^n and 'w' is noise)
:return: Most probably sent codeword (element of [0, 1]^n)
"""
s = np.zeros(self._n)
@@ -83,7 +86,9 @@ class ProximalDecoder:
s = self._projection(s) # Equation (15)
x_hat = np.sign(s)
x_hat = (x_hat == -1) * 1 # Map the codeword from [-1, 1]^n to [0, 1]^n
# Map the codeword from [ -1, 1]^n to [0, 1]^n
x_hat = (x_hat == -1) * 1
if self._check_parity(x_hat):
break