Files
2026-10-12 14:26:51 +08:00

50 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""荧光衰减信号模型与真值预设(仅依赖 numpy,浏览器可运行)。
三套预设的来源与区别见 docs/项目说明.md §3.2 / §6:
- "monte_carlo" 报告 5.4.4 蒙特卡洛仿真真值(快成分 0.60 ns 为仿真设定值)
- "single" 单指数教学版(YAG:Ce 标称衰减 52.8 ns)
- "measured_ref" 实测参考(附图 A1 标注寿命;权重原始材料未给出,沿用 0.16/0.84 为假设值)
"""
import numpy as np
PRESETS = {
"monte_carlo": {"A1": 0.16, "tau1": 0.60, "A2": 0.84, "tau2": 52.80, "B0": 0.0, "B1": 0.0},
"single": {"A1": 0.0, "tau1": None, "A2": 1.0, "tau2": 52.8, "B0": 0.0, "B1": 0.0},
"measured_ref": {"A1": 0.16, "tau1": 5.0, "A2": 0.84, "tau2": 52.5, "B0": 0.0, "B1": 0.0},
}
def resolve_preset(preset, tau=None, amplitude=1.0, b0=0.0, b1=0.0):
"""按预设名取参数;允许覆盖 tau/幅度/基线(单指数预设)。"""
if preset not in PRESETS:
raise ValueError(f"未知预设 {preset!r},可选:{list(PRESETS)}")
p = dict(PRESETS[preset])
if preset == "single":
if tau is not None:
p["tau2"] = float(tau)
p["A2"] = float(amplitude)
if b0:
p["B0"] = float(b0)
if b1:
p["B1"] = float(b1)
return p
def generate(t, params):
"""荧光衰减信号 I(t) = A1·exp(-t/tau1) + A2·exp(-t/tau2) + B0 + B1·t。
tau 为 None 或幅度为 0 时对应项不参与(单指数退化)。
"""
t = np.asarray(t, dtype=float)
y = np.full(t.shape, float(params.get("B0", 0.0))) + float(params.get("B1", 0.0)) * t
a1 = float(params.get("A1", 0.0))
tau1 = params.get("tau1")
if a1 != 0.0 and tau1:
y = y + a1 * np.exp(-t / float(tau1))
a2 = float(params.get("A2", 0.0))
tau2 = params.get("tau2")
if a2 != 0.0 and tau2:
y = y + a2 * np.exp(-t / float(tau2))
return y