From 93ab08e5d5558dd4a2e26aa0a16e5f90e31d3177 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 6 Mar 2022 19:07:46 +0100 Subject: [PATCH] Remove post-IDFT windowing in STFT helper There's no reason to. --- plugins/examples/stft/src/lib.rs | 9 +++++---- src/util/stft.rs | 1 - 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/examples/stft/src/lib.rs b/plugins/examples/stft/src/lib.rs index d2bdbab2..f115d0b6 100644 --- a/plugins/examples/stft/src/lib.rs +++ b/plugins/examples/stft/src/lib.rs @@ -49,9 +49,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 the levels doesn't change much after convolving - let filter_normalization_factor = - real_fft_scratch_buffer.iter().sum::().recip() * 2.0f32.sqrt() * 2.0; + // 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; } @@ -129,7 +128,9 @@ impl Plugin for Stft { buffer: &mut Buffer, _context: &mut impl ProcessContext, ) -> ProcessStatus { - const GAIN_COMPENSATION: f32 = 1.0 / OVERLAP_TIMES as f32 / WINDOW_SIZE as f32; + // Compensate for the window function, the overlap, and the extra gain introduced by the + // IDFT operation + const GAIN_COMPENSATION: f32 = 2.0 / OVERLAP_TIMES as f32 / WINDOW_SIZE as f32; self.stft.process_overlap_add( buffer, diff --git a/src/util/stft.rs b/src/util/stft.rs index 3662a9f4..42d8e264 100644 --- a/src/util/stft.rs +++ b/src/util/stft.rs @@ -248,7 +248,6 @@ impl StftHelper { process_cb(channel_idx, None, &mut self.scratch_buffer); // The actual overlap-add part of the equation - multiply_with_window(&mut self.scratch_buffer, window_function); add_scratch_to_ring_buffer( &self.scratch_buffer, self.current_pos,