/*
MIT License
Copyright (c) 2021 Philipp Schuster
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
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.
use Box;
/// Helper struct for [`SpectrumScalingFunction`], that gets passed into the
/// function together with the actual value. This structure can be used to scale
/// each value. All properties reference the current data of a
/// [`crate::spectrum::FrequencySpectrum`].
///
/// This uses `f32` in favor of [`crate::FrequencyValue`] because the latter led to
/// some implementation problems.
/// 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.
///
/// 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.
///
/// 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
/// some implementation problems.
pub type SpectrumScalingFunction = dyn Fn ;
/// 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:
/// <https://www.sjsu.edu/people/burford.furman/docs/me120/FFT_tutorial_NI.pdf>
///
/// ## Usage
/// ```rust
///use spectrum_analyzer::{samples_fft_to_spectrum, scaling, FrequencyLimit};
///let window = [0.0, 0.1, 0.2, 0.3]; // add real data here
///let spectrum = samples_fft_to_spectrum(
/// &window,
/// 44100,
/// FrequencyLimit::All,
/// Some(&scaling::scale_20_times_log10),
/// );
/// ```
/// Function is of type [`SpectrumScalingFunction`].
/// Scales each frequency value/amplitude in the spectrum to interval `[0.0; 1.0]`.
/// Function is of type [`SpectrumScalingFunction`]. Expects that [`SpectrumDataStats::min`] is
/// not negative.
/// Divides each value by N. Several resources recommend that the FFT result should be divided
/// by the length of samples, so that values of different samples lengths are comparable.
/// 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>
/// Combines several scaling functions into a new single one.
///
/// Currently there is the limitation that the functions need to have
/// a `'static` lifetime. This will be fixed if someone needs this.
///
/// # Example
/// ```ignored
/// let fncs = combined(&[÷_by_N, &scale_20_times_log10]);
/// ```