Skip to main content

spectral_flatness_db

Function spectral_flatness_db 

Source
pub fn spectral_flatness_db(
    spectrum: &[f32],
    as_db: bool,
) -> Result<f32, AudioError>
Expand description

Spectral flatness of a one-sided spectrum spectrum.

Flatness is the ratio of the geometric mean to the arithmetic mean of the bin values:

flatness = exp(mean(ln x_k)) / mean(x_k)

It lies in [0, 1]: a perfectly flat (white) spectrum gives 1.0 (0 dB), while a single-tone spectrum with most bins near zero gives a value near 0 (a large negative dB). When as_db is true the result is returned as 10·log10(flatness) (floored so a degenerate ratio maps to a large negative dB rather than -inf).

Any non-positive bin drives the geometric mean to zero, so the linear flatness is 0.0 (or the dB floor) whenever a bin is <= 0.

Zero-heap: single pass, scalar result.

§Errors