80 lines
3.5 KiB
Python
80 lines
3.5 KiB
Python
"""M2 实验:四通道误差注入与校准(raw / 基线+增益校准 / 完整校准 三方案对比)。
|
||
|
||
运行:python -m fiber_delay_sim.experiments.exp03_m2_calibration [--seed 42]
|
||
"""
|
||
|
||
import argparse
|
||
|
||
import numpy as np
|
||
|
||
from .. import pipeline
|
||
from ..export import record
|
||
from ._plot import setup_style
|
||
|
||
|
||
def run(seed=42, runs_dir="runs"):
|
||
plt = setup_style()
|
||
rec = pipeline.run_m2({"snr_db": 60.0, "bits": 14}, seed=seed)
|
||
tau_true = rec["tau_true"]
|
||
|
||
# ---------- 图 1:三方案重建曲线 vs 理论 ----------
|
||
fig, ax = plt.subplots(figsize=(8, 4.6))
|
||
schemes = [("raw", "不校准", "#C25B2A", "o"),
|
||
("nominal", "基线+增益校准", "#6B2A35", "s"),
|
||
("calibrated", "完整校准", "#2D5F8A", "^")]
|
||
for key, label, color, mk in schemes:
|
||
d = rec[key]
|
||
ax.plot(d["t"], d["y"], mk, ms=3, color=color, label=label, alpha=0.75)
|
||
t_th = np.arange(0, 400, 0.5)
|
||
ax.plot(t_th, np.exp(-t_th / tau_true), "k-", lw=1.2,
|
||
label=f"理论 I(t)=e^(−t/{tau_true:g})")
|
||
ax.set_xlim(0, 180)
|
||
ax.set_xlabel("等效时间 u (ns)")
|
||
ax.set_ylabel("归一化强度")
|
||
ax.set_title("M2:误差注入后三种处理方案的重建曲线(0–180 ns)")
|
||
ax.legend(fontsize=9)
|
||
ax.grid(alpha=0.3)
|
||
|
||
# ---------- 图 2:连续度与 τ̂ 对比柱状 ----------
|
||
fig2, axes = plt.subplots(1, 2, figsize=(8.5, 3.8))
|
||
names = ["raw", "nominal", "calibrated"]
|
||
labels = ["不校准", "基线+增益", "完整校准"]
|
||
jumps = [rec[n]["continuity"]["mean_jump"] for n in names]
|
||
errs = [abs(rec[n]["tau_hat"] - tau_true) / tau_true * 100 for n in names]
|
||
axes[0].bar(labels, jumps, color=["#C25B2A", "#6B2A35", "#2D5F8A"])
|
||
axes[0].set_ylabel("重建连续度 mean_jump")
|
||
axes[0].set_title("校准降低通道断层")
|
||
axes[1].bar(labels, errs, color=["#C25B2A", "#6B2A35", "#2D5F8A"])
|
||
axes[1].set_ylabel("|τ̂−τ|/τ (%)")
|
||
axes[1].set_title(f"寿命拟合偏差(真值 {tau_true} ns)")
|
||
for ax2 in axes:
|
||
ax2.grid(alpha=0.3, axis="y")
|
||
|
||
print(f"\n=== M2 三方案对比(真值 τ = {tau_true} ns)===")
|
||
print(f"{'方案':<12} {'τ̂(ns)':>10} {'偏差%':>8} {'点数':>6} {'连续度(mean)':>12}")
|
||
for key, label in zip(names, labels):
|
||
d = rec[key]
|
||
err = abs(d["tau_hat"] - tau_true) / tau_true * 100 if d["tau_hat"] else float("nan")
|
||
print(f"{label:<12} {d['tau_hat']:>10.4f} {err:>8.2f} {d['n_points']:>6d} "
|
||
f"{d['continuity']['mean_jump']:>12.5f}")
|
||
print(f"完整校准系数:增益={[round(g, 3) for g in rec['calibrated']['gains']]}")
|
||
print(f"基线(相对峰值)={[round(b, 4) for b in rec['calibrated']['baselines']]}")
|
||
print(f"延时估计(ns)={[round(d, 3) for d in rec['calibrated']['delay_est_ns']]}")
|
||
|
||
run_dir = record.save_run(
|
||
runs_dir, "exp03_m2_calibration", records=[rec],
|
||
figures=[("fig1_reconstruction", fig), ("fig2_compare", fig2)],
|
||
notes=("M2 校准对比:raw / 基线+增益校准 / 完整校准。"
|
||
"校准价值体现在重建曲线连续度与 τ̂ 有界性;"
|
||
"延时常数错位不改变单指数拟合的 τ(被幅度自由度吸收)——教学点。"),
|
||
seed=seed)
|
||
print(f"\n[M2] 记录包:{run_dir}")
|
||
return run_dir
|
||
|
||
|
||
if __name__ == "__main__":
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("--seed", type=int, default=42)
|
||
ap.add_argument("--runs-dir", default="runs")
|
||
a = ap.parse_args()
|
||
run(seed=a.seed, runs_dir=a.runs_dir) |