Skip to content

Commit a0edbb3

Browse files
committed
Fix lack of use of num_traits::identities
Older commits didn't utilise some properities afforded by Real and RealField. Also added a useless(?) cfg(attr = "alloc") compiler attribute for consistency.
1 parent 2017a92 commit a0edbb3

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

sci-rs/src/signal/filter/design/kaiser.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use core::f64::consts::PI;
2-
use core::ops::Mul;
1+
use nalgebra::RealField;
32
use num_traits::{real::Real, MulAdd, Pow, ToPrimitive};
43

54
/// Compute the Kaiser parameter `beta`, given the attenuation `a`.
@@ -43,8 +42,8 @@ where
4342
F::from(0.5842).unwrap(),
4443
F::from(0.07886).unwrap() * a,
4544
)
46-
} else if a > F::from(0).unwrap() {
47-
F::from(0).unwrap()
45+
} else if a > F::zero() {
46+
F::zero()
4847
} else {
4948
panic!("Expected a positive input.")
5049
}
@@ -84,11 +83,11 @@ where
8483
/// [kaiserord], [kaiser_beta]
8584
pub fn kaiser_atten<F>(numtaps: u32, width: F) -> F
8685
where
87-
F: Real + MulAdd<Output = F>,
86+
F: Real + MulAdd<Output = F> + RealField,
8887
{
8988
MulAdd::mul_add(
9089
width,
91-
F::from(numtaps - 1).unwrap() * F::from(2.285 * PI).unwrap(),
90+
F::from(numtaps - 1).unwrap() * F::from(2.285).unwrap() * F::pi(),
9291
F::from(7.95).unwrap(),
9392
)
9493
}
@@ -217,16 +216,16 @@ where
217216
///
218217
pub fn kaiserord<F>(ripple: F, width: F) -> (F, F)
219218
where
220-
F: Real + MulAdd<Output = F> + Pow<F, Output = F>,
219+
F: Real + MulAdd<Output = F> + Pow<F, Output = F> + RealField,
221220
{
222-
let a = ripple.abs();
221+
let a = Real::abs(ripple);
223222
if a < F::from(8).unwrap() {
224223
panic!("Requested maximum ripple attenuation is too small for the Kaiser formula.");
225224
}
226225
let beta = kaiser_beta(a);
227226
let numtaps =
228-
F::from(1).unwrap() + (a - F::from(7.95).unwrap()) / (F::from(2.285 * PI).unwrap() * width);
229-
(numtaps.ceil(), beta)
227+
F::one() + (a - F::from(7.95).unwrap()) / (F::from(2.285).unwrap() * F::pi() * width);
228+
(Real::ceil(numtaps), beta)
230229
}
231230

232231
#[cfg(test)]

sci-rs/src/signal/windows/boxcar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ where
6262
let (m, needs_trunc) = extend(self.m, self.sym);
6363

6464
if !needs_trunc {
65-
vec![W::from(1).unwrap(); m]
65+
vec![W::one(); m]
6666
} else {
67-
vec![W::from(1).unwrap(); m - 1]
67+
vec![W::one(); m - 1]
6868
}
6969
}
7070
}

sci-rs/src/signal/windows/general_hamming.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,8 @@ where
145145
/// [5]: #references
146146
#[cfg(feature = "alloc")]
147147
fn get_window(&self) -> Vec<W> {
148-
GeneralCosine::new(
149-
self.m,
150-
[self.alpha, F::from(1).unwrap() - self.alpha].into(),
151-
self.sym,
152-
)
153-
.get_window()
148+
GeneralCosine::new(self.m, [self.alpha, F::one() - self.alpha].into(), self.sym)
149+
.get_window()
154150
}
155151
}
156152

sci-rs/src/signal/windows/kaiser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ where
172172
/// [3]: #references
173173
/// [4]: #references
174174
/// [5]: #references
175+
#[cfg(feature = "alloc")]
175176
fn get_window(&self) -> Vec<W> {
176177
if len_guard(self.m) {
177178
return Vec::<W>::new();

sci-rs/src/signal/windows/triangle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ where
7676
let w: Vec<W> = match m % 2 {
7777
0 => {
7878
let mut w: Vec<W> = n
79-
.map(|n| (W::from(2).unwrap() * n - W::from(1).unwrap()) / m_f)
79+
.map(|n| (W::from(2).unwrap() * n - W::one()) / m_f)
8080
.collect();
8181
w.extend(w.clone().iter().rev());
8282
w
8383
}
8484
1 => {
8585
let mut w: Vec<W> = n
86-
.map(|n| W::from(2).unwrap() * n / (m_f + W::from(1).unwrap()))
86+
.map(|n| W::from(2).unwrap() * n / (m_f + W::one()))
8787
.collect();
8888
w.extend(w.clone().iter().rev().skip(1));
8989
w

0 commit comments

Comments
 (0)