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.

State.h 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_STATE_H
  27. #define DSPFILTERS_STATE_H
  28. #include "Common.h"
  29. #include "Biquad.h"
  30. #include <stdexcept>
  31. namespace Dsp
  32. {
  33. /*
  34. * Various forms of state information required to
  35. * process channels of actual sample data.
  36. *
  37. */
  38. //------------------------------------------------------------------------------
  39. /*
  40. * State for applying a second order section to a sample using Direct Form I
  41. *
  42. * Difference equation:
  43. *
  44. * y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2]
  45. * - (a1/a0)*y[n-1] - (a2/a0)*y[n-2]
  46. */
  47. class DirectFormI
  48. {
  49. public:
  50. DirectFormI() { reset(); }
  51. void reset()
  52. {
  53. m_x1 = 0;
  54. m_x2 = 0;
  55. m_y1 = 0;
  56. m_y2 = 0;
  57. }
  58. template <typename Sample>
  59. Sample process1(const Sample in, const BiquadBase& s, const double vsa) // very small amount
  60. {
  61. double out = s.m_b0 * in + s.m_b1 * m_x1 + s.m_b2 * m_x2 - s.m_a1 * m_y1 - s.m_a2 * m_y2 + vsa;
  62. m_x2 = m_x1;
  63. m_y2 = m_y1;
  64. m_x1 = in;
  65. m_y1 = out;
  66. return Sample(out);
  67. }
  68. protected:
  69. double m_x2 = 0; // x[n-2]
  70. double m_y2 = 0; // y[n-2]
  71. double m_x1 = 0; // x[n-1]
  72. double m_y1 = 0; // y[n-1]
  73. };
  74. //------------------------------------------------------------------------------
  75. /*
  76. * State for applying a second order section to a sample using Direct Form II
  77. *
  78. * Difference equation:
  79. *
  80. * v[n] = x[n] - (a1/a0)*v[n-1] - (a2/a0)*v[n-2]
  81. * y(n) = (b0/a0)*v[n] + (b1/a0)*v[n-1] + (b2/a0)*v[n-2]
  82. *
  83. */
  84. class DirectFormII
  85. {
  86. public:
  87. DirectFormII() { reset(); }
  88. void reset()
  89. {
  90. m_v1 = 0;
  91. m_v2 = 0;
  92. }
  93. template <typename Sample>
  94. Sample process1(const Sample in, const BiquadBase& s, const double vsa)
  95. {
  96. double w = in - s.m_a1 * m_v1 - s.m_a2 * m_v2 + vsa;
  97. double out = s.m_b0 * w + s.m_b1 * m_v1 + s.m_b2 * m_v2;
  98. m_v2 = m_v1;
  99. m_v1 = w;
  100. return Sample(out);
  101. }
  102. private:
  103. double m_v1 = 0; // v[-1]
  104. double m_v2 = 0; // v[-2]
  105. };
  106. //------------------------------------------------------------------------------
  107. /*
  108. * Transposed Direct Form I and II
  109. * by lubomir i. ivanov (neolit123 [at] gmail)
  110. *
  111. * Reference:
  112. * http://www.kvraudio.com/forum/viewtopic.php?p=4430351
  113. *
  114. */
  115. // I think this one is broken
  116. class TransposedDirectFormI
  117. {
  118. public:
  119. TransposedDirectFormI() { reset(); }
  120. void reset()
  121. {
  122. m_v = 0;
  123. m_s1 = 0;
  124. m_s1_1 = 0;
  125. m_s2 = 0;
  126. m_s2_1 = 0;
  127. m_s3 = 0;
  128. m_s3_1 = 0;
  129. m_s4 = 0;
  130. m_s4_1 = 0;
  131. }
  132. template <typename Sample>
  133. Sample process1(const Sample in, const BiquadBase& s, const double /*vsa*/)
  134. {
  135. // can be: in += m_s1_1;
  136. m_v = in + m_s1_1;
  137. double out = s.m_b0 * m_v + m_s3_1;
  138. m_s1 = m_s2_1 - s.m_a1 * m_v;
  139. m_s2 = -s.m_a2 * m_v;
  140. m_s3 = s.m_b1 * m_v + m_s4_1;
  141. m_s4 = s.m_b2 * m_v;
  142. m_s4_1 = m_s4;
  143. m_s3_1 = m_s3;
  144. m_s2_1 = m_s2;
  145. m_s1_1 = m_s1;
  146. return Sample(out);
  147. }
  148. private:
  149. double m_v = 0;
  150. double m_s1 = 0;
  151. double m_s1_1 = 0;
  152. double m_s2 = 0;
  153. double m_s2_1 = 0;
  154. double m_s3 = 0;
  155. double m_s3_1 = 0;
  156. double m_s4 = 0;
  157. double m_s4_1 = 0;
  158. };
  159. //------------------------------------------------------------------------------
  160. class TransposedDirectFormII
  161. {
  162. public:
  163. TransposedDirectFormII() { reset(); }
  164. void reset()
  165. {
  166. m_s1 = 0;
  167. m_s1_1 = 0;
  168. m_s2 = 0;
  169. m_s2_1 = 0;
  170. }
  171. template <typename Sample>
  172. Sample process1(const Sample in, const BiquadBase& s, const double vsa)
  173. {
  174. double out = m_s1_1 + s.m_b0 * in + vsa;
  175. m_s1 = m_s2_1 + s.m_b1 * in - s.m_a1 * out;
  176. m_s2 = s.m_b2 * in - s.m_a2 * out;
  177. m_s1_1 = m_s1;
  178. m_s2_1 = m_s2;
  179. return Sample(out);
  180. }
  181. private:
  182. double m_s1 = 0;
  183. double m_s1_1 = 0;
  184. double m_s2 = 0;
  185. double m_s2_1 = 0;
  186. };
  187. //------------------------------------------------------------------------------
  188. // Holds an array of states suitable for multi-channel processing
  189. template <int Channels, class StateType>
  190. class ChannelsState
  191. {
  192. public:
  193. ChannelsState() { }
  194. int getNumChannels() const { return Channels; }
  195. void reset() { for (int i = 0; i < Channels; ++i) { m_state[i].reset(); } }
  196. StateType& operator[](int index)
  197. {
  198. assert(index >= 0 && index < Channels);
  199. return m_state[index];
  200. }
  201. template <class Filter, typename Sample>
  202. void process(int nSamples, Sample* const* arrayOfChannels, Filter& filter)
  203. {
  204. for (int i = 0; i < Channels; ++i) { filter.process(nSamples, arrayOfChannels[i], m_state[i]); }
  205. }
  206. private:
  207. StateType m_state[Channels];
  208. };
  209. // Empty state, can't process anything
  210. template <class StateType>
  211. class ChannelsState<0, StateType>
  212. {
  213. public:
  214. int getNumChannels() const { return 0; }
  215. void reset() { throw std::logic_error("attempt to reset empty ChannelState"); }
  216. template <class FilterDesign, typename Sample>
  217. void process(int /*nSamples*/, Sample* const* /*arrayOfChannels*/, FilterDesign& /*filter*/)
  218. {
  219. throw std::logic_error("attempt to process empty ChannelState");
  220. }
  221. };
  222. //------------------------------------------------------------------------------
  223. } // namespace Dsp
  224. #endif