Line data Source code
1 : /** 2 : * \file HalfTanhShaperFilter.cpp 3 : */ 4 : 5 : #include "HalfTanhShaperFilter.h" 6 : #include <ATK/Utility/fmath.h> 7 : 8 : #include <cassert> 9 : #include <cmath> 10 : 11 : namespace ATK 12 : { 13 : template<typename DataType_> 14 4 : HalfTanhShaperFilter<DataType_>::HalfTanhShaperFilter(gsl::index nb_channels) 15 4 : :Parent(nb_channels, nb_channels) 16 : { 17 4 : } 18 : 19 : template<typename DataType_> 20 3 : void HalfTanhShaperFilter<DataType_>::set_coefficient(DataType coeff) 21 : { 22 3 : if(coeff <= 0) 23 : { 24 1 : throw std::out_of_range("Coefficient must be strictly positive."); 25 : } 26 2 : this->coeff = coeff; 27 2 : } 28 : 29 : template<typename DataType_> 30 1 : DataType_ HalfTanhShaperFilter<DataType_>::get_coefficient() const 31 : { 32 1 : return coeff; 33 : } 34 : 35 : template<typename DataType_> 36 2 : void HalfTanhShaperFilter<DataType_>::process_impl(gsl::index size) const 37 : { 38 4 : for(gsl::index channel = 0; channel < nb_input_ports; ++channel) 39 : { 40 2 : const DataType* ATK_RESTRICT input = converted_inputs[channel]; 41 2 : DataType* ATK_RESTRICT output = outputs[channel]; 42 2002 : for(gsl::index i = 0; i < size; ++i) 43 : { 44 2000 : if(input[i] < 0) 45 : { 46 984 : auto exp = fmath::exp(2 * coeff * input[i]); 47 984 : output[i] = (exp - 1) / (coeff * (exp + 1)); 48 : } 49 : else 50 : { 51 1016 : output[i] = input[i]; 52 : } 53 : } 54 : } 55 2 : } 56 : 57 : #if ATK_ENABLE_INSTANTIATION 58 : template class HalfTanhShaperFilter<float>; 59 : #endif 60 : template class HalfTanhShaperFilter<double>; 61 : }