kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
Log | Files | Refs | README

commit 8a3ee958ef5b428eafc71bd968478bd7e536f6e9
parent ba30478efe8824e9d77b85c33e07cb7f5daf79c6
Author: [email protected] <[email protected]>
Date:   Mon, 19 Aug 2019 12:28:39 +0000

Fix NaNs in amp_to_dB

Diffstat:
Minclude/kfr/dsp/units.hpp | 10++++++++--
Minclude/kfr/math/impl/select.hpp | 5+++++
2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/include/kfr/dsp/units.hpp b/include/kfr/dsp/units.hpp @@ -39,10 +39,16 @@ using sample_rate_t = double; namespace intrinsics { +template <typename T1, typename T2> +KFR_INTRINSIC common_type<T1, T2> fix_nans(const T1& val, const T2& replacement) +{ + return select(val != val, replacement, val); +} + template <typename T, typename TF = flt_type<T>> KFR_INTRINSIC TF amp_to_dB(const T& amp) { - return log(static_cast<TF>(abs(amp))) * subtype<TF>(8.6858896380650365530225783783322); + return fix_nans(log(static_cast<TF>(abs(amp))) * subtype<TF>(8.6858896380650365530225783783322), -c_infinity<T>); // return T( 20.0 ) * log10( level ); } @@ -56,7 +62,7 @@ KFR_INTRINSIC TF dB_to_amp(const T& dB) template <typename T, typename TF = flt_type<T>> KFR_INTRINSIC TF amp_to_dB(const T& amp, const T& offset) { - return log_fmadd(static_cast<TF>(abs(amp)), subtype<TF>(8.6858896380650365530225783783322), offset); + return fix_nans(log_fmadd(static_cast<TF>(abs(amp)), subtype<TF>(8.6858896380650365530225783783322), offset), -c_infinity<T>); // return T( 20.0 ) * log10( level ); } diff --git a/include/kfr/math/impl/select.hpp b/include/kfr/math/impl/select.hpp @@ -319,6 +319,11 @@ KFR_INTRINSIC vec<T, N> select(const vec<bit<T>, N>& m, const T& x, const vec<T, return select(m, vec<T, N>(x), y); } #endif +template <typename T1, typename T2> +KFR_INTRINSIC common_type<T1, T2> select(bool m, const T1& x, const T2& y) +{ + return m ? x : y; +} } // namespace intrinsics KFR_I_FN(select)