"""M1 光纤设计实验:名义四组 → 延时/相位表 + 反向设计 + 相位图。 运行:python -m fiber_delay_sim.experiments.exp02_fiber_design """ import argparse import numpy as np from ..core import fiber from ..export import record from ._plot import setup_style def _print_table(title, rows, header): print(f"\n=== {title} ===") print(" | ".join(f"{h:>12}" for h in header)) for r in rows: print(" | ".join(f"{c:>12}" if isinstance(c, str) else f"{c:>12.4f}" for c in r)) def run(runs_dir="runs"): plt = setup_style() # ---------- 正向:名义四组(设计延时精确值) ---------- fwd = fiber.forward_design(delays_ns=fiber.NOMINAL_DELAYS_NS) rows = [] for ch in fwd["channels"]: rows.append((f"CH{ch['index'] + 1}", ch["delay_ns"], ch["phase_ns"], fiber.NOMINAL_LENGTHS_M[ch["index"]], ch["length_m"])) _print_table("名义四组:设计延时 / 相位 / 长度", rows, ["通道", "设计延时 ns", "相位 ns", "名义长度 m", "反算长度 m"]) u = fwd["uniformity"] print(f"排序后等效网格:{fwd['sorted_phases_ns']} ns" f"(理想步长 {u['ideal_step_ns']} ns,最大间隔偏差 {u['max_gap_dev_ns']:.3g} ns)") # ---------- 反向设计:目标 0/2.5/5/7.5 ---------- targets = [0.0, 2.5, 5.0, 7.5] inv = fiber.inverse_design(targets) assert inv["is_uniform_grid"] and inv["recommended"] rec_rows = [(e["phase_ns"], e["k"], e["delay_ns"], e["length_m"]) for e in inv["recommended"]] _print_table("反向设计推荐解(目标相位 0/2.5/5/7.5 ns)", rec_rows, ["目标相位 ns", "通道序 k", "延时 ns", "长度 m"]) print(f"构造式:{inv['construction']}") # ---------- 图 1:一个采样周期内的相位位置 ---------- fig1, ax = plt.subplots(figsize=(7, 3.2)) colors = ["#2D5F8A", "#C25B2A", "#2A6B5A", "#6B2A35"] for i, ch in enumerate(fwd["channels"]): ax.plot([ch["phase_ns"]], [1], "o", ms=12, color=colors[i % 4]) ax.annotate(f"CH{ch['index'] + 1}\nφ={ch['phase_ns']:.1f} ns", (ch["phase_ns"], 1), textcoords="offset points", xytext=(0, 14 + 12 * (i % 2)), ha="center", fontsize=9) ax.axhline(1, color="#9aa5b1", lw=1) for x in np.arange(0, 11, 2.5): ax.axvline(x, color="#d8dee5", lw=0.8, ls="--") ax.set_xlim(-1, 11) ax.set_ylim(0.6, 1.9) ax.set_yticks([]) ax.set_xticks(np.arange(0, 11, 2.5)) ax.set_xlabel("采样周期内相位 (ns)") ax.set_title("四路光纤延时在一个 10 ns 采样周期内的相位位置(0 / 2.5 / 5.0 / 7.5 ns)") ax.grid(alpha=0.2) # ---------- 图 2:相位圆图 ---------- fig2 = plt.figure(figsize=(5.2, 5.2)) ax2 = fig2.add_subplot(111, projection="polar") for i, ch in enumerate(fwd["channels"]): theta = 2 * np.pi * ch["phase_ns"] / fiber.DT_DEFAULT ax2.plot([theta], [1], "o", ms=14, color=colors[i % 4]) ax2.annotate(f"CH{ch['index'] + 1}", (theta, 1.15), ha="center", textcoords="data", fontsize=11) ax2.set_yticks([]) ax2.set_xticks(2 * np.pi * np.arange(0, 10, 2.5) / 10) ax2.set_xticklabels(["0 ns", "2.5 ns", "5.0 ns", "7.5 ns"]) ax2.set_title("相位圆图:10 ns 周期内的等效采样相位", pad=18) # ---------- 图 3:记录时间轴上的四路延时片段示意 ---------- fig3, ax3 = plt.subplots(figsize=(7.2, 3.6)) for i, ch in enumerate(fwd["channels"]): ax3.plot([ch["delay_ns"], ch["delay_ns"] + 20], [i, i], "-", lw=3, color=colors[i % 4], solid_capstyle="butt") ax3.annotate(f"CH{ch['index'] + 1}: δ={ch['delay_ns']:.1f} ns (L={fiber.NOMINAL_LENGTHS_M[ch['index']]:.2f} m)", (ch["delay_ns"], i), textcoords="offset points", xytext=(2, -14), fontsize=9) ax3.set_xlim(-5, 175) ax3.set_ylim(-0.8, 3.8) ax3.set_yticks([]) ax3.set_xlabel("记录时间轴 (ns)") ax3.set_title("四路延时片段在记录时间轴上的定位(片段间隔约 47.5 ns,相位错开 2.5 ns)") ax3.grid(alpha=0.25) # ---------- 记录包 ---------- pack = { "meta": {"experiment": "exp02_fiber_design", "data_origin": "software-simulated", "params": None}, "channels": fwd["channels"], "sorted_phases_ns": fwd["sorted_phases_ns"], "uniformity": u, "target_phases_ns": inv["target_phases_ns"], "is_uniform_grid": inv["is_uniform_grid"], "recommended": inv["recommended"], "construction": inv["construction"], } run_dir = record.save_run( runs_dir, "exp02_fiber_design", records=[pack], figures=[("fig1_phase_positions", fig1), ("fig2_phase_circle", fig2), ("fig3_delay_segments", fig3)], notes=("M1 光纤设计:名义四组 0/9.70/19.40/29.10 m → 延时 0/47.5/95/142.5 ns → " "相位 0/2.5/5.0/7.5 ns(均匀网格,最大间隔偏差 " f"{u['max_gap_dev_ns']:.3g} ns);反向设计复现原项目方案。"), ) print(f"\n[M1] 记录包:{run_dir}") return run_dir if __name__ == "__main__": ap = argparse.ArgumentParser(description="M1 光纤设计实验") ap.add_argument("--runs-dir", default="runs") a = ap.parse_args() run(runs_dir=a.runs_dir)