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

          Line data    Source code
       1             : /**
       2             :  * \file RIAAFilter.hxx
       3             :  */
       4             : 
       5             : #include "RIAAFilter.h"
       6             : #include <ATK/EQ/helpers.h>
       7             : 
       8             : #include <boost/math/tools/polynomial.hpp>
       9             : 
      10             : #include <cmath>
      11             : 
      12             : namespace ATK
      13             : {
      14             :   template<typename DataType>
      15           5 :   RIAACoefficients<DataType>::RIAACoefficients(gsl::index nb_channels)
      16           5 :     :Parent(nb_channels)
      17             :   {
      18           5 :   }
      19             : 
      20             :   template<typename T>
      21          20 :   void generate_RIAA_coeffs(EQUtilities::ZPK<T>& zpk, gsl::index input_sampling_rate)
      22             :   {
      23          20 :     auto pi = boost::math::constants::pi<T>();
      24          20 :     T t1 = 1 / (input_sampling_rate * std::tan(pi / (75e-6 * input_sampling_rate)));
      25          20 :     T t2 = 1 / (input_sampling_rate * std::tan(pi / (318e-6 * input_sampling_rate)));
      26          20 :     T t3 = 1 / (input_sampling_rate * std::tan(pi / (3180e-6 * input_sampling_rate)));
      27             :     
      28          20 :     zpk.k = 318e-6/75e-6 * t2/(t1*t3);
      29          20 :     zpk.z.push_back(-1/t2);
      30          20 :     zpk.p.push_back(-1/t1);
      31          20 :     zpk.p.push_back(-1/t3);
      32             :     
      33          20 :     EQUtilities::zpk_bilinear(input_sampling_rate, zpk);
      34          20 :   }
      35             :   
      36             :   template <typename DataType>
      37          10 :   void RIAACoefficients<DataType>::setup()
      38             :   {
      39          10 :     Parent::setup();
      40             : 
      41          10 :     coefficients_in.assign(in_order+1, 0);
      42          10 :     coefficients_out.assign(out_order, 0);
      43             :     
      44          20 :     EQUtilities::ZPK<CoeffDataType> zpk;
      45             : 
      46          20 :     boost::math::tools::polynomial<CoeffDataType> b{ 1 };
      47          20 :     boost::math::tools::polynomial<CoeffDataType> a{ 1 };
      48             : 
      49          10 :     generate_RIAA_coeffs(zpk, input_sampling_rate);
      50          10 :     EQUtilities::zpk2ba(zpk, b, a);
      51             :     
      52          10 :     auto in_size = std::min(in_order + 1, static_cast<gsl::index>(b.size()));
      53          40 :     for (gsl::index i = 0; i < in_size; ++i)
      54             :     {
      55          30 :       coefficients_in[i] = b[i];
      56             :     }
      57          10 :     auto out_size = std::min(out_order, static_cast<gsl::index>(a.size() - 1));
      58          30 :     for (gsl::index i = 0; i < out_size; ++i)
      59             :     {
      60          20 :       coefficients_out[i] = -a[i];
      61             :     }
      62          10 :   }
      63             : 
      64             :   template<typename DataType>
      65           5 :   InverseRIAACoefficients<DataType>::InverseRIAACoefficients(gsl::index nb_channels)
      66           5 :     :Parent(nb_channels)
      67             :   {
      68           5 :   }
      69             : 
      70             :   template <typename DataType>
      71          10 :   void InverseRIAACoefficients<DataType>::setup()
      72             :   {
      73          10 :     Parent::setup();
      74             : 
      75          10 :     coefficients_in.assign(in_order+1, 0);
      76          10 :     coefficients_out.assign(out_order, 0);
      77             :     
      78          20 :     EQUtilities::ZPK<CoeffDataType> zpk;
      79             :     
      80          20 :     boost::math::tools::polynomial<CoeffDataType> b{ 1 };
      81          20 :     boost::math::tools::polynomial<CoeffDataType> a{ 1 };
      82             :     
      83          10 :     generate_RIAA_coeffs(zpk, input_sampling_rate);
      84          10 :     zpk.z.back() = -.8;
      85          10 :     EQUtilities::zpk2ba(zpk, b, a);
      86             :     
      87          10 :     auto in_size = std::min(in_order + 1, static_cast<gsl::index>(a.size()));
      88          40 :     for (gsl::index i = 0; i < in_size; ++i)
      89             :     {
      90          30 :       coefficients_in[i] = a[i] / b[b.size() - 1];
      91             :     }
      92          10 :     auto out_size = std::min(out_order, static_cast<gsl::index>(b.size() - 1));
      93          30 :     for (gsl::index i = 0; i < out_size; ++i)
      94             :     {
      95          20 :       coefficients_out[i] = -b[i] / b[b.size() - 1];
      96             :     }
      97          10 :   }
      98             : }

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