From ee94fba57b0c56ebec6e0f650c676b89b042869b Mon Sep 17 00:00:00 2001 From: Stefano D'Angelo Date: Mon, 14 Aug 2023 12:11:11 +0200 Subject: [PATCH] now using backward Euler method in bw_one_pole --- TODO | 1 - include/bw_one_pole.h | 10 ++++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 05b34c4..3b78934 100644 --- a/TODO +++ b/TODO @@ -6,7 +6,6 @@ code: * check all examples again * osc post filter (and one pole init, slew rate, etc.) val from input? set state instead? * audio rate optional pulse width/slope inputs? -* should rather use backward Euler in bw_onepole? * one pole process const input? (return also if const out) * empty functions etc. to keep consistency and forward compatibility? * float in [-1,1] for velocity, pitch bend, mod wheel diff --git a/include/bw_one_pole.h b/include/bw_one_pole.h index 7d0b820..0447693 100644 --- a/include/bw_one_pole.h +++ b/include/bw_one_pole.h @@ -40,6 +40,8 @@ *
  • Added overladed C++ process() function taking * C-style arrays as arguments.
  • *
  • Removed usage of reserved identifiers.
  • + *
  • Now using backward Euler rather than impulse invariant + * method.
  • * * *
  • Version 0.6.0: @@ -364,7 +366,7 @@ struct bw_one_pole_coeffs { #endif // Coefficients - float Ttm2pi; + float fs_2pi; float mA1u; float mA1d; @@ -412,7 +414,7 @@ static inline void bw_one_pole_set_sample_rate(bw_one_pole_coeffs *BW_RESTRICT c BW_ASSERT_DEEP(bw_one_pole_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_one_pole_coeffs_state_init); - coeffs->Ttm2pi = -6.283185307179586f / sample_rate; + coeffs->fs_2pi = 0.15915494309189535f * sample_rate; #ifdef BW_DEBUG_DEEP coeffs->state = bw_one_pole_coeffs_state_set_sample_rate; @@ -424,10 +426,10 @@ static inline void bw_one_pole_set_sample_rate(bw_one_pole_coeffs *BW_RESTRICT c static inline void bw_one_pole_do_update_coeffs_ctrl(bw_one_pole_coeffs *BW_RESTRICT coeffs) { if (coeffs->param_changed) { if (coeffs->param_changed & BW_ONE_POLE_PARAM_CUTOFF_UP) - coeffs->mA1u = coeffs->cutoff_up > 1.591549430918953e8f ? 0.f : bw_expf(coeffs->Ttm2pi * coeffs->cutoff_up); + coeffs->mA1u = coeffs->cutoff_up > 1.591549430918953e8f ? 0.f : coeffs->fs_2pi * bw_rcpf(coeffs->fs_2pi + coeffs->cutoff_up); // tau < 1 ns is instantaneous for any practical purpose if (coeffs->param_changed & BW_ONE_POLE_PARAM_CUTOFF_DOWN) - coeffs->mA1d = coeffs->cutoff_down > 1.591549430918953e8f ? 0.f : bw_expf(coeffs->Ttm2pi * coeffs->cutoff_down); + coeffs->mA1d = coeffs->cutoff_down > 1.591549430918953e8f ? 0.f : coeffs->fs_2pi * bw_rcpf(coeffs->fs_2pi + coeffs->cutoff_down); // as before if (coeffs->param_changed & BW_ONE_POLE_PARAM_STICKY_THRESH) coeffs->st2 = coeffs->sticky_thresh * coeffs->sticky_thresh;