Added Encoder class and modified interface of utility.test_decoder()

This commit is contained in:
Andreas Tsouchlos 2022-11-07 11:54:28 +01:00
parent 26fa791872
commit 2c620a77df
3 changed files with 50 additions and 15 deletions

24
sw/decoders/channel.py Normal file
View File

@ -0,0 +1,24 @@
import numpy as np
# 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)
"""
return np.dot(d, self._G) * 2 - 1

View File

@ -43,26 +43,28 @@ def count_bit_errors(d: np.array, d_hat: np.array) -> int:
return np.sum(d != d_hat)
def test_decoder(decoder: typing.Any,
def test_decoder(encoder: typing.Any,
decoder: typing.Any,
d: np.array,
c: np.array,
SNRs: typing.Sequence[float] = np.linspace(1, 4, 7),
target_bit_errors=100,
N_max=10000) \
target_bit_errors: int = 100,
N_max: int = 10000) \
-> typing.Tuple[np.array, np.array]:
"""Calculate the Bit Error Rate (BER) for a given decoder for a number of SNRs.
This function prints its progress to stdout.
:param encoder: Instance of the encoder used to generate the codeword to transmit
:param decoder: Instance of the decoder to be tested
:param d: Dataword (element of [0, 1]^n)
:param c: Codeword whose transmission is to be simulated (element of [0, 1]^n)
:param SNRs: List of SNRs for which the BER should be calculated
:param target_bit_errors: Number of bit errors after which to stop the simulation
:param N_max: Maximum number of iterations to perform for each SNR
:return: Tuple of numpy arrays of the form (SNRs, BERs)
"""
x = c * 2 - 1 # Map the codeword from [0, 1]^n to [-1, 1]^n
x = encoder.encode(d)
BERs = []
for SNR in tqdm(SNRs, desc="Calculating Bit-Error-Rates",
position=0,
@ -79,10 +81,11 @@ def test_decoder(decoder: typing.Any,
# TODO: Is this a valid simulation? Can we just add AWGN to the codeword, ignoring and modulation and (
# e.g. matched) filtering?
y = add_awgn(x, SNR, signal_amp=np.sqrt(2))
y_hat = decoder.decode(y)
total_bit_errors += count_bit_errors(d, y_hat)
total_bits += c.size
total_bits += x.size
if total_bit_errors >= target_bit_errors:
break

View File

@ -5,6 +5,7 @@ import pandas as pd
from decoders import proximal
from decoders import naive_soft_decision
from decoders import channel
from decoders import utility
@ -20,20 +21,27 @@ def main():
[0, 1, 1, 0, 0, 1, 1],
[0, 0, 0, 1, 1, 1, 1]])
encoder = channel.Encoder(G)
proximal_decoder = proximal.ProximalDecoder(H, K=100, gamma=0.01)
soft_decision_decoder = naive_soft_decision.SoftDecisionDecoder(G, H)
# Test decoder
d = np.array([0, 0, 0, 0])
c = np.dot(G.transpose(), d) % 2
k, n = G.shape
d = np.zeros(k) # All-zeros assumption
print(f"Simulating with c = {c}")
SNRs_sd, BERs_sd = utility.test_decoder(encoder=encoder,
decoder=soft_decision_decoder,
d=d,
SNRs=np.linspace(1, 7, 9),
target_bit_errors=500)
# decoder = proximal.ProximalDecoder(H, K=100, gamma=0.01)
decoder = naive_soft_decision.SoftDecisionDecoder(G, H)
SNRs, BERs = utility.test_decoder(decoder, d, c, SNRs=np.linspace(1, 7, 9), target_bit_errors=500, N_max=10000)
data = pd.DataFrame({"SNR": SNRs_sd, "BER_sd": BERs_sd})
data = pd.DataFrame({"SNR": SNRs, "BER": BERs})
# Plot results
ax = sns.lineplot(data=data, x="SNR", y="BER")
ax = sns.lineplot(data=data, x="SNR", y="BER_sd")
ax.set(yscale="log")
ax.set_yticks([10e-5, 10e-4, 10e-3, 10e-2, 10e-1, 10e0])
# ax.set_ylim([10e-6, 10e0])