From 175030b77c169032ab1a82483632de93025f7d1e Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 6 Mar 2022 18:54:18 +0100 Subject: [PATCH] Fix STFT example filter normalization --- README.md | 6 +++--- plugins/examples/stft/src/lib.rs | 14 +++++--------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fec9e606..5855310a 100644 --- a/README.md +++ b/README.md @@ -78,9 +78,9 @@ examples. frequency smoothing that can also make use of MIDI input instead of generating a static signal based on the plugin's parameters. - [**stft**](plugins/examples/stft) shows off some of NIH-plug's other optional - helper features, like an adapter to process audio in buffered blocks meant for - short-term Fourier transform operations, all using the compositional `Buffer` - interface. + higher level helper features, such as an adapter to process audio with a + short-term Fourier transform using the overlap-add method, all using the + compositional `Buffer` interfaces. ## Licensing diff --git a/plugins/examples/stft/src/lib.rs b/plugins/examples/stft/src/lib.rs index 5bd35712..d2bdbab2 100644 --- a/plugins/examples/stft/src/lib.rs +++ b/plugins/examples/stft/src/lib.rs @@ -49,15 +49,11 @@ 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); - // Our STFT functions will have a window function applied, so we need to do the same thing - // with this filter - let window_function = util::window::hann(WINDOW_SIZE); - util::window::multiply_with_window(&mut real_fft_scratch_buffer, &window_function); - - // And make sure to normalize this so convolution sums to 1 - let filter_sum_recip = real_fft_scratch_buffer.iter().sum::().recip(); + // And make sure to normalize this so the levels doesn't change much after convolving + let filter_normalization_factor = + real_fft_scratch_buffer.iter().sum::().recip() * 2.0f32.sqrt() * 2.0; for sample in real_fft_scratch_buffer.as_slice_mut() { - *sample *= filter_sum_recip; + *sample *= filter_normalization_factor; } r2c_plan @@ -71,7 +67,7 @@ impl Default for Stft { params: Box::pin(StftParams::default()), stft: util::StftHelper::new(2, WINDOW_SIZE), - window_function, + window_function: util::window::hann(WINDOW_SIZE), lp_filter_kernel: complex_fft_scratch_buffer .iter()