Open Ephys GUI
 All Classes Functions Variables
SmoothedFilter.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_SMOOTHEDFILTER_H
37 #define DSPFILTERS_SMOOTHEDFILTER_H
38 
39 #include "Common.h"
40 #include "Filter.h"
41 
42 namespace Dsp {
43 
44 /*
45  * Implements smooth modulation of time-varying filter parameters
46  *
47  */
48 template <class DesignClass,
49  int Channels,
50  class StateType = DirectFormII>
52  : public FilterDesign <DesignClass,
53  Channels,
54  StateType>
55 {
56 public:
58 
59  SmoothedFilterDesign (int transitionSamples)
60  : m_transitionSamples (transitionSamples)
61  , m_remainingSamples (-1) // first time flag
62  {
63  }
64 
65  // Process a block of samples.
66  template <typename Sample>
67  void processBlock (int numSamples,
68  Sample* const* destChannelArray)
69  {
70  const int numChannels = this->getNumChannels();
71 
72  // If this goes off it means setup() was never called
73  assert (m_remainingSamples >= 0);
74 
75  // first handle any transition samples
76  int remainingSamples = std::min (m_remainingSamples, numSamples);
77 
78  if (remainingSamples > 0)
79  {
80  // interpolate parameters for each sample
81  const double t = 1. / m_remainingSamples;
82  double dp[maxParameters];
83  for (int i = 0; i < DesignClass::NumParams; ++i)
84  dp[i] = (this->getParams()[i] - m_transitionParams[i]) * t;
85 
86  for (int n = 0; n < remainingSamples; ++n)
87  {
88  for (int i = DesignClass::NumParams; --i >=0;)
89  m_transitionParams[i] += dp[i];
90 
91  m_transitionFilter.setParams (m_transitionParams);
92 
93  for (int i = numChannels; --i >= 0;)
94  {
95  Sample* dest = destChannelArray[i]+n;
96  *dest = this->m_state[i].process (*dest, m_transitionFilter);
97  }
98  }
99 
100  m_remainingSamples -= remainingSamples;
101 
102  if (m_remainingSamples == 0)
103  m_transitionParams = this->getParams();
104  }
105 
106  // do what's left
107  if (numSamples - remainingSamples > 0)
108  {
109  // no transition
110  for (int i = 0; i < numChannels; ++i)
111  this->m_design.process (numSamples - remainingSamples,
112  destChannelArray[i] + remainingSamples,
113  this->m_state[i]);
114  }
115  }
116 
117  void process (int numSamples, float* const* arrayOfChannels)
118  {
119  processBlock (numSamples, arrayOfChannels);
120  }
121 
122  void process (int numSamples, double* const* arrayOfChannels)
123  {
124  processBlock (numSamples, arrayOfChannels);
125  }
126 
127 protected:
128  void doSetParams (const Params& parameters)
129  {
130  if (m_remainingSamples >= 0)
131  {
132  m_remainingSamples = m_transitionSamples;
133  }
134  else
135  {
136  // first time
137  m_remainingSamples = 0;
138  m_transitionParams = parameters;
139  }
140 
141  filter_type_t::doSetParams (parameters);
142  }
143 
144 protected:
145  Params m_transitionParams;
146  DesignClass m_transitionFilter;
147  int m_transitionSamples;
148 
149  int m_remainingSamples; // remaining transition samples
150 };
151 
152 }
153 
154 #endif