From 27c36855298273290a3e7c3658a249442055cb0c Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Mon, 28 Nov 2022 16:01:11 +0100 Subject: [PATCH] Added simulate_2d_dec_fails.py --- sw/simulate_2d_dec_fails.py | 101 ++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 sw/simulate_2d_dec_fails.py diff --git a/sw/simulate_2d_dec_fails.py b/sw/simulate_2d_dec_fails.py new file mode 100644 index 0000000..e237a8a --- /dev/null +++ b/sw/simulate_2d_dec_fails.py @@ -0,0 +1,101 @@ +import numpy as np +import pandas as pd +import seaborn as sns +import matplotlib.pyplot as plt +import signal +from timeit import default_timer + +from utility import codes, noise, misc +from utility.simulation.simulators import GenericMultithreadedSimulator + +from cpp_modules.cpp_decoders import ProximalDecoder + + +def task_func(params): + signal.signal(signal.SIGINT, signal.SIG_IGN) + + decoder, num_iterations, x_bpsk, SNR, n, k = params + + dec_fails = 0 + for i in range(num_iterations): + x = noise.add_awgn(x_bpsk, SNR, n, k) + x_hat, num_iter = decoder.decode(x) + + if x_hat is None: + dec_fails += 1 + + return dec_fails / num_iterations + + +def simulate(H_file, SNR, num_iterations, omegas, Ks): + sim = GenericMultithreadedSimulator() + + # Define fixed simulation params + + H = codes.read_alist_file(f"res/{H_file}") + n_min_k, n = H.shape + k = n - n_min_k + + x_bpsk = np.zeros(n) + 1 + + # Define params different for each task + + params = {} + for i, omega in enumerate(omegas): + for j, K in enumerate(Ks): + decoder = ProximalDecoder(H=H.astype('int32'), K=K.astype('int32'), + omega=omega) + params[f"{i}_{j}"] = (decoder, num_iterations, x_bpsk, SNR, n, k) + + # Set up simulation + + sim.task_params = params + sim.task_func = task_func + + sim.start_or_continue() + + return sim.get_current_results() + + +def reformat_data(results, omegas, Ks): + data = np.zeros(1600).reshape(40, 40) + + for key, value in results.items(): + i_w, i_k = key.split('_') + data[int(i_w), int(i_k)] = value + + return pd.DataFrame(data, columns=Ks, index=omegas) + + +def main(): + # Set up simulation params + + sim_name = "w_log_k_lin_zoomed_in" + + H_file = "96.3.965.alist" + SNR = 3 + + num_iterations = 1000 + omegas = np.logspace(-0.3, -2.82, 40) + Ks = np.ceil(np.linspace(10 ** 1.3, 10 ** 2.3, 40)).astype('int32') + + # Run simulation + + start_time = default_timer() + results = simulate(H_file, SNR, num_iterations, omegas, Ks) + end_time = default_timer() + + print(f"duration: {end_time - start_time}") + + df = reformat_data(results, omegas, Ks) + + df.to_csv( + f"sim_results/2d_dec_fails_{sim_name}_{misc.slugify(H_file)}.csv") + + sns.set_theme() + sns.heatmap(df) + plt.show() + + +if __name__ == "__main__": + main()