#[derive(Debug, Clone)]
pub struct MzCalibrator {
pub timebase: f64,
pub delay: f64,
pub c0: f64,
pub b: f64, pub c2: f64,
pub c3: f64,
pub c4: f64,
}
impl MzCalibrator {
#[allow(clippy::too_many_arguments)]
pub fn new(
model_type: i64,
timebase: f64,
delay: f64,
t1_ref: f64,
t2_ref: f64,
dc1: f64,
dc2: f64,
c0: f64,
c1: f64,
c2: f64,
c3: f64,
c4: f64,
t1_frame: f64,
t2_frame: f64,
) -> Self {
let tc = 1.0 + (dc1 * (t1_ref - t1_frame) + dc2 * (t2_ref - t2_frame)) / 1.0e6;
let b = (1.0e12 / (c1 * tc)).sqrt();
let c2 = c2 / tc;
let c3 = if model_type == 1 { c3 } else { 0.0 };
Self { timebase, delay, c0, b, c2, c3, c4 }
}
#[inline]
fn tof_index_to_time(&self, tof_index: f64) -> f64 {
tof_index * self.timebase + self.delay
}
pub fn tof_to_mz(&self, tof_index: u32) -> f64 {
let t = self.tof_index_to_time(tof_index as f64);
let s0 = (t - self.c0) / self.b;
let s = if self.c3 != 0.0 {
let mut s = s0;
for _ in 0..8 {
let f = self.c0 + self.b * s + self.c2 * s * s + self.c3 * s * s * s - t;
let df = self.b + 2.0 * self.c2 * s + 3.0 * self.c3 * s * s;
if df == 0.0 {
break;
}
let step = f / df;
s -= step;
if step.abs() < 1e-12 {
break;
}
}
s
} else if self.c2 != 0.0 {
let disc = self.b * self.b - 4.0 * self.c2 * (self.c0 - t);
if disc < 0.0 {
s0
} else {
let q = -0.5 * (self.b + disc.sqrt());
(self.c0 - t) / q
}
} else {
s0
};
s * s - self.c4
}
pub fn mz_to_tof(&self, mz: f64) -> u32 {
let s = (mz + self.c4).max(0.0).sqrt();
let t = self.c0 + self.b * s + self.c2 * s * s + self.c3 * s * s * s;
(((t - self.delay) / self.timebase).round()).max(0.0) as u32
}
}
#[derive(Debug, Clone)]
pub struct MobilityCalibrator {
pub ndelay: f64,
pub dv_start: f64,
pub ttrans: f64,
pub c0m: f64,
pub c1m: f64,
pub vmin: f64,
pub vmax: f64,
pub slope: f64,
}
impl MobilityCalibrator {
#[allow(clippy::too_many_arguments)]
pub fn new(
c0: f64,
c1: f64,
c2: f64,
c3: f64,
c4: f64,
_c5: f64,
c6: f64,
c7: f64,
c8: f64,
c9: f64,
) -> Self {
Self {
ndelay: c0,
dv_start: c2,
ttrans: c4,
c0m: c6,
c1m: c7,
vmin: c8,
vmax: c9,
slope: (c3 - c2) / c1,
}
}
#[inline]
fn voltage(&self, scan: f64) -> f64 {
let v = self.dv_start + self.slope * (scan - self.ttrans - self.ndelay);
v.clamp(self.vmin, self.vmax)
}
pub fn scan_to_one_over_k0(&self, scan: u32) -> f64 {
1.0 / (self.c0m + self.c1m / self.voltage(scan as f64))
}
pub fn one_over_k0_to_scan(&self, one_over_k0: f64) -> u32 {
let inv = 1.0 / one_over_k0;
let v = self.c1m / (inv - self.c0m);
let scan = (v - self.dv_start) / self.slope + self.ttrans + self.ndelay;
scan.round().max(0.0) as u32
}
}