Open Ephys GUI
 All Classes Functions Variables
MathSupplement.h
1 /*******************************************************************************
2 
3 "A Collection of Useful C++ Classes for Digital Signal Processing"
4  By Vincent Falco
5 
6 Official project location:
7 http://code.google.com/p/dspfilterscpp/
8 
9 See Documentation.cpp for contact information, notes, and bibliography.
10 
11 --------------------------------------------------------------------------------
12 
13 License: MIT License (http://www.opensource.org/licenses/mit-license.php)
14 Copyright (c) 2009 by Vincent Falco
15 
16 Permission is hereby granted, free of charge, to any person obtaining a copy
17 of this software and associated documentation files (the "Software"), to deal
18 in the Software without restriction, including without limitation the rights
19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 copies of the Software, and to permit persons to whom the Software is
21 furnished to do so, subject to the following conditions:
22 
23 The above copyright notice and this permission notice shall be included in
24 all copies or substantial portions of the Software.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 THE SOFTWARE.
33 
34 *******************************************************************************/
35 
36 #ifndef DSPFILTERS_MATHSUPPLEMENT_H
37 #define DSPFILTERS_MATHSUPPLEMENT_H
38 
39 #include "Common.h"
40 
41 namespace Dsp {
42 
43 const double doublePi =3.1415926535897932384626433832795028841971;
44 const double doublePi_2 =1.5707963267948966192313216916397514420986;
45 const double doubleLn2 =0.69314718055994530941723212145818;//?????
46 const double doubleLn10 =2.3025850929940456840179914546844;//??????
47 
48 typedef std::complex<double> complex_t;
49 typedef std::pair<complex_t, complex_t> complex_pair_t;
50 
51 template<typename Real>
52 inline std::complex<Real> solve_quadratic_1 (Real a, Real b, Real c)
53 {
54  return (-b + sqrt (std::complex<Real> (b*b - 4*a*c, 0))) / (2. * a);
55 }
56 
57 template<typename Real>
58 inline std::complex<Real> solve_quadratic_2 (Real a, Real b, Real c)
59 {
60  return (-b - sqrt (std::complex<Real> (b*b - 4*a*c, 0))) / (2. * a);
61 }
62 
63 inline const complex_t infinity()
64 {
65  return complex_t (std::numeric_limits<double>::infinity());
66 }
67 
68 inline const complex_t adjust_imag (const complex_t& c)
69 {
70  if (fabs (c.imag()) < 1e-30)
71  return complex_t (c.real(), 0);
72  else
73  return c;
74 }
75 
76 template <typename Ty, typename To>
77 inline std::complex<Ty> addmul (const std::complex<Ty>& c,
78  Ty v,
79  const std::complex<To>& c1)
80 {
81  return std::complex <Ty> (
82  c.real() + v * c1.real(), c.imag() + v * c1.imag());
83 }
84 
85 template <typename Ty>
86 inline std::complex<Ty> recip (const std::complex<Ty>& c)
87 {
88  Ty n = 1.0 / std::norm (c);
89 
90  return std::complex<Ty> (n * c.real(), n * c.imag());
91 }
92 
93 template <typename Ty>
94 inline Ty asinh (Ty x)
95 {
96  return log (x + std::sqrt (x * x + 1 ));
97 }
98 
99 template <typename Ty>
100 inline Ty acosh (Ty x)
101 {
102  return log (x + std::sqrt (x * x - 1));
103 }
104 
105 template <typename Ty>
106 inline bool is_nan (Ty v)
107 {
108  return !(v == v);
109 }
110 
111 template <>
112 inline bool is_nan<complex_t> (complex_t v)
113 {
114  return Dsp::is_nan (v.real()) || Dsp::is_nan (v.imag());
115 }
116 
117 //------------------------------------------------------------------------------
118 
119 /*
120  * Hack to prevent denormals
121  *
122  */
123 
124 //const double anti_denormal_vsa = 1e-16; // doesn't prevent denormals
125 //const double anti_denormal_vsa = 0;
126 const double anti_denormal_vsa = 1e-8;
127 
129 {
130 public:
132  : m_v (anti_denormal_vsa)
133  {
134  }
135 
136  // small alternating current
137  inline double ac ()
138  {
139  return m_v = -m_v;
140  }
141 
142  // small direct current
143  static inline double dc ()
144  {
145  return anti_denormal_vsa;
146  }
147 
148 private:
149  double m_v;
150 };
151 
152 }
153 
154 #endif