Skip to content

Commit

Permalink
clippy: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
phip1611 committed Dec 16, 2024
1 parent b9c4b9f commit 86e7f15
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
17 changes: 8 additions & 9 deletions src/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ impl Display for OrderableF32 {
impl Ord for OrderableF32 {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
if self.val() < other.val() {
Ordering::Less
} else if self.val() == other.val() {
Ordering::Equal
} else {
Ordering::Greater
}
}
}

Expand All @@ -81,14 +87,7 @@ impl PartialOrd for OrderableF32 {
#[allow(clippy::float_cmp)]
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
// self.cmp(other).is_eq()
Some(if self.val() < other.val() {
Ordering::Less
} else if self.val() == other.val() {
Ordering::Equal
} else {
Ordering::Greater
})
Some(self.cmp(other))
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ SOFTWARE.
*/
//! Module for the struct [`FrequencyLimit`].
/// Can be used to specify a desired frequency limit. If you know that you only
/// need frequencies `f <= 1000Hz`, `1000 <= f <= 6777`, or `10000 <= f`, then this
/// can help you to accelerate overall computation speed and memory usage.
/// Can be used to specify a desired frequency limit.
///
/// If you know that you only need frequencies `f <= 1000Hz`,
/// `1000 <= f <= 6777`, or `10000 <= f`, then this can help you to accelerate
/// overall computation speed and memory usage.
///
/// Please note that due to frequency inaccuracies the FFT result may not contain
/// a value for `1000Hz` but for `998.76Hz`!
Expand Down
64 changes: 42 additions & 22 deletions src/scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//! This module contains convenient public transform functions that you can use
//! as parameters in [`crate::samples_fft_to_spectrum`] for scaling the
//! frequency value (the FFT result). They act as "idea/inspiration". Feel free
//! to either compose them or create your own derivation from them.
//! as parameters in [`samples_fft_to_spectrum`] for scaling the frequency value
//! (the FFT result).
//!
//! They act as "idea/inspiration". Feel free to either compose them or create
//! your own derivation from them.
//!
/// [`samples_fft_to_spectrum`]: crate::samples_fft_to_spectrum
use alloc::boxed::Box;

/// Helper struct for [`SpectrumScalingFunction`] that is passed into the
/// scaling function together with the current frequency value. This structure
/// can be used to scale each value. All properties reference the current data
/// of a [`crate::spectrum::FrequencySpectrum`].
/// scaling function together with the current frequency value.
///
/// This uses `f32` in favor of [`crate::FrequencyValue`] because the latter led to
/// This structure can be used to scale each value. All properties reference the
/// current data of a [`FrequencySpectrum`].
///
/// This uses `f32` in favor of [`FrequencyValue`] because the latter led to
/// some implementation problems.
///
/// [`FrequencySpectrum`]: crate::FrequencySpectrum
/// [`FrequencyValue`]: crate::FrequencyValue
#[derive(Debug)]
pub struct SpectrumDataStats {
/// Minimal frequency value in spectrum.
Expand All @@ -50,25 +58,35 @@ pub struct SpectrumDataStats {
pub n: f32,
}

/// Describes the type for a function that scales/normalizes the data inside [`crate::FrequencySpectrum`].
/// The scaling only affects the value/amplitude of the frequency, but not the frequency itself.
/// It is applied to every single element.
/// Describes the type for a function that scales/normalizes the data inside
/// [`FrequencySpectrum`].
///
/// The scaling only affects the value/amplitude of the frequency, but not the
/// frequency itself. It is applied to every single element.
///
/// A scaling function can be used for example to subtract the minimum (`min`)
/// from each value. It is optional to use the second parameter
/// [`SpectrumDataStats`].
///
/// A scaling function can be used for example to subtract the minimum (`min`) from each value.
/// It is optional to use the second parameter [`SpectrumDataStats`].
/// and the type works with static functions as well as dynamically created closures.
/// The type works with static functions as well as dynamically created
/// closures.
///
/// You must take care of, that you don't have division by zero in your function or
/// that the result is NaN or Infinity (regarding IEEE-754). If the result is NaN or Infinity,
/// the library will return `Err`.
/// You must take care of, that you don't have division by zero in your function
/// or that the result is NaN or Infinity (regarding IEEE-754). If the result
/// is NaN or Infinity, the library will return `Err`.
///
/// This uses `f32` in favor of [`crate::FrequencyValue`] because the latter led to
/// This uses `f32` in favor of [`FrequencyValue`] because the latter led to
/// some implementation problems.
///
/// [`FrequencySpectrum`]: crate::FrequencySpectrum
/// [`FrequencyValue`]: crate::FrequencyValue
pub type SpectrumScalingFunction = dyn Fn(f32, &SpectrumDataStats) -> f32;

/// Calculates the base 10 logarithm of each frequency magnitude and
/// multiplies it with 20. This scaling is quite common, you can
/// find more information for example here:
/// multiplies it with 20.
///
/// This scaling is quite common, you can find more information for example
/// here:
/// <https://www.sjsu.edu/people/burford.furman/docs/me120/FFT_tutorial_NI.pdf>
///
/// ## Usage
Expand Down Expand Up @@ -125,8 +143,10 @@ pub fn divide_by_N(fr_val: f32, stats: &SpectrumDataStats) -> f32 {
}
}

/// Like [`divide_by_N`] but divides each value by `sqrt(N)`. This is the recommended scaling
/// in the `rustfft` documentation (but is generally applicable).
/// Like [`divide_by_N`] but divides each value by `sqrt(N)`.
///
/// This is the recommended scaling in the `rustfft` documentation (but is
/// generally applicable).
/// See <https://docs.rs/rustfft/latest/rustfft/#normalization>
#[allow(non_snake_case)]
#[must_use]
Expand Down Expand Up @@ -183,7 +203,7 @@ mod tests {
.into_iter()
.map(|x| scaling_fn(x, &stats))
.collect::<Vec<_>>();
let expected = vec![0.0_f32, 0.2, 0.4, 0.6, 0.8, 1.0];
let expected = [0.0_f32, 0.2, 0.4, 0.6, 0.8, 1.0];
for (expected_val, actual_val) in expected.iter().zip(scaled_data.iter()) {
float_cmp::approx_eq!(f32, *expected_val, *actual_val, ulps = 3);
}
Expand Down
5 changes: 3 additions & 2 deletions src/spectrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ use alloc::collections::BTreeMap;
use alloc::vec::Vec;

/// Convenient wrapper around the processed FFT result which describes each
/// frequency and its value/amplitude from the analyzed samples. It only
/// contains the frequencies that were desired, e.g., specified via
/// frequency and its value/amplitude from the analyzed samples.
///
/// It only contains the frequencies that were desired, e.g., specified via
/// [`crate::limit::FrequencyLimit`] when [`crate::samples_fft_to_spectrum`]
/// was called.
///
Expand Down

0 comments on commit 86e7f15

Please sign in to comment.