diff --git a/data_management.py b/data_management.py index 3df988d87dcd388aef7ba5011408535eed7c0f52..f26c505ff87db3152b676f6eaa2a24a2ae77d335 100644 --- a/data_management.py +++ b/data_management.py @@ -948,6 +948,12 @@ class DataManager: result = self.df_table.loc[selection] return result + def list_fourier_coef(self, **parameters): + result = self.list(has_fourier_coef=True, **parameters) + with self.engine.begin() as connection: + fourier_coef_table = pd.read_sql_table("fourier_coef", connection, index_col="id") + return result.join(fourier_coef_table, on="fourier_coef_id", how="left") + def load_all(self, fourier_coef=None, **parameters): table = self.list(**parameters) if fourier_coef is not None: diff --git a/gen_data.py b/gen_data.py index 856ba8fbfd3293e49e7e884ac68eceb6ccdd8410..d0a90296f5b8004f0f9d541a0a08e295a2d3fde9 100644 --- a/gen_data.py +++ b/gen_data.py @@ -107,8 +107,9 @@ def gen_option_iter(steps=None, scale=None, **options): for key, (dim, array) in iter_variables.items(): options[key] = array[index[dim]] result = options.copy() - if "vac_omega" in result: - result["vac"] = result.pop("vac_omega") * result["omega"] + vac_omega = result.pop("vac_omega", None) + if vac_omega is not None: + result["vac"] = vac_omega * result["omega"] settings.logger.debug('step %s/%s: '%(index, steps) + ', '.join('%s=%s'%(key, value) for (key, value) in result.items() if key in iter_variables)) yield result diff --git a/plot_pulses.py b/plot_pulses.py index 1789bf629eaca28ddb1723f35c8edb9eb5ed37c8..06c48abbd00725a19aeadf710a710ea1a67042f9 100644 --- a/plot_pulses.py +++ b/plot_pulses.py @@ -9,6 +9,7 @@ import argparse import numpy as np import settings from data_management import DataManager +from scipy.optimize import leastsq from gen_pulse_data import fourier_coef_gauss, fourier_coef_gauss_symmetric import matplotlib.pyplot as plt from matplotlib.colors import LogNorm @@ -62,6 +63,47 @@ def current_time(dm, omega=0., pulse_duration=0.1, pulse_height=4., pulse_phase= plt.show() +def fit_fourier_coef(fourier_coef): + # gauss + narr = np.arange(1, fourier_coef.size+1) + func = lambda param: (param[0]*np.exp(-param[1]*narr**2) - fourier_coef).view(dtype=np.float64) + fit, cov = leastsq(func, (0.5, 0.1)) + residuals = (func(fit)**2).sum() + settings.logger.debug(f"fit_fourier_coef: fit {fit}, residuals {residuals:.3g}") + if residuals < 1e-3: + return dict(type="gauss", prefactor=fit[0], smoothen=fit[1]) + return dict(type="unknown") + + +def parameter_overview(dm, **trash): + table = dm.list_fourier_coef() + table["pulse_type"] = "unknown" + table["pulse_param1"] = 0. + table["pulse_param2"] = 0. + fourier_coef = np.array([table[f"fcr{i}"]+1j*table[f"fci{i}"] for i in range(10)]).T + for coef, idx in zip(fourier_coef, table.index): + parameters = fit_fourier_coef(coef) + match parameters["type"]: + case "gauss": + table.pulse_type[idx] = "gauss" + table.pulse_param1[idx] = parameters["prefactor"] + table.pulse_param2[idx] = parameters["smoothen"] + case _: + print(f"unknown type: {parameters['type']}") + fig, ax_gauss = plt.subplots() + + sel = table.pulse_type == "gauss" + gauss_frequency = table.omega[sel] + gauss_prefactor = table.pulse_param1[sel] + gauss_smoothen = table.pulse_param2[sel] + gauss_duration = 4*np.sqrt(gauss_smoothen*np.log(2)) / gauss_frequency + gauss_height = gauss_prefactor / np.sqrt(gauss_smoothen) * np.pi**0.5 + gauss_phase = gauss_height * gauss_duration / (4*(np.log(2)*np.pi)**0.5) + gauss_phase = gauss_prefactor / gauss_frequency + ax_gauss.plot(gauss_duration, gauss_phase, "o") + plt.show() + + def main(): from logging import _levelToName parser = argparse.ArgumentParser(description="Generate plots for pulsed voltage")