1
0
Fork 0

Fix gain compensation in STFT example

This commit is contained in:
Robbert van der Helm 2022-03-07 02:02:46 +01:00
parent f05d209169
commit 376c1d7b0a

View file

@ -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::<f32>().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::<f32>().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,