Moved python files from sw to sw/python; Moved scritps into sw/python/scripts
This commit is contained in:
154
sw/python/scripts/proximal/simulate_2d_BER.py
Normal file
154
sw/python/scripts/proximal/simulate_2d_BER.py
Normal file
@@ -0,0 +1,154 @@
|
||||
import sys, os
|
||||
import sys, os
|
||||
sys.path.append(os.path.abspath('../..'))
|
||||
print(sys.path)
|
||||
|
||||
|
||||
import numpy as np
|
||||
import seaborn as sns
|
||||
import matplotlib.pyplot as plt
|
||||
import signal
|
||||
from timeit import default_timer
|
||||
from functools import partial
|
||||
|
||||
from utility import codes, noise, misc
|
||||
from utility.simulation.simulators import GenericMultithreadedSimulator
|
||||
from utility.simulation import SimulationManager
|
||||
|
||||
from cpp_modules.cpp_decoders import ProximalDecoder_204_102 as ProximalDecoder
|
||||
|
||||
|
||||
def task_func(params):
|
||||
"""Function called by the GenericMultithreadedSimulator instance.
|
||||
|
||||
Calculate the BER, FER, and DFR for a given SNR and gamma.
|
||||
"""
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
|
||||
decoder = params["decoder"]
|
||||
max_iterations = params["max_iterations"]
|
||||
SNR = params["SNR"]
|
||||
n = params["n"]
|
||||
k = params["k"]
|
||||
|
||||
c = np.zeros(n)
|
||||
x_bpsk = c + 1
|
||||
|
||||
total_bit_errors = 0
|
||||
total_frame_errors = 0
|
||||
dec_fails = 0
|
||||
|
||||
num_iterations = 0
|
||||
|
||||
for i in range(max_iterations):
|
||||
x = noise.add_awgn(x_bpsk, SNR, n, k)
|
||||
x_hat, k_max = decoder.decode(x)
|
||||
|
||||
bit_errors = misc.count_bit_errors(x_hat, c)
|
||||
if bit_errors > 0:
|
||||
total_bit_errors += bit_errors
|
||||
total_frame_errors += 1
|
||||
|
||||
num_iterations += 1
|
||||
|
||||
if k_max == -1:
|
||||
dec_fails += 1
|
||||
|
||||
if total_frame_errors > 100:
|
||||
break
|
||||
|
||||
BER = total_bit_errors / (num_iterations * n)
|
||||
FER = total_frame_errors / num_iterations
|
||||
DFR = dec_fails / (num_iterations + dec_fails)
|
||||
|
||||
return {"BER": BER, "FER": FER, "DFR": DFR,
|
||||
"num_iterations": num_iterations}
|
||||
|
||||
|
||||
def get_params(code_name: str):
|
||||
"""In this function all parameters for the simulation are defined."""
|
||||
# Define global simulation parameters
|
||||
|
||||
H_file = f"../../res/{code_name}.alist"
|
||||
|
||||
H = codes.read_alist_file(H_file)
|
||||
n_min_k, n = H.shape
|
||||
k = n - n_min_k
|
||||
|
||||
omega = 0.05
|
||||
K = 100
|
||||
gammas = np.arange(0.0, 0.17, 0.01)
|
||||
|
||||
SNRs = np.arange(1, 6, 0.5)
|
||||
max_iterations = 20000
|
||||
|
||||
# Define parameters different for each task
|
||||
|
||||
task_params = []
|
||||
for i, SNR in enumerate(SNRs):
|
||||
for j, gamma in enumerate(gammas):
|
||||
decoder = ProximalDecoder(H=H.astype('int32'), K=K, omega=omega,
|
||||
gamma=gamma)
|
||||
|
||||
task_params.append(
|
||||
{"decoder": decoder, "max_iterations": max_iterations,
|
||||
"SNR": SNR, "gamma": gamma, "n": n, "k": k})
|
||||
|
||||
return omega, K, task_params
|
||||
|
||||
|
||||
def configure_new_simulation(sim_mgr: SimulationManager, code_name: str,
|
||||
sim_name: str) -> None:
|
||||
sim = GenericMultithreadedSimulator()
|
||||
|
||||
omega, K, task_params = get_params(code_name)
|
||||
|
||||
sim.task_params = task_params
|
||||
sim.task_func = task_func
|
||||
sim.format_func = partial(misc.pgf_reformat_data_3d, x_param_name="SNR",
|
||||
y_param_name="gamma",
|
||||
z_param_names=["BER", "FER", "DFR",
|
||||
"num_iterations"])
|
||||
|
||||
sim_mgr.configure_simulation(simulator=sim, name=sim_name,
|
||||
additional_metadata={"omega": omega, "K": K})
|
||||
|
||||
|
||||
def main():
|
||||
# code_name = "BCH_7_4"
|
||||
# code_name = "BCH_31_11"
|
||||
# code_name = "BCH_31_26"
|
||||
# code_name = "96.3.965"
|
||||
# code_name = "204.33.486"
|
||||
code_name = "204.33.484"
|
||||
# code_name = "204.55.187"
|
||||
# code_name = "408.33.844"
|
||||
|
||||
sim_name = f"2d_BER_FER_DFR_{misc.slugify(code_name)}"
|
||||
|
||||
# Run simulation
|
||||
|
||||
sim_mgr = SimulationManager(saves_dir="sim_saves",
|
||||
results_dir="sim_results")
|
||||
|
||||
unfinished_sims = sim_mgr.get_unfinished()
|
||||
if len(unfinished_sims) > 0:
|
||||
sim_mgr.load_unfinished(unfinished_sims[0])
|
||||
else:
|
||||
configure_new_simulation(sim_mgr=sim_mgr, code_name=code_name,
|
||||
sim_name=sim_name)
|
||||
|
||||
sim_mgr.simulate()
|
||||
|
||||
# Plot results
|
||||
|
||||
sns.set_theme()
|
||||
ax = sns.lineplot(data=sim_mgr.get_current_results(), x="SNR", y="BER",
|
||||
hue="gamma")
|
||||
ax.set_yscale('log')
|
||||
ax.set_ylim((5e-5, 2e-0))
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
147
sw/python/scripts/proximal/simulate_2d_avg_error.py
Normal file
147
sw/python/scripts/proximal/simulate_2d_avg_error.py
Normal file
@@ -0,0 +1,147 @@
|
||||
import sys, os
|
||||
sys.path.append(os.path.abspath('../..'))
|
||||
|
||||
import numpy as np
|
||||
import seaborn as sns
|
||||
import matplotlib.pyplot as plt
|
||||
import signal
|
||||
from timeit import default_timer
|
||||
from functools import partial
|
||||
import pandas as pd
|
||||
|
||||
from utility import codes, noise, misc
|
||||
from utility.simulation.simulators import GenericMultithreadedSimulator
|
||||
from utility.simulation import SimulationManager
|
||||
|
||||
from cpp_modules.cpp_decoders import ProximalDecoder_204_102 as ProximalDecoder
|
||||
|
||||
|
||||
def task_func(params):
|
||||
"""Function called by the GenericMultithreadedSimulator instance.
|
||||
|
||||
Calculate the average error over a number of iterations.
|
||||
"""
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
|
||||
decoder = params["decoder"]
|
||||
num_iterations = params["num_iterations"]
|
||||
x_bpsk = params["x_bpsk"]
|
||||
SNR = params["SNR"]
|
||||
n = params["n"]
|
||||
k = params["k"]
|
||||
K = params["K"]
|
||||
|
||||
avg_error_values = np.zeros(K)
|
||||
|
||||
for i in range(num_iterations):
|
||||
x = noise.add_awgn(x_bpsk, SNR, n, k)
|
||||
|
||||
error_values = decoder.get_error_values(x_bpsk.astype('int32'), x)
|
||||
|
||||
for j, val in enumerate(error_values):
|
||||
avg_error_values[j] += val
|
||||
|
||||
avg_error_values = avg_error_values / num_iterations
|
||||
|
||||
return {"err": avg_error_values}
|
||||
|
||||
|
||||
def get_params(code_name: str):
|
||||
"""In this function all parameters for the simulation are defined."""
|
||||
# Define global simulation parameters
|
||||
|
||||
H_file = f"../../res/{code_name}.alist"
|
||||
H = codes.read_alist_file(H_file)
|
||||
n_min_k, n = H.shape
|
||||
k = n - n_min_k
|
||||
|
||||
SNR = 8
|
||||
omegas = np.logspace(-0, -10, 40)
|
||||
K = 200
|
||||
|
||||
num_iterations = 1000
|
||||
x_bpsk = np.zeros(n) + 1
|
||||
|
||||
# Define parameters different for each task
|
||||
|
||||
task_params = []
|
||||
for i, omega in enumerate(omegas):
|
||||
decoder = ProximalDecoder(H=H.astype('int32'), K=K,
|
||||
omega=omega)
|
||||
task_params.append(
|
||||
{"decoder": decoder, "num_iterations": num_iterations,
|
||||
"x_bpsk": x_bpsk, "SNR": SNR, "n": n, "k": k, "K": K,
|
||||
"omega": omega})
|
||||
|
||||
return SNR, K, task_params
|
||||
|
||||
|
||||
def reformat_data(results):
|
||||
"""Reformat the data obtained from the GenericMultithreadedSimulator to
|
||||
be usable by pgfplots.
|
||||
"""
|
||||
K = 200
|
||||
num_points = len(results) * K
|
||||
|
||||
x = np.zeros(num_points)
|
||||
y = np.zeros(num_points)
|
||||
z = np.zeros(num_points)
|
||||
|
||||
for i, (params, result) in enumerate(results.items()):
|
||||
np.put(x, np.arange(i * K, (i + 1) * K), np.arange(1, K+1))
|
||||
np.put(y, np.arange(i * K, (i + 1) * K), params["omega"])
|
||||
np.put(z, np.arange(i * K, (i + 1) * K), result["err"])
|
||||
|
||||
x = x[::4]
|
||||
y = y[::4]
|
||||
z = z[::4]
|
||||
|
||||
df = pd.DataFrame({"k": x, "omega": y, "err": z}).sort_values(
|
||||
by=['k', 'omega'], ascending=[True, False])
|
||||
|
||||
return df
|
||||
|
||||
|
||||
def configure_new_simulation(sim_mgr: SimulationManager, code_name: str,
|
||||
sim_name: str) -> None:
|
||||
sim = GenericMultithreadedSimulator()
|
||||
|
||||
SNR, K, task_params = get_params(code_name)
|
||||
|
||||
sim.task_params = task_params
|
||||
sim.task_func = task_func
|
||||
sim.format_func = reformat_data
|
||||
|
||||
sim_mgr.configure_simulation(simulator=sim, name=sim_name,
|
||||
additional_metadata={"SNR": SNR, "K": K})
|
||||
|
||||
|
||||
def main():
|
||||
# code_name = "BCH_7_4"
|
||||
# code_name = "BCH_31_11"
|
||||
# code_name = "BCH_31_26"
|
||||
# code_name = "96.3.965"
|
||||
# code_name = "204.33.486"
|
||||
code_name = "204.33.484"
|
||||
# code_name = "204.55.187"
|
||||
# code_name = "408.33.844"
|
||||
|
||||
sim_name = f"2d_avg_error_{misc.slugify(code_name)}"
|
||||
|
||||
# Run simulation
|
||||
|
||||
sim_mgr = SimulationManager(saves_dir="sim_saves",
|
||||
results_dir="sim_results")
|
||||
|
||||
unfinished_sims = sim_mgr.get_unfinished()
|
||||
if len(unfinished_sims) > 0:
|
||||
sim_mgr.load_unfinished(unfinished_sims[0])
|
||||
else:
|
||||
configure_new_simulation(sim_mgr=sim_mgr, code_name=code_name,
|
||||
sim_name=sim_name)
|
||||
|
||||
sim_mgr.simulate()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
88
sw/python/scripts/proximal/simulate_gradient.py
Normal file
88
sw/python/scripts/proximal/simulate_gradient.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import sys, os
|
||||
sys.path.append(os.path.abspath('../..'))
|
||||
|
||||
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 tqdm import tqdm
|
||||
|
||||
from utility import codes, noise, misc
|
||||
from utility.simulation.simulators import GenericMultithreadedSimulator
|
||||
|
||||
# from cpp_modules.cpp_decoders import ProximalDecoder
|
||||
from cpp_modules.cpp_decoders import ProximalDecoder_204_102 as ProximalDecoder
|
||||
|
||||
|
||||
def simulate(H_file, SNR, omega, K, gamma):
|
||||
H = codes.read_alist_file(f"../../res/{H_file}")
|
||||
n_min_k, n = H.shape
|
||||
k = n - n_min_k
|
||||
|
||||
decoder = ProximalDecoder(H.astype('int32'), K=K, omega=omega, gamma=gamma)
|
||||
|
||||
c = np.zeros(n)
|
||||
x_bpsk = (c + 1)
|
||||
|
||||
avg_grad_values = np.zeros(shape=(K, 2))
|
||||
|
||||
for i in range(1000):
|
||||
x = noise.add_awgn(x_bpsk, SNR, n, k)
|
||||
grad_values = decoder.get_gradient_values(x)
|
||||
|
||||
for j, (val_h, val_l) in enumerate(grad_values):
|
||||
avg_grad_values[j, 0] += val_h
|
||||
avg_grad_values[j, 1] += val_l
|
||||
|
||||
avg_grad_values = avg_grad_values / 1000
|
||||
|
||||
return avg_grad_values
|
||||
|
||||
|
||||
def reformat_data(results):
|
||||
return pd.DataFrame({"k": np.arange(0, results.size // 2, 1), "grad_h": results[:, 0], "grad_l": results[:, 1]})
|
||||
|
||||
|
||||
def main():
|
||||
# Set up simulation params
|
||||
|
||||
sim_name = "avg_grad_1dB"
|
||||
|
||||
# H_file = "96.3.965.alist"
|
||||
H_file = "204.33.486.alist"
|
||||
# H_file = "204.33.484.alist"
|
||||
# H_file = "204.55.187.alist"
|
||||
# H_file = "408.33.844.alist"
|
||||
# H_file = "BCH_7_4.alist"
|
||||
# H_file = "BCH_31_11.alist"
|
||||
# H_file = "BCH_31_26.alist"
|
||||
|
||||
SNR = 1
|
||||
omega = 0.05
|
||||
K = 100
|
||||
gamma = 0.05
|
||||
|
||||
# Run simulation
|
||||
|
||||
start_time = default_timer()
|
||||
results = simulate(H_file, SNR, omega, K, gamma)
|
||||
end_time = default_timer()
|
||||
|
||||
print(f"duration: {end_time - start_time}")
|
||||
|
||||
df = reformat_data(results)
|
||||
|
||||
df.to_csv(
|
||||
f"sim_results/{sim_name}_{misc.slugify(H_file)}.csv", index=False)
|
||||
|
||||
sns.set_theme()
|
||||
sns.lineplot(data=df, x="k", y="grad_h")
|
||||
sns.lineplot(data=df, x="k", y="grad_l")
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user