start signal gen
This commit is contained in:
216
src/single-core-regen/generate_signal.py
Normal file
216
src/single-core-regen/generate_signal.py
Normal file
@@ -0,0 +1,216 @@
|
||||
import configparser
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
from matplotlib import pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
from rich import inspect
|
||||
|
||||
import _path_fix # noqa: F401
|
||||
import pypho
|
||||
import io
|
||||
# import inspect
|
||||
|
||||
default_config = f"""
|
||||
[glova]
|
||||
sps = 256
|
||||
nos = 256
|
||||
f0 = 193414489032258.06
|
||||
symbolrate = 10e9
|
||||
wisdom_dir = "{str((Path.home()/".pypho"))}"
|
||||
flags = "FFTW_PATIENT"
|
||||
nthreads = 32
|
||||
|
||||
[fiber]
|
||||
length = 80000
|
||||
gamma = 1.14
|
||||
alpha = 0.2
|
||||
D = 17
|
||||
S = 0
|
||||
birefsteps = 1
|
||||
birefseed = 0xC0FFEE
|
||||
|
||||
[signal]
|
||||
modulation = "pam"
|
||||
mod_order = 4
|
||||
seed = 0xC0FFEE
|
||||
pulse_shape = "gauss_rz"
|
||||
fwhm = 0.33
|
||||
|
||||
[script]
|
||||
data_dir = "{str((Path.home()/".pypho"/"data"))}"
|
||||
"""
|
||||
|
||||
|
||||
|
||||
def get_config(config_file=None):
|
||||
"""
|
||||
DANGER! The function uses eval() to parse the config file. Do not use this function with untrusted input.
|
||||
"""
|
||||
if config_file is None:
|
||||
config_file = Path(__file__).parent / "signal_generation.ini"
|
||||
if not config_file.exists():
|
||||
with open(config_file, "w") as f:
|
||||
f.write(default_config)
|
||||
config = configparser.ConfigParser()
|
||||
config.read(config_file)
|
||||
|
||||
conf = {}
|
||||
for section in config.sections():
|
||||
# print(f"[{section}]")
|
||||
conf[section] = {}
|
||||
for key in config[section]:
|
||||
# print(f"{key} = {config[section][key]}")
|
||||
conf[section][key] = eval(config[section][key])
|
||||
# if isinstance(conf[section][key], str):
|
||||
# conf[section][key] = config[section][key].strip('"')
|
||||
return conf
|
||||
|
||||
def initialize_fiber_and_data(config, input_data_override=None):
|
||||
py_glova = pypho.setup(
|
||||
nos = config['glova']['nos'],
|
||||
sps = config['glova']['sps'],
|
||||
f0 = config['glova']['f0'],
|
||||
symbolrate=config['glova']['symbolrate'],
|
||||
wisdom_dir = config['glova']['wisdom_dir'],
|
||||
flags = config['glova']['flags'],
|
||||
nthreads = config['glova']['nthreads'],
|
||||
)
|
||||
|
||||
c_glova = pypho.cfiber.GlovaWrapper.from_setup(py_glova)
|
||||
c_data = pypho.cfiber.DataWrapper(py_glova.sps * py_glova.nos)
|
||||
|
||||
if input_data_override is not None:
|
||||
c_data.E_in = input_data_override
|
||||
else:
|
||||
symbolsrc = pypho.symbols(py_glova, py_glova.nos, pattern='ones', seed=config['signal']['seed'])
|
||||
esigsrc = pypho.signalsrc(py_glova, pulseshape=config['signal']['pulse_shape'], fwhm=config['signal']['fwhm'])
|
||||
sig = pypho.lasmod(py_glova, power=0, Df=0, theta=np.pi/4)
|
||||
modulator = pypho.arbmod(py_glova)
|
||||
|
||||
symbols_x = symbolsrc(pattern='random', p1=config['signal']['mod_order'])
|
||||
symbols_y = symbolsrc(pattern='random', p1=config['signal']['mod_order'])
|
||||
|
||||
ones = symbolsrc(pattern='ones')
|
||||
esig = esigsrc(bitsequence=ones)*np.sqrt(2)
|
||||
|
||||
source_signal = sig(esig)
|
||||
|
||||
constpts_x = [np.linspace(1/config['signal']['mod_order'], 1, config['signal']['mod_order'], endpoint=True, dtype=np.complex128)]
|
||||
constpts_y = [np.linspace(1/config['signal']['mod_order'], 1, config['signal']['mod_order'], endpoint=True, dtype=np.complex128)]
|
||||
|
||||
source_signal = modulator(E=source_signal, symbols = (symbols_x, symbols_y), constpoints= (constpts_x, constpts_y))
|
||||
|
||||
c_data.E_in = source_signal[0]['E']
|
||||
|
||||
py_fiber = pypho.fiber(
|
||||
glova = py_glova,
|
||||
l=config['fiber']['length'],
|
||||
alpha=pypho.functions.dB_to_Neper(config['fiber']['alpha'])/1000,
|
||||
gamma=config['fiber']['gamma'],
|
||||
D=config['fiber']['d'],
|
||||
S=config['fiber']['s'],
|
||||
)
|
||||
py_fiber.birefarray = pypho.birefringence_segment.create_pmd_fibre(py_fiber.l, py_fiber.l/config['fiber']['birefsteps'], 0, config['fiber']['birefseed'])
|
||||
c_params = pypho.cfiber.ParamsWrapper.from_fiber(py_fiber)
|
||||
c_fiber = pypho.cfiber.FiberWrapper(c_data, c_params, c_glova)
|
||||
|
||||
return c_fiber, c_data
|
||||
|
||||
|
||||
def save_data(data, config):
|
||||
data_dir = Path(config['script']['data_dir'])
|
||||
save_dir = data_dir
|
||||
save_dir.mkdir(parents=True, exist_ok=True)
|
||||
save_data = np.column_stack([data.E_in[0], data.E_in[1], data.E_out[0], data.E_out[1]])
|
||||
timestamp = datetime.now()
|
||||
config_content = '\n'.join((
|
||||
f"; Generated by {str(Path(__file__).name)} @ {timestamp.strftime("%Y-%m-%d %H:%M:%S")}",
|
||||
"[glova]",
|
||||
f"sps = {config['glova']['sps']}",
|
||||
f"nos = {config['glova']['nos']}",
|
||||
f"f0 = {config['glova']['f0']}",
|
||||
f"symbolrate = {config['glova']['symbolrate']}",
|
||||
f'wisdom_dir = "{config['glova']['wisdom_dir']}"',
|
||||
f'flags = "{config["glova"]["flags"]}"',
|
||||
f"nthreads = {config['glova']['nthreads']}",
|
||||
"",
|
||||
"[fiber]",
|
||||
f"length = {config['fiber']['length']}",
|
||||
f"gamma = {config['fiber']['gamma']}",
|
||||
f"alpha = {config['fiber']['alpha']}",
|
||||
f"D = {config['fiber']['d']}",
|
||||
f"S = {config['fiber']['s']}",
|
||||
f"birefsteps = {config['fiber']['birefsteps']}",
|
||||
f"birefseed = {config['fiber']['birefseed']}",
|
||||
"",
|
||||
"[signal]",
|
||||
f'modulation = "{config['signal']['modulation']}"',
|
||||
f"mod_order = {config['signal']['mod_order']}",
|
||||
f"seed = {config['signal']['seed']}",
|
||||
f'pulse_shape = "{config["signal"]["pulse_shape"]}"',
|
||||
f"fwhm = {config['signal']['fwhm']}",
|
||||
"",
|
||||
"[script]",
|
||||
f'data_dir = "{config["script"]["data_dir"]}"',
|
||||
))
|
||||
config_hash = hashlib.md5(config_content.encode()).hexdigest()
|
||||
save_file = f"npys/{config_hash}.npy"
|
||||
config_content += f'\n\n[DATA]\nfile = "{str(save_file)}\n"'
|
||||
|
||||
filename_components = (
|
||||
timestamp.strftime("%Y%m%d-%H%M%S"),
|
||||
config['glova']['sps'],
|
||||
config['glova']['nos'],
|
||||
config['fiber']['length'],
|
||||
config['fiber']['gamma'],
|
||||
config['fiber']['alpha'],
|
||||
config['fiber']['d'],
|
||||
config['fiber']['s'],
|
||||
f"{config['signal']['modulation'].upper()}{config['signal']['mod_order']}",
|
||||
)
|
||||
|
||||
lookup_file = "-".join(map(str, filename_components)) + ".ini"
|
||||
with open(data_dir / lookup_file, "w") as f:
|
||||
f.write(config_content)
|
||||
|
||||
np.save(save_dir / save_file, save_data)
|
||||
|
||||
print("Saved config to", data_dir / lookup_file)
|
||||
print("Saved data to", save_dir / f"{config_hash}.npy")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
config = get_config()
|
||||
|
||||
# loop over lengths
|
||||
# lengths = [1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000]
|
||||
lengths = [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000]
|
||||
lengths = sorted(lengths)
|
||||
input_override = None
|
||||
for lind, length in enumerate(lengths):
|
||||
print(f"\nGenerating data for fiber length {length}")
|
||||
# if lind > 0:
|
||||
# # set the length to the difference between the current and previous length -> incremental
|
||||
# length = lengths[lind] - lengths[lind-1]
|
||||
# print(f"\nGenerating data for fiber length {lengths[lind]}m -> {length}m")
|
||||
config['fiber']['length'] = length
|
||||
# set the input data to the output data of the previous run
|
||||
cfiber, cdata = initialize_fiber_and_data(config, input_data_override=input_override)
|
||||
cfiber()
|
||||
# input_override = cdata.E_out
|
||||
save_data(cdata, config)
|
||||
# cfiber, cdata = initialize_fiber_and_data(config)
|
||||
# cfiber()
|
||||
# save_data(cdata, config)
|
||||
|
||||
# fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
|
||||
# xax = np.linspace(0, cfiber.glova.nos, cfiber.glova.nos*cfiber.glova.sps)-0.5
|
||||
# axs[0,0].plot(xax,np.abs(cdata.E_in[0]))
|
||||
# axs[1,0].plot(xax,np.abs(cdata.E_in[1]))
|
||||
# axs[0,1].plot(xax,np.abs(cdata.E_out[0]))
|
||||
# axs[1,1].plot(xax,np.abs(cdata.E_out[1]))
|
||||
|
||||
# plt.show()
|
||||
Reference in New Issue
Block a user