iir1
MathSupplement.h
1 
36 #ifndef IIR1_MATHSUPPLEMENT_H
37 #define IIR1_MATHSUPPLEMENT_H
38 
39 #include "Common.h"
40 
41 #include<complex>
42 
43 #ifdef _MSC_VER
44  // Under Unix these have already default instantiations but not under Vis Studio
45 template class DllExport std::complex<double>;
46 template class DllExport std::complex<float>;
47 #endif
48 
49 namespace Iir {
50 
51 const double doublePi =3.1415926535897932384626433832795028841971;
52 const double doublePi_2 =1.5707963267948966192313216916397514420986;
53 const double doubleLn2 =0.69314718055994530941723212145818;
54 const double doubleLn10 =2.3025850929940456840179914546844;
55 
56 typedef std::complex<double> complex_t;
57 typedef std::pair<complex_t, complex_t> complex_pair_t;
58 
59 template<typename Real>
60 inline std::complex<Real> solve_quadratic_1 (Real a, Real b, Real c)
61 {
62  return (-b + sqrt (std::complex<Real> (b*b - 4*a*c, 0))) / (2. * a);
63 }
64 
65 template<typename Real>
66 inline std::complex<Real> solve_quadratic_2 (Real a, Real b, Real c)
67 {
68  return (-b - sqrt (std::complex<Real> (b*b - 4*a*c, 0))) / (2. * a);
69 }
70 
71 inline const complex_t infinity()
72 {
73  return complex_t (std::numeric_limits<double>::infinity());
74 }
75 
76 inline const complex_t adjust_imag (const complex_t& c)
77 {
78  if (fabs (c.imag()) < 1e-30)
79  return complex_t (c.real(), 0);
80  else
81  return c;
82 }
83 
84 template <typename Ty, typename To>
85 inline std::complex<Ty> addmul (const std::complex<Ty>& c,
86  Ty v,
87  const std::complex<To>& c1)
88 {
89  return std::complex <Ty> (
90  c.real() + v * c1.real(), c.imag() + v * c1.imag());
91 }
92 
93 template <typename Ty>
94 inline std::complex<Ty> recip (const std::complex<Ty>& c)
95 {
96  Ty n = 1.0 / std::norm (c);
97 
98  return std::complex<Ty> (n * c.real(), n * c.imag());
99 }
100 
101 template <typename Ty>
102 inline Ty asinh (Ty x)
103 {
104  return log (x + std::sqrt (x * x + 1 ));
105 }
106 
107 template <typename Ty>
108 inline Ty acosh (Ty x)
109 {
110  return log (x + std::sqrt (x * x - 1));
111 }
112 
113 template <typename Ty>
114 inline bool is_nan (Ty v)
115 {
116  return !(v == v);
117 }
118 
119 template <>
120 inline bool is_nan<complex_t> (complex_t v)
121 {
122  return Iir::is_nan (v.real()) || Iir::is_nan (v.imag());
123 }
124 
125 }
126 
127 #endif
Definition: Biquad.cpp:40