codexoptical-tail-detector-factorization.py
optical-tail-detector-factorization.py
"""Two optical/receiver factorizations with exactly the same recorded trace.
Run from the media output directory. Python3.14, NumPy and Matplotlib;
all dependencies are already declared in the repository pyproject.toml.
"""
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
energy_nj=100.0
tau_fast=2.0 # ns
tau_slow=10.0
t=np.linspace(0,60,1201)
fast=energy_nj/tau_fast*np.exp(-t/tau_fast) # nJ/ns = W
slow=energy_nj/tau_slow*np.exp(-t/tau_slow)
recorded=energy_nj/(tau_slow-tau_fast)*(np.exp(-t/tau_slow)-np.exp(-t/tau_fast))
assert np.all(recorded>=0)
# Independent quadrature of the convolution at several observation times.
for obs in [1.,5.,20.,40.]:
u=np.linspace(0,obs,100001)
calculated=np.trapezoid((energy_nj/tau_fast*np.exp(-u/tau_fast))
*(np.exp(-(obs-u)/tau_slow)/tau_slow),u)
expected=energy_nj/(tau_slow-tau_fast)*(np.exp(-obs/tau_slow)-np.exp(-obs/tau_fast))
np.testing.assert_allclose(calculated,expected,rtol=1e-8)
np.testing.assert_allclose(energy_nj*np.exp(-20/np.array([tau_fast,tau_slow])),
[.004539992976,13.5335283237],rtol=1e-9)
fig,axs=plt.subplots(1,2,figsize=(7,3.2),dpi=100,layout='constrained')
fig.set_facecolor('white')
for ax in axs:
ax.set_facecolor('white');ax.set(xlim=(0,50),xlabel='Time (ns)')
ax.tick_params(labelsize=8);ax.grid(alpha=.2)
axs[0].plot(t,fast,label='A:2ns optical pulse',color='#246a9a',lw=2)
axs[0].plot(t,slow,label='B:10ns optical pulse',color='#c16a16',lw=2)
axs[0].axvline(20,color='#555555',ls='--',lw=1)
axs[0].set(yscale='log',ylim=(1e-4,100),ylabel='Incident optical power (W)',title='Different true optical tails')
axs[0].legend(loc='upper right',fontsize=8)
axs[1].plot(t,recorded,lw=2,color='#6d3b87',label='A and B:identical trace')
axs[1].set(ylim=(0,8),ylabel='Calibrated recorded signal (W)',title='Same optical–detector convolution')
axs[1].legend(loc='upper right',fontsize=8)
axs[1].text(.48,.55,'A:10ns detector\nB:2ns detector',transform=axs[1].transAxes,fontsize=9)
fig.savefig(Path.cwd()/'optical-tail-detector-factorization.png',facecolor='white')
plt.close(fig)