ba-thesis/sw/utility/encoders.py
2022-11-08 19:13:51 +01:00

27 lines
856 B
Python

import numpy as np
# TODO: Unify the interface regarding [0, 1]^n and [-1, 1]^n
# TODO: Should the encoder be responsible for mapping the message from [0, 1]^n to [-1, 1]^n?
# (ie. should the encoder perform modulation?)
class Encoder:
"""Class implementing an encoder for block codes.
"""
def __init__(self, G: np.array):
"""Construct a new Encoder object.
:param G: Generator matrix
"""
self._G = G
def encode(self, d: np.array) -> np.array:
"""Map a given dataword onto the corresponding codeword.
The returned codeword is mapped from [0, 1]^n onto [-1, 1]^n.
:param d: Dataword (element of [0, 1]^n)
:return: Codeword (already element of [-1, 1]^n)
"""
# TODO: (0 -> 1), (1 -> -1); Not (0 -> -1), (-1 -> 1)
return np.dot(d, self._G) * 2 - 1