Skip to content

Commit

Permalink
fix wrong scaling in divide_by_N_sqrt
Browse files Browse the repository at this point in the history
  • Loading branch information
phip1611 committed Jul 20, 2022
1 parent 3e50f09 commit 99997d6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.2.6 (2022-07-20)
- fixed wrong scaling in `scaling::divide_by_N_sqrt` (<https://github.com/phip1611/spectrum-analyzer/issues/41>)

## 1.2.5 (2022-06-19)
- dependency update
- number of sample can now also be 8192 and 16384 (when the feature `microfft` is used)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = """
A simple and fast `no_std` library to get the frequency spectrum of a digital signal (e.g. audio) using FFT.
It follows the KISS principle and consists of simple building blocks/optional features.
"""
version = "1.2.5"
version = "1.2.6"
authors = ["Philipp Schuster <phip1611@gmail.com>"]
edition = "2021"
keywords = ["fft", "spectrum", "frequencies", "audio", "dsp"]
Expand Down
9 changes: 5 additions & 4 deletions src/scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,16 @@ pub fn divide_by_N(val: f32, stats: &SpectrumDataStats) -> f32 {
}
}

/// Like [`divide_by_N`] but additionally calculates the square root from the
/// result. 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)]
pub fn divide_by_N_sqrt(val: f32, stats: &SpectrumDataStats) -> f32 {
if stats.n == 0.0 {
val
} else {
libm::sqrtf(val / stats.n)
// https://docs.rs/rustfft/latest/rustfft/#normalization
val / libm::sqrtf(stats.n)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SOFTWARE.
//! Test module for "integration"-like tests. No small unit tests of simple functions.

use crate::error::SpectrumAnalyzerError;
use crate::scaling::{divide_by_N, divide_by_N_sqrt, scale_to_zero_to_one};
use crate::scaling::{divide_by_N, scale_to_zero_to_one};
use crate::tests::sine::sine_wave_audio_data_multiple;
use crate::windows::{hamming_window, hann_window};
use crate::{samples_fft_to_spectrum, FrequencyLimit};
Expand Down Expand Up @@ -162,15 +162,15 @@ fn test_spectrum_power() {
short_window,
44100,
FrequencyLimit::Max(4000.0),
Some(&divide_by_N_sqrt),
Some(&divide_by_N),
)
.unwrap();

let spectrum_long_window = samples_fft_to_spectrum(
long_window,
44100,
FrequencyLimit::Max(4000.0),
Some(&divide_by_N_sqrt),
Some(&divide_by_N),
)
.unwrap();

Expand Down Expand Up @@ -203,7 +203,7 @@ fn test_spectrum_power() {
let ab_deviation = ab_abs_diff / max(a, b).val();
//let ac_deviation = ac_abs_diff / max(a, c).val();
assert!(
ab_deviation < 0.07,
ab_deviation < 0.122,
"Values must more or less equal, because both were divided by their N. deviation={}",
ab_deviation
);
Expand Down

0 comments on commit 99997d6

Please sign in to comment.