From 376c1d7b0ae88b559373b2b37faa31f38c5a5fc3 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Mon, 7 Mar 2022 02:02:46 +0100 Subject: [PATCH] Fix gain compensation in STFT example --- plugins/examples/stft/src/lib.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/plugins/examples/stft/src/lib.rs b/plugins/examples/stft/src/lib.rs index 27575a86..2f847292 100644 --- a/plugins/examples/stft/src/lib.rs +++ b/plugins/examples/stft/src/lib.rs @@ -50,11 +50,8 @@ impl Default for Stft { let filter_window = util::window::hann(FILTER_WINDOW_SIZE); real_fft_scratch_buffer[0..FILTER_WINDOW_SIZE].copy_from_slice(&filter_window); - // And make sure to normalize this so convolution sums to `e` which together with the - // compensation for the windowing causes everything to stay about at unit level. Don't ask - // my why this number. - let filter_normalization_factor = - real_fft_scratch_buffer.iter().sum::().recip() * f32::consts::E; + // And make sure to normalize this so convolution sums to 1 + let filter_normalization_factor = real_fft_scratch_buffer.iter().sum::().recip(); for sample in real_fft_scratch_buffer.as_slice_mut() { *sample *= filter_normalization_factor; } @@ -134,7 +131,7 @@ impl Plugin for Stft { ) -> ProcessStatus { // Compensate for the window function, the overlap, and the extra gain introduced by the // IDFT operation - const GAIN_COMPENSATION: f32 = 1.0 / OVERLAP_TIMES as f32 / WINDOW_SIZE as f32; + const GAIN_COMPENSATION: f32 = f32::consts::E / OVERLAP_TIMES as f32 / WINDOW_SIZE as f32; self.stft.process_overlap_add( buffer,