Open Ephys GUI
 All Classes Functions Variables
Cascade.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_CASCADE_H
37 #define DSPFILTERS_CASCADE_H
38 
39 #include "Common.h"
40 #include "Biquad.h"
41 #include "Filter.h"
42 #include "Layout.h"
43 #include "MathSupplement.h"
44 
45 namespace Dsp {
46 
47 /*
48  * Holds coefficients for a cascade of second order sections.
49  *
50  */
51 
52 // Factored implementation to reduce template instantiations
53 class Cascade
54 {
55 public:
56  template <class StateType>
57  class StateBase : private DenormalPrevention
58  {
59  public:
60  template <typename Sample>
61  inline Sample process (const Sample in, const Cascade& c)
62  {
63  double out = in;
64  StateType* state = m_stateArray;
65  Biquad const* stage = c.m_stageArray;
66  const double vsa = ac();
67  int i = c.m_numStages - 1;
68  out = (state++)->process1 (out, *stage++, vsa);
69  for (; --i >= 0;)
70  out = (state++)->process1 (out, *stage++, 0);
71  //for (int i = c.m_numStages; --i >= 0; ++state, ++stage)
72  // out = state->process1 (out, *stage, vsa);
73  return static_cast<Sample> (out);
74  }
75 
76  protected:
77  StateBase (StateType* stateArray)
78  : m_stateArray (stateArray)
79  {
80  }
81 
82  protected:
83  StateType* m_stateArray;
84  };
85 
86  struct Stage : Biquad
87  {
88  };
89 
90  struct Storage
91  {
92  Storage (int maxStages_, Stage* stageArray_)
93  : maxStages (maxStages_)
94  , stageArray (stageArray_)
95  {
96  }
97 
98  int maxStages;
99  Stage* stageArray;
100  };
101 
102  int getNumStages () const
103  {
104  return m_numStages;
105  }
106 
107  const Stage& operator[] (int index)
108  {
109  assert (index >= 0 && index <= m_numStages);
110  return m_stageArray[index];
111  }
112 
113 public:
114  // Calculate filter response at the given normalized frequency.
115  complex_t response (double normalizedFrequency) const;
116 
117  std::vector<PoleZeroPair> getPoleZeros () const;
118 
119  // Process a block of samples in the given form
120  template <class StateType, typename Sample>
121  void process (int numSamples, Sample* dest, StateType& state) const
122  {
123  while (--numSamples >= 0)
124  *dest++ = state.process (*dest, *this);
125  }
126 
127 protected:
128  Cascade ();
129 
130  void setCascadeStorage (const Storage& storage);
131 
132  void applyScale (double scale);
133  void setLayout (const LayoutBase& proto);
134 
135 private:
136  int m_numStages;
137  int m_maxStages;
138  Stage* m_stageArray;
139 };
140 
141 //------------------------------------------------------------------------------
142 
143 // Storage for Cascade
144 template <int MaxStages>
146 {
147 public:
148  template <class StateType>
149  class State : public Cascade::StateBase <StateType>
150  {
151  public:
153  {
155  reset ();
156  }
157 
158  void reset ()
159  {
160  StateType* state = m_states;
161  for (int i = MaxStages; --i >= 0; ++state)
162  state->reset();
163  }
164 
165  private:
166  StateType m_states[MaxStages];
167  };
168 
169  /*@Internal*/
170  Cascade::Storage getCascadeStorage()
171  {
172  return Cascade::Storage (MaxStages, m_stages);
173  }
174 
175 private:
176  Cascade::Stage m_stages[MaxStages];
177 };
178 
179 }
180 
181 #endif