180 lines
6.5 KiB
Python
180 lines
6.5 KiB
Python
"""测量链仿真:前端展宽 → 采样 → 噪声 → ADC 量化(仅依赖 numpy)。
|
||
|
||
物理顺序对应 docs/实现方案.md §7.2。
|
||
"""
|
||
|
||
import numpy as np
|
||
|
||
|
||
def snr_to_sigma(snr_db, peak):
|
||
"""峰值定义的信噪比 → 噪声标准差:σ = peak · 10^(−SNR/20)。"""
|
||
return float(peak) * 10.0 ** (-float(snr_db) / 20.0)
|
||
|
||
|
||
def gaussian_response(t, y, sigma_ns):
|
||
"""模拟前端有限带宽:与高斯冲激响应卷积(sigma_ns<=0 时原样返回)。
|
||
|
||
sigma_ns 参考值 1.373 ns(原项目 Fisher 分析中的系统响应,报告 5.4.5)。
|
||
"""
|
||
if sigma_ns is None or sigma_ns <= 0:
|
||
return y
|
||
t = np.asarray(t, dtype=float)
|
||
y = np.asarray(y, dtype=float)
|
||
dt = t[1] - t[0]
|
||
half = max(1, int(np.ceil(4.0 * sigma_ns / dt)))
|
||
k = (np.arange(2 * half + 1) - half) * dt
|
||
kernel = np.exp(-0.5 * (k / sigma_ns) ** 2)
|
||
kernel = kernel / kernel.sum()
|
||
padded = np.pad(y, half, mode="edge")
|
||
return np.convolve(padded, kernel, mode="valid")
|
||
|
||
|
||
def quantize_adc(y, bits, fullscale):
|
||
"""理想 ADC 均匀量化:LSB = fullscale / (2^bits − 1)。"""
|
||
lsb = float(fullscale) / (2 ** int(bits) - 1)
|
||
return np.round(np.asarray(y, dtype=float) / lsb) * lsb
|
||
|
||
|
||
def sample_points(t_cont, y_cont, t0_ns, dt_ns, n_samples):
|
||
"""理想采样:t_k = t0 + k·Δt 处线性插值取值。"""
|
||
ts = float(t0_ns) + float(dt_ns) * np.arange(int(n_samples))
|
||
return ts, np.interp(ts, np.asarray(t_cont, dtype=float), np.asarray(y_cont, dtype=float))
|
||
|
||
|
||
def run_single_channel(t_cont, y_cont, params, rng):
|
||
"""单通道测量链(M0)。
|
||
|
||
params:
|
||
dt_ns 采样间隔(默认 10)
|
||
t0_ns 触发位置(默认 0)
|
||
window_ns 记录窗口(决定采样点数)
|
||
snr_db 信噪比(None → 无噪声)
|
||
bits ADC 位数(None → 不量化)
|
||
fullscale ADC 满量程(None → 2×峰值)
|
||
bandwidth_sigma_ns 前端高斯响应宽度(0 → 不展宽)
|
||
jitter_ns 时钟抖动标准差(默认 0)
|
||
返回 dict(全部纯数据)。
|
||
"""
|
||
p = dict(params)
|
||
t_cont = np.asarray(t_cont, dtype=float)
|
||
y_cont = np.asarray(y_cont, dtype=float)
|
||
|
||
dt = float(p.get("dt_ns", 10.0))
|
||
t0 = float(p.get("t0_ns", 0.0))
|
||
window = float(p.get("window_ns", t_cont[-1] - t_cont[0]))
|
||
n_samples = int(round(window / dt))
|
||
|
||
# 1) 前端带宽
|
||
y_bw = gaussian_response(t_cont, y_cont, p.get("bandwidth_sigma_ns", 0.0))
|
||
|
||
# 2) 采样(含可选时钟抖动)
|
||
ts = t0 + dt * np.arange(n_samples)
|
||
jitter = float(p.get("jitter_ns", 0.0))
|
||
if jitter > 0:
|
||
ts_eff = ts + rng.normal(0.0, jitter, size=n_samples)
|
||
else:
|
||
ts_eff = ts
|
||
y_clean = np.interp(ts_eff, t_cont, y_bw)
|
||
|
||
# 3) 噪声(模拟域)
|
||
snr_db = p.get("snr_db")
|
||
peak = float(np.max(y_bw)) if np.max(y_bw) > 0 else 1.0
|
||
sigma = snr_to_sigma(snr_db, peak) if snr_db is not None else 0.0
|
||
y_meas = y_clean + (rng.normal(0.0, sigma, size=n_samples) if sigma > 0 else 0.0)
|
||
|
||
# 4) ADC 量化(数字域)
|
||
bits = p.get("bits")
|
||
if bits is not None:
|
||
fs = p.get("fullscale") or 2.0 * peak
|
||
y_meas = quantize_adc(y_meas, bits, fs)
|
||
|
||
return {
|
||
"t_samples": ts,
|
||
"y_clean": y_clean,
|
||
"y_samples": y_meas,
|
||
"sigma": sigma,
|
||
"snr_db": snr_db,
|
||
"bits": bits,
|
||
"n_samples": n_samples,
|
||
"jitter_ns": jitter,
|
||
"bandwidth_sigma_ns": float(p.get("bandwidth_sigma_ns", 0.0) or 0.0),
|
||
}
|
||
|
||
|
||
def _interp_signal(s_times, t_cont, y_cont):
|
||
"""在等效时间 s 处取值;s<0(信号未到达)按 0 处理(触发前无信号)。"""
|
||
s = np.asarray(s_times, dtype=float)
|
||
y = np.interp(s, t_cont, y_cont) # 先按边界插值(s<0 → y(0))
|
||
y = np.where(s < 0, 0.0, y) # s<0 → 0(激发关断前无荧光)
|
||
return y
|
||
|
||
|
||
def run_multi_channel(t_cont, y_cont, params, rng):
|
||
"""四通道光纤延时等效采样(M2 起)。
|
||
|
||
物理模型:四路信号副本经光纤延时 δk;同一 ADC 从触发时刻起以 Δt 采样,
|
||
第 k 路第 m 个样本采样到的是 x(t_m − δk),等效时间 s = t_m − δk。
|
||
|
||
params(在 M0 单通道参数基础上增加):
|
||
delays 名义延时列表(ns),如 [0, 47.5, 95.0, 142.5]
|
||
gain_errors 各通道相对增益误差(0 为理想)
|
||
delay_errors 各通道延时误差(ns,0 为理想)
|
||
baselines 各通道基线偏置(相对峰值,0 为理想)
|
||
返回 dict(纯数据)。
|
||
"""
|
||
p = dict(params)
|
||
t_cont = np.asarray(t_cont, dtype=float)
|
||
y_cont = np.asarray(y_cont, dtype=float)
|
||
|
||
delays = [float(d) for d in p["delays"]]
|
||
n_ch = len(delays)
|
||
gain_err = p.get("gain_errors", [0.0] * n_ch)
|
||
delay_err = p.get("delay_errors", [0.0] * n_ch)
|
||
base_frac = p.get("baselines", [0.0] * n_ch)
|
||
|
||
dt = float(p.get("dt_ns", 10.0))
|
||
t0 = float(p.get("t0_ns", 0.0))
|
||
window = float(p.get("window_ns", t_cont[-1] - t_cont[0]))
|
||
n_samples = int(round(window / dt))
|
||
|
||
y_bw = gaussian_response(t_cont, y_cont, p.get("bandwidth_sigma_ns", 0.0))
|
||
peak = float(np.max(y_bw)) if np.max(y_bw) > 0 else 1.0
|
||
snr_db = p.get("snr_db")
|
||
sigma = snr_to_sigma(snr_db, peak) if snr_db is not None else 0.0
|
||
bits = p.get("bits")
|
||
|
||
ts_m = t0 + dt * np.arange(n_samples) # ADC 绝对采样时刻
|
||
channels = []
|
||
for k in range(n_ch):
|
||
d_eff = delays[k] + delay_err[k] # 实际延时(含误差)
|
||
s = ts_m - d_eff # 等效时间(相对触发)
|
||
y_clean = _interp_signal(s, t_cont, y_bw)
|
||
y_clean = y_clean * (1.0 + gain_err[k]) + base_frac[k] * peak
|
||
y_meas = y_clean + (rng.normal(0.0, sigma, size=n_samples) if sigma > 0 else 0.0)
|
||
if bits is not None:
|
||
fs = p.get("fullscale") or 2.0 * peak
|
||
y_meas = quantize_adc(y_meas, bits, fs)
|
||
channels.append({
|
||
"index": k,
|
||
"delay_ns": delays[k],
|
||
"delay_eff_ns": d_eff,
|
||
"gain": 1.0 + gain_err[k],
|
||
"baseline": base_frac[k] * peak,
|
||
"t_adc": ts_m,
|
||
"s_equiv": s, # 等效时间(校准前:用设计延时)
|
||
"y_clean": y_clean,
|
||
"y_samples": y_meas,
|
||
})
|
||
|
||
return {
|
||
"n_channels": n_ch,
|
||
"dt_ns": dt,
|
||
"t0_ns": t0,
|
||
"n_samples": n_samples,
|
||
"sigma": sigma,
|
||
"snr_db": snr_db,
|
||
"bits": bits,
|
||
"peak": peak,
|
||
"channels": channels,
|
||
}
|