Skip to content

Commit f96e406

Browse files
committed
fix: Add zero numtap check in Firwin validation
Scipy returns a value error in this case.
1 parent 7106a98 commit f96e406

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ fn firwin_dyn_validate<F: Real + PartialOrd, W: Real>(
3030
reason: "At least one cutoff frequency must be given.".into(),
3131
});
3232
}
33+
if *numtaps == 0 {
34+
return Err(Error::InvalidArg {
35+
arg: "numtaps".into(),
36+
reason: "Invalid numtaps: Nonzero-numtaps is expected!".into(),
37+
});
38+
}
3339

3440
// Whilst it may be faster to write
3541
// if *cutoff.iter().min().unwrap() <= F::zero() || *cutoff.iter().max().unwrap() >= F::one() {
@@ -624,6 +630,26 @@ mod test {
624630
}
625631
);
626632
}
633+
{
634+
// numtaps = 0 has ValueError in Python
635+
let cutoff: Vec<f64> = vec![0.2, 0.2];
636+
let decreasing_cutoff: Result<Vec<f64>, E> = firwin_dyn(
637+
0,
638+
&cutoff,
639+
None,
640+
None::<&Hamming>,
641+
&FilterBandType::Bandpass,
642+
None,
643+
None,
644+
);
645+
assert_eq!(
646+
decreasing_cutoff.unwrap_err(),
647+
E::InvalidArg {
648+
arg: "numtaps".into(),
649+
reason: "Invalid numtaps: Nonzero-numtaps is expected!".into()
650+
}
651+
);
652+
}
627653
{
628654
// Lowpass
629655
let cutoff: Vec<f64> = vec![0.2, 0.7];
@@ -746,7 +772,7 @@ mod test {
746772
conflicting.unwrap_err(),
747773
E::InvalidArg {
748774
arg: "cutoff".into(),
749-
reason: "Setting both window and with to something is silently ignored only in Scipy."
775+
reason: "Setting both window and width to something is silently ignored only in Scipy."
750776
.into()
751777
}
752778
);

0 commit comments

Comments
 (0)