Replaced for loop with builtin numpy function

This commit is contained in:
Andreas Tsouchlos 2022-11-07 11:07:39 +01:00
parent 2f1497b1b8
commit c90cddf30b

View File

@ -4,13 +4,13 @@ import itertools
class SoftDecisionDecoder: class SoftDecisionDecoder:
"""This class naively implements a soft decision decoder. This decoder calculates """This class naively implements a soft decision decoder. The decoder calculates
the posterior probability for each codeword and then chooses the one with the largest the correlation between the received signal and each codeword and then chooses the
probability. one with the largest correlation.
""" """
def __init__(self, G: np.array, H: np.array): def __init__(self, G: np.array, H: np.array):
"""Construct a new SotDecisionDecoder object. """Construct a new SoftDecisionDecoder object.
:param G: Generator matrix :param G: Generator matrix
:param H: Parity check matrix :param H: Parity check matrix
@ -39,16 +39,12 @@ class SoftDecisionDecoder:
def decode(self, y: np.array) -> np.array: def decode(self, y: np.array) -> np.array:
"""Decode a received signal. """Decode a received signal.
This function assumes a BPSK-like modulated signal ([-1, 1]^n instead of [0, 1]^n) This function assumes a BPSK-like modulated signal ([-1, 1]^n instead of [0, 1]^n).
and an AWGN channel.
:param y: Vector of received values. (y = x + n, where 'x' is element of [-1, 1]^m :param y: Vector of received values. (y = x + w, where 'x' is element of [-1, 1]^n
and 'n' is noise) and 'w' is noise)
:return: Most probably sent symbol :return: Most probably sent dataword (element of [0, 1]^k)
""" """
# TODO: Is there a nice numpy way to implement this for loop? correlations = np.dot(self._codewords_bpsk, y)
correlations = []
for c in self._codewords_bpsk:
correlations.append(np.dot(y, c))
return self._datawords[numpy.argmax(correlations)] return self._datawords[numpy.argmax(correlations)]