Files
chenproject/fiber_delay_sim/core/acquisition.py
2026-10-12 14:26:51 +08:00

102 lines
3.5 KiB
Python
Raw 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.

"""测量链仿真:前端展宽 → 采样 → 噪声 → 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),
}