Pulse: fit gaussian

This commit is contained in:
Eric Teunis de Boone 2023-04-24 17:52:19 +02:00
parent 8f42db2a99
commit 81b3502de5

View file

@ -2,9 +2,10 @@
from lib import util from lib import util
from scipy import signal, interpolate from scipy import signal, interpolate, stats
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
from itertools import zip_longest
try: try:
from tqdm import tqdm from tqdm import tqdm
@ -203,8 +204,6 @@ if __name__ == "__main__":
antenna.peak_sample = antenna.peak_time/antenna.dt antenna.peak_sample = antenna.peak_time/antenna.dt
antenna_true_signal = antenna.signal antenna_true_signal = antenna.signal
true_time_offset = antenna.peak_time - template.peak_time true_time_offset = antenna.peak_time - template.peak_time
if do_plots: if do_plots:
@ -296,6 +295,9 @@ if __name__ == "__main__":
best_time_lag = best_sample_lag * lag_dt best_time_lag = best_sample_lag * lag_dt
time_residuals[j] = best_time_lag - true_time_offset time_residuals[j] = best_time_lag - true_time_offset
if not do_plots:
continue
if do_plots and axs2: if do_plots and axs2:
axs2[-1].axvline(best_time_lag, color='r', alpha=0.5, linewidth=2) axs2[-1].axvline(best_time_lag, color='r', alpha=0.5, linewidth=2)
axs2[-1].axvline(true_time_offset, color='g', alpha=0.5, linewidth=2) axs2[-1].axvline(true_time_offset, color='g', alpha=0.5, linewidth=2)
@ -368,13 +370,59 @@ if __name__ == "__main__":
# Make a plot of the time residuals # Make a plot of the time residuals
if len(time_residuals) > 1: if len(time_residuals) > 1:
hist_kwargs = dict(bins='sqrt', density=False, alpha=0.8, histtype='step')
fig, ax = plt.subplots() fig, ax = plt.subplots()
ax.set_title("Template Correlation Lag finding") ax.set_title(
"Template Correlation Lag finding"
+ f"\n template dt: {template.dt*1e3: .1e}ps"
+ f"; antenna dt: {antenna.dt: .1e}ns"
+ f"; noise_factor: {noise_sigma_factor: .1e}"
)
ax.set_xlabel("Time Residual [ns]") ax.set_xlabel("Time Residual [ns]")
ax.set_ylabel("#") ax.set_ylabel("#")
ax.hist(time_residuals, bins='sqrt', density=False)
ax.legend(title=f"template dt: {template.dt: .1e}ns\nantenna dt: {antenna.dt: .1e}ns") counts, bins, _patches = ax.hist(time_residuals, **hist_kwargs)
if True: # fit gaussian to histogram
min_x = min(time_residuals)
max_x = max(time_residuals)
dx = bins[1] - bins[0]
scale = len(time_residuals) * dx
xs = np.linspace(min_x, max_x)
# do the fit
name = "Norm"
param_names = [ "$\\mu$", "$\\sigma$" ]
distr_func = stats.norm
label = name +"(" + ','.join(param_names) + ')'
# plot
fit_params = distr_func.fit(time_residuals)
fit_ys = scale * distr_func.pdf(xs, *fit_params)
ax.plot(xs, fit_ys, label=label)
# chisq
ct = np.diff(distr_func.cdf(bins, *fit_params))*np.sum(counts)
if True:
ct *= np.sum(counts)/np.sum(ct)
c2t = stats.chisquare(counts, ct, ddof=len(fit_params))
chisq_strs = [
f"$\\chi^2$/dof = {c2t[0]: .2g}/{len(fit_params)}"
]
# text on plot
text_str = "\n".join(
[label]
+
[ f"{param} = {value: .2e}" for param, value in zip_longest(param_names, fit_params, fillvalue='?') ]
+
chisq_strs
)
ax.text( *(0.02, 0.95), text_str, fontsize=12, ha='left', va='top', transform=ax.transAxes)
fig.savefig("figures/11_time_residual_hist.pdf") fig.savefig("figures/11_time_residual_hist.pdf")