modewise_signals/
sampling_rate.rs1use crate::SignalError;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct SamplingRate(f64);
6
7impl SamplingRate {
8 pub fn new(hz: f64) -> Result<Self, SignalError> {
9 if hz.is_finite() && hz > 0.0 {
10 Ok(Self(hz))
11 } else {
12 Err(SignalError::InvalidSamplingRate(hz))
13 }
14 }
15
16 pub fn hz(self) -> f64 {
17 self.0
18 }
19
20 pub fn period_s(self) -> f64 {
21 1.0 / self.0
22 }
23}
24
25#[cfg(test)]
26mod tests {
27 use super::*;
28
29 #[test]
30 fn accepts_positive_rate() {
31 let fs = SamplingRate::new(1.25).unwrap();
32 assert_eq!(fs.hz(), 1.25);
33 assert_eq!(fs.period_s(), 0.8);
34 }
35
36 #[test]
37 fn rejects_zero_negative_and_non_finite() {
38 for hz in [0.0, -1.0, f64::NAN, f64::INFINITY] {
39 assert!(SamplingRate::new(hz).is_err(), "accepted {hz}");
40 }
41 }
42}