use crate::processing::contracts::error::ProcessingError;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum FourierExponentSign {
#[default]
Negative,
Positive,
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
pub enum Window {
SineBell {
offset: f64,
end: f64,
power: f64,
first_point_scale: f64,
},
LorentzToGauss {
lb_hz: f64,
gb_hz: f64,
},
Exponential {
lb_hz: f64,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ZeroFill {
pub(in crate::processing) target_points: usize,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FourierTransform {
pub(in crate::processing) sign: FourierExponentSign,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DelaySource {
AxisEvidence,
Explicit(f64),
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TimeDomainResidualPolicy {
CorrectFully,
IntegerOnlyRetainResidual,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DigitalFilterCorrection {
AcknowledgeZeroDelayV1,
FrequencyDomainPhaseRampV1(DelaySource),
TimeDomainShiftFoldV1 {
source: DelaySource,
policy: TimeDomainResidualPolicy,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PhaseCorrection {
pub(in crate::processing) p0_degrees: f64,
pub(in crate::processing) p1_degrees: f64,
pub(in crate::processing) pivot_fraction: f64,
}
impl FourierTransform {
#[allow(clippy::type_complexity)]
pub(crate) fn model_parts(&self) -> (&FourierExponentSign,) {
(&self.sign,)
}
#[allow(clippy::type_complexity)]
pub(crate) fn from_model_parts(
parts: (FourierExponentSign,),
) -> Result<Self, crate::internal::ModelError> {
let (sign,) = parts;
let value = Self { sign };
Ok(value)
}
}
impl PhaseCorrection {
#[allow(clippy::type_complexity)]
pub(crate) fn model_parts(&self) -> (&f64, &f64, &f64) {
(&self.p0_degrees, &self.p1_degrees, &self.pivot_fraction)
}
#[allow(clippy::type_complexity)]
pub(crate) fn from_model_parts(
parts: (f64, f64, f64),
) -> Result<Self, crate::internal::ModelError> {
let (p0_degrees, p1_degrees, pivot_fraction) = parts;
let value = Self {
p0_degrees,
p1_degrees,
pivot_fraction,
};
let value = Self::new(value.p0_degrees, value.p1_degrees, value.pivot_fraction)
.map_err(|e| crate::internal::ModelError::Validation(e.to_string()))?;
Ok(value)
}
}
impl ZeroFill {
#[allow(clippy::type_complexity)]
pub(crate) fn model_parts(&self) -> (&usize,) {
(&self.target_points,)
}
#[allow(clippy::type_complexity)]
pub(crate) fn from_model_parts(parts: (usize,)) -> Result<Self, crate::internal::ModelError> {
let (target_points,) = parts;
let value = Self { target_points };
Ok(value)
}
}
impl Window {
pub fn lorentz_to_gauss(lb_hz: f64, gb_hz: f64) -> Result<Self, ProcessingError> {
let window = Self::LorentzToGauss { lb_hz, gb_hz };
window
.validate()
.map_err(ProcessingError::InvalidParameter)?;
Ok(window)
}
pub fn sine_bell(
offset: f64,
end: f64,
power: f64,
first_point_scale: f64,
) -> Result<Self, ProcessingError> {
let window = Self::SineBell {
offset,
end,
power,
first_point_scale,
};
window
.validate()
.map_err(ProcessingError::InvalidParameter)?;
Ok(window)
}
pub fn exponential(lb_hz: f64) -> Result<Self, ProcessingError> {
let window = Self::Exponential { lb_hz };
window
.validate()
.map_err(ProcessingError::InvalidParameter)?;
Ok(window)
}
pub(crate) fn validate(&self) -> Result<(), &'static str> {
match *self {
Self::SineBell {
offset,
end,
power,
first_point_scale,
} if !offset.is_finite()
|| !end.is_finite()
|| !(0.0..=1.0).contains(&offset)
|| !(0.0..=1.0).contains(&end)
|| !power.is_finite()
|| power <= 0.0
|| !first_point_scale.is_finite()
|| first_point_scale < 0.0 =>
{
Err("sine-bell window")
}
Self::LorentzToGauss { lb_hz, gb_hz }
if !lb_hz.is_finite() || !gb_hz.is_finite() || gb_hz < 0.0 =>
{
Err("Lorentz-to-Gauss widths")
}
Self::Exponential { lb_hz } if !lb_hz.is_finite() => Err("exponential line broadening"),
_ => Ok(()),
}
}
}
impl ZeroFill {
pub fn new(target_points: usize) -> Result<Self, ProcessingError> {
if target_points == 0 {
return Err(ProcessingError::InvalidParameter("zero-fill target"));
}
Ok(Self { target_points })
}
pub fn target_points(self) -> usize {
self.target_points
}
}
impl FourierTransform {
pub fn new(sign: FourierExponentSign) -> Self {
Self { sign }
}
pub fn sign(self) -> FourierExponentSign {
self.sign
}
}
impl Default for FourierTransform {
fn default() -> Self {
Self::new(FourierExponentSign::Negative)
}
}
impl PhaseCorrection {
pub fn zero_order_degrees(p0_degrees: f64) -> Result<Self, ProcessingError> {
Self::new(p0_degrees, 0.0, 0.0)
}
pub fn with_first_order_degrees(self, p1_degrees: f64) -> Result<Self, ProcessingError> {
Self::new(self.p0_degrees, p1_degrees, self.pivot_fraction)
}
pub fn with_pivot_fraction(self, pivot_fraction: f64) -> Result<Self, ProcessingError> {
Self::new(self.p0_degrees, self.p1_degrees, pivot_fraction)
}
pub fn new(
p0_degrees: f64,
p1_degrees: f64,
pivot_fraction: f64,
) -> Result<Self, ProcessingError> {
if !p0_degrees.is_finite()
|| !p1_degrees.is_finite()
|| !pivot_fraction.is_finite()
|| !(0.0..=1.0).contains(&pivot_fraction)
{
return Err(ProcessingError::InvalidParameter("phase correction"));
}
Ok(Self {
p0_degrees,
p1_degrees,
pivot_fraction,
})
}
pub fn p0_degrees(self) -> f64 {
self.p0_degrees
}
pub fn p1_degrees(self) -> f64 {
self.p1_degrees
}
pub fn pivot_fraction(self) -> f64 {
self.pivot_fraction
}
}