Skip to content

Commit

Permalink
lib.rs: code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
phip1611 committed Mar 4, 2023
1 parent 7a6bd70 commit a1a3b45
Showing 1 changed file with 1 addition and 23 deletions.
24 changes: 1 addition & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn samples_fft_to_spectrum(
if samples.iter().any(|x| x.is_infinite()) {
return Err(SpectrumAnalyzerError::InfinityValuesNotSupported);
}
if !is_power_of_two(samples.len()) {
if !samples.len().is_power_of_two() {
return Err(SpectrumAnalyzerError::SamplesLengthNotAPowerOfTwo);
}
let max_detectable_frequency = sampling_rate as f32 / 2.0;
Expand Down Expand Up @@ -366,25 +366,3 @@ fn complex_to_magnitude(val: &Complex32) -> f32 {
debug_assert!(!sqrt.is_nan(), "sqrt is NaN!");
sqrt
}

// idea from https://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2
const fn is_power_of_two(num: usize) -> bool {
num != 0 && ((num & (num - 1)) == 0)
}

// tests module for small unit tests

#[cfg(test)]
mod tests2 {
use super::*;

#[test]
fn test_is_power_of_two() {
assert!(!is_power_of_two(0));
assert!(is_power_of_two(1));
assert!(is_power_of_two(2));
assert!(!is_power_of_two(3));
assert!(is_power_of_two(2));
assert!(is_power_of_two(256));
}
}

0 comments on commit a1a3b45

Please sign in to comment.