You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Filter.h 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*******************************************************************************
  2. "A Collection of Useful C++ Classes for Digital Signal Processing"
  3. By Vinnie Falco
  4. Official project location:
  5. https://github.com/vinniefalco/DSPFilters
  6. See Documentation.cpp for contact information, notes, and bibliography.
  7. --------------------------------------------------------------------------------
  8. License: MIT License (http://www.opensource.org/licenses/mit-license.php)
  9. Copyright (c) 2009 by Vinnie Falco
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. *******************************************************************************/
  26. #ifndef DSPFILTERS_FILTER_H
  27. #define DSPFILTERS_FILTER_H
  28. #include "Common.h"
  29. #include "MathSupplement.h"
  30. #include "Params.h"
  31. #include "State.h"
  32. #include "Types.h"
  33. namespace Dsp
  34. {
  35. /*
  36. * Filter
  37. *
  38. * Full abstraction of a digital IIR filter.
  39. * Supports run-time introspection and modulation of filter
  40. * parameters.
  41. *
  42. */
  43. class Filter
  44. {
  45. public:
  46. virtual ~Filter();
  47. virtual Kind getKind() const = 0;
  48. virtual const std::string getName() const = 0;
  49. virtual int getNumParams() const = 0;
  50. virtual ParamInfo getParamInfo(int index) const = 0;
  51. Params getDefaultParams() const;
  52. const Params& getParams() const { return m_params; }
  53. double getParam(int paramIndex) const
  54. {
  55. assert(paramIndex >= 0 && paramIndex <= getNumParams());
  56. return m_params[paramIndex];
  57. }
  58. void setParam(int paramIndex, double nativeValue)
  59. {
  60. assert(paramIndex >= 0 && paramIndex <= getNumParams());
  61. m_params[paramIndex] = nativeValue;
  62. doSetParams(m_params);
  63. }
  64. int findParamId(int paramId);
  65. void setParamById(int paramId, double nativeValue);
  66. void setParams(const Params& parameters)
  67. {
  68. m_params = parameters;
  69. doSetParams(parameters);
  70. }
  71. #include "FilterSynthesisH.inl"
  72. // This makes a best-effort to pick up the values
  73. // of matching parameters from another set. It uses
  74. // the ParamID information to make the match.
  75. void copyParamsFrom(Filter const* other);
  76. virtual std::vector<PoleZeroPair> getPoleZeros() const = 0;
  77. virtual complex_t response(double normalizedFrequency) const = 0;
  78. virtual int getNumChannels() = 0;
  79. virtual void reset() = 0;
  80. virtual void process(int nSamples, float* const* arrayOfChannels) = 0;
  81. virtual void process(int nSamples, double* const* arrayOfChannels) = 0;
  82. protected:
  83. virtual void doSetParams(const Params& parameters) = 0;
  84. private:
  85. Params m_params;
  86. };
  87. //------------------------------------------------------------------------------
  88. /*
  89. * FilterDesign
  90. *
  91. * This container holds a filter Design (Gui-friendly layer) and
  92. * optionally combines it with the necessary state information to
  93. * process channel data.
  94. *
  95. */
  96. // Factored to reduce template instantiations
  97. template <class DesignClass>
  98. class FilterDesignBase : public Filter
  99. {
  100. public:
  101. Kind getKind() const override { return m_design.getKind(); }
  102. const std::string getName() const override { return m_design.getName(); }
  103. int getNumParams() const override { return DesignClass::NumParams; }
  104. Params getDefaultParams() const { return m_design.getDefaultParams(); }
  105. ParamInfo getParamInfo(int index) const override
  106. {
  107. switch (index)
  108. {
  109. case 0: return m_design.getParamInfo_0();
  110. case 1: return m_design.getParamInfo_1();
  111. case 2: return m_design.getParamInfo_2();
  112. case 3: return m_design.getParamInfo_3();
  113. case 4: return m_design.getParamInfo_4();
  114. case 5: return m_design.getParamInfo_5();
  115. case 6: return m_design.getParamInfo_6();
  116. case 7: return m_design.getParamInfo_7();
  117. default: return ParamInfo();
  118. }
  119. }
  120. std::vector<PoleZeroPair> getPoleZeros() const override { return m_design.getPoleZeros(); }
  121. complex_t response(double normalizedFrequency) const override { return m_design.response(normalizedFrequency); }
  122. protected:
  123. void doSetParams(const Params& parameters) override { m_design.setParams(parameters); }
  124. #include "FilterSynthesisH2.inl"
  125. DesignClass m_design;
  126. };
  127. template <class DesignClass, int Channels = 0, class StateType = DirectFormII>
  128. class FilterDesign : public FilterDesignBase<DesignClass>
  129. {
  130. public:
  131. FilterDesign() { }
  132. int getNumChannels() override { return Channels; }
  133. void reset() override { m_state.reset(); }
  134. void process(int nSamples, float* const* arrayOfChannels) override
  135. {
  136. m_state.process(nSamples, arrayOfChannels, FilterDesignBase<DesignClass>::m_design);
  137. }
  138. void process(int nSamples, double* const* arrayOfChannels) override
  139. {
  140. m_state.process(nSamples, arrayOfChannels, FilterDesignBase<DesignClass>::m_design);
  141. }
  142. protected:
  143. ChannelsState<Channels, typename DesignClass::template State<StateType>> m_state;
  144. };
  145. //------------------------------------------------------------------------------
  146. /*
  147. * This container combines a raw filter with state information
  148. * so it can process channels. In order to set up the filter you
  149. * must call a setup function directly. Smooth changes are
  150. * not supported, but this class has a smaller footprint.
  151. *
  152. */
  153. template <class FilterClass, int Channels = 0, class StateType = DirectFormII>
  154. class SimpleFilter : public FilterClass
  155. {
  156. public:
  157. int getNumChannels() { return Channels; }
  158. void reset() { m_state.reset(); }
  159. template <typename Sample>
  160. void process(int nSamples, Sample* const* arrayOfChannels) { m_state.process(nSamples, arrayOfChannels, *static_cast<FilterClass*>(this)); }
  161. protected:
  162. ChannelsState<Channels, typename FilterClass::template State<StateType>> m_state;
  163. };
  164. } // namespace Dsp
  165. #endif