Skip to content

Commit

Permalink
Fix lack of use of num_traits::identities
Browse files Browse the repository at this point in the history
Older commits didn't utilise some properities afforded by Real and
RealField.

Also added a useless(?) cfg(attr = "alloc") compiler attribute for
consistency.
  • Loading branch information
SpookyYomo committed Dec 9, 2024
1 parent 79420b9 commit 13e96f1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
19 changes: 9 additions & 10 deletions sci-rs/src/signal/filter/design/kaiser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::f64::consts::PI;
use core::ops::Mul;
use nalgebra::RealField;
use num_traits::{real::Real, MulAdd, Pow, ToPrimitive};

/// Compute the Kaiser parameter `beta`, given the attenuation `a`.
Expand Down Expand Up @@ -43,8 +42,8 @@ where
F::from(0.5842).unwrap(),
F::from(0.07886).unwrap() * a,
)
} else if a > F::from(0).unwrap() {
F::from(0).unwrap()
} else if a > F::zero() {
F::zero()
} else {
panic!("Expected a positive input.")
}
Expand Down Expand Up @@ -84,11 +83,11 @@ where
/// [kaiserord], [kaiser_beta]
pub fn kaiser_atten<F>(numtaps: u32, width: F) -> F
where
F: Real + MulAdd<Output = F>,
F: Real + MulAdd<Output = F> + RealField,
{
MulAdd::mul_add(
width,
F::from(numtaps - 1).unwrap() * F::from(2.285 * PI).unwrap(),
F::from(numtaps - 1).unwrap() * F::from(2.285).unwrap() * F::pi(),
F::from(7.95).unwrap(),
)
}
Expand Down Expand Up @@ -217,16 +216,16 @@ where
///
pub fn kaiserord<F>(ripple: F, width: F) -> (F, F)
where
F: Real + MulAdd<Output = F> + Pow<F, Output = F>,
F: Real + MulAdd<Output = F> + Pow<F, Output = F> + RealField,
{
let a = ripple.abs();
let a = Real::abs(ripple);
if a < F::from(8).unwrap() {
panic!("Requested maximum ripple attenuation is too small for the Kaiser formula.");
}
let beta = kaiser_beta(a);
let numtaps =
F::from(1).unwrap() + (a - F::from(7.95).unwrap()) / (F::from(2.285 * PI).unwrap() * width);
(numtaps.ceil(), beta)
F::one() + (a - F::from(7.95).unwrap()) / (F::from(2.285).unwrap() * F::pi() * width);
(Real::ceil(numtaps), beta)
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions sci-rs/src/signal/windows/boxcar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ where
let (m, needs_trunc) = extend(self.m, self.sym);

if !needs_trunc {
vec![W::from(1).unwrap(); m]
vec![W::one(); m]
} else {
vec![W::from(1).unwrap(); m - 1]
vec![W::one(); m - 1]
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions sci-rs/src/signal/windows/general_hamming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,8 @@ where
/// [5]: #references
#[cfg(feature = "alloc")]
fn get_window(&self) -> Vec<W> {
GeneralCosine::new(
self.m,
[self.alpha, F::from(1).unwrap() - self.alpha].into(),
self.sym,
)
.get_window()
GeneralCosine::new(self.m, [self.alpha, F::one() - self.alpha].into(), self.sym)
.get_window()
}
}

Expand Down
1 change: 1 addition & 0 deletions sci-rs/src/signal/windows/kaiser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ where
/// [3]: #references
/// [4]: #references
/// [5]: #references
#[cfg(feature = "alloc")]
fn get_window(&self) -> Vec<W> {
if len_guard(self.m) {
return Vec::<W>::new();
Expand Down
4 changes: 2 additions & 2 deletions sci-rs/src/signal/windows/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ where
let w: Vec<W> = match m % 2 {
0 => {
let mut w: Vec<W> = n
.map(|n| (W::from(2).unwrap() * n - W::from(1).unwrap()) / m_f)
.map(|n| (W::from(2).unwrap() * n - W::one()) / m_f)
.collect();
w.extend(w.clone().iter().rev());
w
}
1 => {
let mut w: Vec<W> = n
.map(|n| W::from(2).unwrap() * n / (m_f + W::from(1).unwrap()))
.map(|n| W::from(2).unwrap() * n / (m_f + W::one()))
.collect();
w.extend(w.clone().iter().rev().skip(1));
w
Expand Down

0 comments on commit 13e96f1

Please sign in to comment.