Open Ephys GUI
 All Classes Functions Variables
RootFinder.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_ROOTFINDER_H
37 #define DSPFILTERS_ROOTFINDER_H
38 
39 #include "Common.h"
40 #include "Types.h"
41 
42 namespace Dsp {
43 
44 //
45 // Finds the complex roots of the given polynomial with
46 // complex-valued coefficients using a numerical method.
47 //
48 
50 {
51 public:
52  struct Array
53  {
54  Array (int max, complex_t* values)
55  // : m_max (max)
56  // , m_values (values)
57  {
58  }
59 
60  //complex_t& operator[] (int index)
61  //{
62  //};
63  };
64 
65  //
66  // Find roots of polynomial f(x)=a[0]+a[1]*x+a[2]*x^2...+a[degree]*x^degree
67  // The input coefficients are set using coef()[].
68  // The solutions are placed in roots.
69  //
70  void solve (int degree,
71  bool polish = true,
72  bool doSort = true);
73 
74  // Evaluates the polynomial at x
75  complex_t eval (int degree,
76  const complex_t& x);
77 
78  // Direct access to the input coefficient array of size degree+1.
79  complex_t* coef()
80  {
81  return m_a;
82  }
83 
84  // Direct access to the resulting roots array of size degree
85  complex_t* root()
86  {
87  return m_root;
88  }
89 
90  // sort the roots by descending imaginary part
91  void sort (int degree);
92 
93 private:
94  // Improves x as a root using Laguerre's method.
95  // The input coefficient array has degree+1 elements.
96  void laguerre (int degree,
97  complex_t a[],
98  complex_t& x,
99  int& its);
100 
101 protected:
102  int m_maxdegree;
103  complex_t* m_a; // input coefficients (m_maxdegree+1 elements)
104  complex_t* m_ad; // copy of deflating coefficients
105  complex_t* m_root; // array of roots (maxdegree elements)
106 };
107 
108 //------------------------------------------------------------------------------
109 
110 template<int maxdegree>
112 {
113  RootFinder()
114  {
115  m_maxdegree = maxdegree;
116  m_a = m_a0;
117  m_ad = m_ad0;
118  m_root = m_r;
119  }
120 
121 private:
122  complex_t m_a0 [maxdegree+1];
123  complex_t m_ad0[maxdegree+1];
124  complex_t m_r [maxdegree];
125 };
126 
127 }
128 
129 #endif