Made main.py work with the new simulation functions

This commit is contained in:
Andreas Tsouchlos 2022-11-10 12:19:01 +01:00
parent f791a9a086
commit 2fae3ba3be
2 changed files with 22 additions and 45 deletions

View File

@ -12,32 +12,6 @@ from decoders import proximal, maximum_likelihood
from utility import simulations, codes from utility import simulations, codes
def test_decoders(G, decoders: typing.List) -> pd.DataFrame:
k, n = G.shape
x = np.zeros(n) # All-zeros assumption
SNRs = np.linspace(1, 7, 7)
data = pd.DataFrame({"SNR": SNRs})
start_time = default_timer()
for decoder_name in decoders:
decoder = decoders[decoder_name]
_, BERs_sd = simulations.test_decoder(x,
decoder=decoder,
SNRs=SNRs,
target_frame_errors=100,
N_max=50000)
data[f"BER_{decoder_name}"] = BERs_sd
stop_time = default_timer()
print(f"Elapsed time: {stop_time-start_time:.2f}s")
return data
# TODO: Fix spacing between axes and margins # TODO: Fix spacing between axes and margins
def plot_results(): def plot_results():
results_dir = "sim_results" results_dir = "sim_results"
@ -78,27 +52,32 @@ def plot_results():
def main(): def main():
Path("sim_results").mkdir(parents=True, exist_ok=True) Path("sim_results").mkdir(parents=True, exist_ok=True)
used_codes = [ # used_code = "Hamming_7_4"
"Hamming_7_4", # used_code = "Golay_24_12"
"Golay_24_12", used_code = "BCH_31_16"
# "BCH_31_16", # used_code = "BCH_31_21"
# "BCH_31_21", # used_code = "BCH_63_16"
# "BCH_63_16",
G = codes.Gs[used_code]
H = codes.get_systematic_H(G)
decoders = [
maximum_likelihood.MLDecoder(G, H),
proximal.ProximalDecoder(H, gamma=0.01),
proximal.ProximalDecoder(H, gamma=0.05),
proximal.ProximalDecoder(H, gamma=0.15)
] ]
for used_code in used_codes: k, n = G.shape
G = codes.Gs[used_code] SNRs, BERs = simulations.test_decoders(n, k, decoders, N_max=30000, target_frame_errors=100)
H = codes.get_systematic_H(G)
decoders = { df = pd.DataFrame({"SNR": SNRs})
"ML": maximum_likelihood.MLDecoder(G, H), df["BER_ML"] = BERs[0]
"proximal_0_01": proximal.ProximalDecoder(H, gamma=0.01), df["BER_prox_0_01"] = BERs[0]
"proximal_0_05": proximal.ProximalDecoder(H, gamma=0.05), df["BER_prox_0_05"] = BERs[1]
"proximal_0_15": proximal.ProximalDecoder(H, gamma=0.15), df["BER_prox_0_15"] = BERs[2]
}
data = test_decoders(G, decoders) df.to_csv(f"sim_results/{used_code}.csv")
data.to_csv(f"sim_results/{used_code}.csv")
plot_results() plot_results()

View File

@ -12,8 +12,6 @@ def get_noise_variance_from_SNR(SNR: float, n: int, k: int) -> float:
:param k: Length of a dataword of the used code :param k: Length of a dataword of the used code
:return: Variance of the noise :return: Variance of the noise
""" """
# noise_amp = (1 / np.sqrt(SNR_linear)) * signal_amp
SNR_linear = 10 ** (SNR / 10) SNR_linear = 10 ** (SNR / 10)
variance = 1 / (2 * (k/n) * SNR_linear) variance = 1 / (2 * (k/n) * SNR_linear)