LCOV - code coverage report
Current view: top level - EQ - SecondOrderSVFFilter.cpp (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 162 162 100.0 %
Date: 2021-02-18 20:07:22 Functions: 51 51 100.0 %

          Line data    Source code
       1             : /**
       2             :  * \file SecondOrderSVFFilter.cpp
       3             :  */
       4             : 
       5             : #include "SecondOrderSVFFilter.h"
       6             : 
       7             : #include <boost/math/constants/constants.hpp>
       8             : 
       9             : #include <cassert>
      10             : 
      11             : namespace ATK
      12             : {
      13             :   template<typename SVFCoefficients>
      14             :   class SecondOrderSVFFilter<SVFCoefficients>::SVFState
      15             :   {
      16             :   public:
      17             :     typename SVFCoefficients::DataType iceq1{0};
      18             :     typename SVFCoefficients::DataType iceq2{0};
      19             :   };
      20             :   
      21             :   template<typename SVFCoefficients>
      22          36 :   SecondOrderSVFFilter<SVFCoefficients>::SecondOrderSVFFilter(gsl::index nb_channels)
      23          36 :   :SVFCoefficients(nb_channels), state(std::make_unique<SVFState[]>(nb_channels))
      24             :   {
      25          36 :   }
      26             : 
      27             :   template <typename SVFCoefficients>
      28             :   SecondOrderSVFFilter<SVFCoefficients>::~SecondOrderSVFFilter() = default;
      29             : 
      30             :   template<typename SVFCoefficients>
      31          56 :   void SecondOrderSVFFilter<SVFCoefficients>::full_setup()
      32             :   {
      33          56 :     state = std::make_unique<SVFState[]>(nb_input_ports);
      34          56 :   }
      35             : 
      36             :   template<typename DataType>
      37          56 :   void SecondOrderSVFFilter<DataType>::process_impl(gsl::index size) const
      38             :   {
      39          56 :     assert(nb_input_ports == nb_output_ports);
      40             :     
      41         112 :     for(gsl::index j = 0; j < nb_input_ports; ++j)
      42             :     {
      43          56 :       const DataType* ATK_RESTRICT input = converted_inputs[j];
      44          56 :       DataType* ATK_RESTRICT output = outputs[j];
      45             :       
      46     3670072 :       for(gsl::index i = 0; i < size; ++i)
      47             :       {
      48     3670016 :         DataType v3 = input[i] - state[j].iceq2;
      49     3670016 :         DataType v1 = a1 * state[j].iceq1 + a2 * v3;
      50     3670016 :         DataType v2 = state[j].iceq2 + a2 * state[j].iceq1 + a3 * v3;
      51     3670016 :         state[j].iceq1 = CoeffDataType(2) * v1 - state[j].iceq1;
      52     3670016 :         state[j].iceq2 = CoeffDataType(2) * v2 - state[j].iceq2;
      53             :         
      54     3670016 :         output[i] = m0 * input[i] + m1 * v1 + m2 * v2;
      55             :       }
      56             :     }
      57          56 :   }
      58             :   
      59             :   template<typename DataType>
      60          36 :   SecondOrderSVFBaseCoefficients<DataType>::SecondOrderSVFBaseCoefficients(gsl::index nb_channels)
      61          36 :   :TypedBaseFilter<DataType>(nb_channels, nb_channels)
      62             :   {
      63          36 :   }
      64             : 
      65             :   template<typename DataType_>
      66          30 :   void SecondOrderSVFBaseCoefficients<DataType_>::set_cut_frequency(CoeffDataType cut_frequency)
      67             :   {
      68          30 :     if(cut_frequency <= 0)
      69             :     {
      70           1 :       throw std::out_of_range("Frequencies must be positive");
      71             :     }
      72          29 :     this->cut_frequency = cut_frequency;
      73          29 :     setup();
      74          29 :   }
      75             : 
      76             :   template<typename DataType>
      77           1 :   typename SecondOrderSVFBaseCoefficients<DataType>::CoeffDataType SecondOrderSVFBaseCoefficients<DataType>::get_cut_frequency() const
      78             :   {
      79           1 :     return cut_frequency;
      80             :   }
      81             : 
      82             :   template<typename DataType_>
      83           8 :   void SecondOrderSVFBaseCoefficients<DataType_>::set_Q(CoeffDataType Q)
      84             :   {
      85           8 :     if(Q <= 0)
      86             :     {
      87           1 :       throw std::out_of_range("Q must be positive");
      88             :     }
      89           7 :     this->Q = Q;
      90           7 :     setup();
      91           7 :   }
      92             : 
      93             :   template<typename DataType>
      94           1 :   typename SecondOrderSVFBaseCoefficients<DataType>::CoeffDataType SecondOrderSVFBaseCoefficients<DataType>::get_Q() const
      95             :   {
      96           1 :     return Q;
      97             :   }
      98             : 
      99             :   template<typename DataType_>
     100           8 :   SecondOrderSVFLowPassCoefficients<DataType_>::SecondOrderSVFLowPassCoefficients(gsl::index nb_channels)
     101           8 :   :Parent(nb_channels)
     102             :   {
     103           8 :   }
     104             : 
     105             :   template<typename DataType>
     106           6 :   void SecondOrderSVFLowPassCoefficients<DataType>::setup()
     107             :   {
     108           6 :     auto g = std::tan(boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate);
     109           6 :     auto k = 1/Q;
     110           6 :     a1 = 1 / (1 + g * (g + k));
     111           6 :     a2 = g * a1;
     112           6 :     a3 = g * a2;
     113           6 :     m0 = 0;
     114           6 :     m1 = 0;
     115           6 :     m2 = 1;
     116           6 :   }
     117             : 
     118             :   template<typename DataType_>
     119           3 :   SecondOrderSVFBandPassCoefficients<DataType_>::SecondOrderSVFBandPassCoefficients(gsl::index nb_channels)
     120           3 :   :Parent(nb_channels)
     121             :   {
     122           3 :   }
     123             : 
     124             :   template<typename DataType>
     125           6 :   void SecondOrderSVFBandPassCoefficients<DataType>::setup()
     126             :   {
     127           6 :     auto g = std::tan(boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate);
     128           6 :     auto k = 1 / Q;
     129           6 :     a1 = 1 / (1 + g * (g + k));
     130           6 :     a2 = g * a1;
     131           6 :     a3 = g * a2;
     132           6 :     m0 = 0;
     133           6 :     m1 = 1;
     134           6 :     m2 = 0;
     135           6 :   }
     136             : 
     137             :   template<typename DataType_>
     138           4 :   SecondOrderSVFHighPassCoefficients<DataType_>::SecondOrderSVFHighPassCoefficients(gsl::index nb_channels)
     139           4 :   :Parent(nb_channels)
     140             :   {
     141           4 :   }
     142             : 
     143             :   template<typename DataType>
     144           4 :   void SecondOrderSVFHighPassCoefficients<DataType>::setup()
     145             :   {
     146           4 :     auto g = std::tan(boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate);
     147           4 :     auto k = 1 / Q;
     148           4 :     a1 = 1 / (1 + g * (g + k));
     149           4 :     a2 = g * a1;
     150           4 :     a3 = g * a2;
     151           4 :     m0 = 1;
     152           4 :     m1 = -k;
     153           4 :     m2 = -1;
     154           4 :   }
     155             : 
     156             :   template<typename DataType_>
     157           4 :   SecondOrderSVFNotchCoefficients<DataType_>::SecondOrderSVFNotchCoefficients(gsl::index nb_channels)
     158           4 :   :Parent(nb_channels)
     159             :   {
     160           4 :   }
     161             : 
     162             :   template<typename DataType>
     163           4 :   void SecondOrderSVFNotchCoefficients<DataType>::setup()
     164             :   {
     165           4 :     auto g = std::tan(boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate);
     166           4 :     auto k = 1 / Q;
     167           4 :     a1 = 1 / (1 + g * (g + k));
     168           4 :     a2 = g * a1;
     169           4 :     a3 = g * a2;
     170           4 :     m0 = 1;
     171           4 :     m1 = -k;
     172           4 :     m2 = 0;
     173           4 :   }
     174             : 
     175             :   template<typename DataType_>
     176           4 :   SecondOrderSVFPeakCoefficients<DataType_>::SecondOrderSVFPeakCoefficients(gsl::index nb_channels)
     177           4 :   :Parent(nb_channels)
     178             :   {
     179           4 :   }
     180             : 
     181             :   template<typename DataType>
     182           4 :   void SecondOrderSVFPeakCoefficients<DataType>::setup()
     183             :   {
     184           4 :     auto g = std::tan(boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate);
     185           4 :     auto k = 1 / Q;
     186           4 :     a1 = 1 / (1 + g * (g + k));
     187           4 :     a2 = g * a1;
     188           4 :     a3 = g * a2;
     189           4 :     m0 = 1;
     190           4 :     m1 = -k;
     191           4 :     m2 = 2;
     192           4 :   }
     193             : 
     194             :   template<typename DataType_>
     195           5 :   SecondOrderSVFBellCoefficients<DataType_>::SecondOrderSVFBellCoefficients(gsl::index nb_channels)
     196           5 :   :Parent(nb_channels)
     197             :   {
     198             :     
     199           5 :   }
     200             :   
     201             :   template<typename DataType_>
     202           5 :   void SecondOrderSVFBellCoefficients<DataType_>::set_gain(CoeffDataType gain)
     203             :   {
     204           5 :     if(gain <= 0)
     205             :     {
     206           1 :       throw std::out_of_range("Gain must be positive");
     207             :     }
     208           4 :     this->gain = gain;
     209           4 :     setup();
     210           4 :   }
     211             : 
     212             :   template<typename DataType>
     213           1 :   typename SecondOrderSVFBellCoefficients<DataType>::CoeffDataType SecondOrderSVFBellCoefficients<DataType>::get_gain() const
     214             :   {
     215           1 :     return gain;
     216             :   }
     217             : 
     218             :   template<typename DataType>
     219          10 :   void SecondOrderSVFBellCoefficients<DataType>::setup()
     220             :   {
     221          10 :     auto g = std::tan(boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate);
     222          10 :     auto k = 1 / (Q * gain);
     223          10 :     a1 = 1 / (1 + g * (g + k));
     224          10 :     a2 = g * a1;
     225          10 :     a3 = g * a2;
     226          10 :     m0 = 1;
     227          10 :     m1 = k * (gain * gain - 1);
     228          10 :     m2 = 0;
     229          10 :   }
     230             : 
     231             :   template<typename DataType_>
     232           4 :   SecondOrderSVFLowShelfCoefficients<DataType_>::SecondOrderSVFLowShelfCoefficients(gsl::index nb_channels)
     233           4 :   :Parent(nb_channels)
     234             :   {
     235           4 :   }
     236             : 
     237             :   template<typename DataType_>
     238           4 :   void SecondOrderSVFLowShelfCoefficients<DataType_>::set_gain(CoeffDataType gain)
     239             :   {
     240           4 :     this->gain = gain;
     241           4 :     setup();
     242           4 :   }
     243             : 
     244             :   template<typename DataType>
     245           1 :   typename SecondOrderSVFLowShelfCoefficients<DataType>::CoeffDataType SecondOrderSVFLowShelfCoefficients<DataType>::get_gain() const
     246             :   {
     247           1 :     return gain;
     248             :   }
     249             : 
     250             :   template<typename DataType>
     251           7 :   void SecondOrderSVFLowShelfCoefficients<DataType>::setup()
     252             :   {
     253           7 :     auto g = std::tan(boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate);
     254           7 :     auto k = 1 / Q;
     255           7 :     a1 = 1 / (1 + g * (g + k));
     256           7 :     a2 = g * a1;
     257           7 :     a3 = g * a2;
     258           7 :     m0 = 1;
     259           7 :     m1 = k * (gain - 1);
     260           7 :     m2 = gain * gain - 1;
     261           7 :   }
     262             : 
     263             :   template<typename DataType_>
     264           4 :   SecondOrderSVFHighShelfCoefficients<DataType_>::SecondOrderSVFHighShelfCoefficients(gsl::index nb_channels)
     265           4 :   :Parent(nb_channels)
     266             :   {
     267           4 :   }
     268             : 
     269             :   template<typename DataType_>
     270           4 :   void SecondOrderSVFHighShelfCoefficients<DataType_>::set_gain(CoeffDataType gain)
     271             :   {
     272           4 :     this->gain = gain;
     273           4 :     setup();
     274           4 :   }
     275             : 
     276             :   template<typename DataType>
     277           1 :   typename SecondOrderSVFHighShelfCoefficients<DataType>::CoeffDataType SecondOrderSVFHighShelfCoefficients<DataType>::get_gain() const
     278             :   {
     279           1 :     return gain;
     280             :   }
     281             : 
     282             :   template<typename DataType>
     283           7 :   void SecondOrderSVFHighShelfCoefficients<DataType>::setup()
     284             :   {
     285           7 :     auto g = std::tan(boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate);
     286           7 :     auto k = 1 / (Q * gain);
     287           7 :     a1 = 1 / (1 + g * (g + k));
     288           7 :     a2 = g * a1;
     289           7 :     a3 = g * a2;
     290           7 :     m0 = gain * gain;
     291           7 :     m1 = k * (1 - gain) * gain;
     292           7 :     m2 = 1 - gain * gain;
     293           7 :   }
     294             : 
     295             : #if ATK_ENABLE_INSTANTIATION
     296             :   template class SecondOrderSVFBaseCoefficients<float>;
     297             :   template class SecondOrderSVFBaseCoefficients<std::complex<float> >;
     298             :   template class SecondOrderSVFBaseCoefficients<std::complex<double> >;
     299             : 
     300             :   template class SecondOrderSVFLowPassCoefficients<float>;
     301             :   template class SecondOrderSVFLowPassCoefficients<std::complex<float> >;
     302             :   template class SecondOrderSVFLowPassCoefficients<std::complex<double> >;
     303             :   template class SecondOrderSVFBandPassCoefficients<float>;
     304             :   template class SecondOrderSVFBandPassCoefficients<std::complex<float> >;
     305             :   template class SecondOrderSVFBandPassCoefficients<std::complex<double> >;
     306             :   template class SecondOrderSVFHighPassCoefficients<float>;
     307             :   template class SecondOrderSVFHighPassCoefficients<std::complex<float> >;
     308             :   template class SecondOrderSVFHighPassCoefficients<std::complex<double> >;
     309             :   template class SecondOrderSVFNotchCoefficients<float>;
     310             :   template class SecondOrderSVFNotchCoefficients<std::complex<float> >;
     311             :   template class SecondOrderSVFNotchCoefficients<std::complex<double> >;
     312             :   template class SecondOrderSVFPeakCoefficients<float>;
     313             :   template class SecondOrderSVFPeakCoefficients<std::complex<float> >;
     314             :   template class SecondOrderSVFPeakCoefficients<std::complex<double> >;
     315             :   template class SecondOrderSVFBellCoefficients<float>;
     316             :   template class SecondOrderSVFBellCoefficients<std::complex<float> >;
     317             :   template class SecondOrderSVFBellCoefficients<std::complex<double> >;
     318             :   template class SecondOrderSVFLowShelfCoefficients<float>;
     319             :   template class SecondOrderSVFLowShelfCoefficients<std::complex<float> >;
     320             :   template class SecondOrderSVFLowShelfCoefficients<std::complex<double> >;
     321             :   template class SecondOrderSVFHighShelfCoefficients<float>;
     322             :   template class SecondOrderSVFHighShelfCoefficients<std::complex<float> >;
     323             :   template class SecondOrderSVFHighShelfCoefficients<std::complex<double> >;
     324             : 
     325             :   template class SecondOrderSVFFilter<SecondOrderSVFLowPassCoefficients<float> >;
     326             :   template class SecondOrderSVFFilter<SecondOrderSVFLowPassCoefficients<std::complex<float> > >;
     327             :   template class SecondOrderSVFFilter<SecondOrderSVFLowPassCoefficients<std::complex<double> > >;
     328             :   template class SecondOrderSVFFilter<SecondOrderSVFBandPassCoefficients<float> >;
     329             :   template class SecondOrderSVFFilter<SecondOrderSVFBandPassCoefficients<std::complex<float> > >;
     330             :   template class SecondOrderSVFFilter<SecondOrderSVFBandPassCoefficients<std::complex<double> > >;
     331             :   template class SecondOrderSVFFilter<SecondOrderSVFHighPassCoefficients<float> >;
     332             :   template class SecondOrderSVFFilter<SecondOrderSVFHighPassCoefficients<std::complex<float> > >;
     333             :   template class SecondOrderSVFFilter<SecondOrderSVFHighPassCoefficients<std::complex<double> > >;
     334             :   template class SecondOrderSVFFilter<SecondOrderSVFNotchCoefficients<float> >;
     335             :   template class SecondOrderSVFFilter<SecondOrderSVFNotchCoefficients<std::complex<float> > >;
     336             :   template class SecondOrderSVFFilter<SecondOrderSVFNotchCoefficients<std::complex<double> > >;
     337             :   template class SecondOrderSVFFilter<SecondOrderSVFPeakCoefficients<float> >;
     338             :   template class SecondOrderSVFFilter<SecondOrderSVFPeakCoefficients<std::complex<float> > >;
     339             :   template class SecondOrderSVFFilter<SecondOrderSVFPeakCoefficients<std::complex<double> > >;
     340             :   template class SecondOrderSVFFilter<SecondOrderSVFBellCoefficients<float> >;
     341             :   template class SecondOrderSVFFilter<SecondOrderSVFBellCoefficients<std::complex<float> > >;
     342             :   template class SecondOrderSVFFilter<SecondOrderSVFBellCoefficients<std::complex<double> > >;
     343             :   template class SecondOrderSVFFilter<SecondOrderSVFLowShelfCoefficients<float> >;
     344             :   template class SecondOrderSVFFilter<SecondOrderSVFLowShelfCoefficients<std::complex<float> > >;
     345             :   template class SecondOrderSVFFilter<SecondOrderSVFLowShelfCoefficients<std::complex<double> > >;
     346             :   template class SecondOrderSVFFilter<SecondOrderSVFHighShelfCoefficients<float> >;
     347             :   template class SecondOrderSVFFilter<SecondOrderSVFHighShelfCoefficients<std::complex<float> > >;
     348             :   template class SecondOrderSVFFilter<SecondOrderSVFHighShelfCoefficients<std::complex<double> > >;
     349             : #endif
     350             :   template class SecondOrderSVFBaseCoefficients<double>;
     351             :   
     352             :   template class SecondOrderSVFLowPassCoefficients<double>;
     353             :   template class SecondOrderSVFBandPassCoefficients<double>;
     354             :   template class SecondOrderSVFHighPassCoefficients<double>;
     355             :   template class SecondOrderSVFNotchCoefficients<double>;
     356             :   template class SecondOrderSVFPeakCoefficients<double>;
     357             :   template class SecondOrderSVFBellCoefficients<double>;
     358             :   template class SecondOrderSVFLowShelfCoefficients<double>;
     359             :   template class SecondOrderSVFHighShelfCoefficients<double>;
     360             :   
     361             :   template class SecondOrderSVFFilter<SecondOrderSVFLowPassCoefficients<double> >;
     362             :   template class SecondOrderSVFFilter<SecondOrderSVFBandPassCoefficients<double> >;
     363             :   template class SecondOrderSVFFilter<SecondOrderSVFHighPassCoefficients<double> >;
     364             :   template class SecondOrderSVFFilter<SecondOrderSVFNotchCoefficients<double> >;
     365             :   template class SecondOrderSVFFilter<SecondOrderSVFPeakCoefficients<double> >;
     366             :   template class SecondOrderSVFFilter<SecondOrderSVFBellCoefficients<double> >;
     367             :   template class SecondOrderSVFFilter<SecondOrderSVFLowShelfCoefficients<double> >;
     368             :   template class SecondOrderSVFFilter<SecondOrderSVFHighShelfCoefficients<double> >;
     369             : }

Generated by: LCOV version TK-3.3.0-4-gdba42eea