Resonator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Resonator.cpp
  2. *
  3. * Copyright (C) 2008-2011, 2015 David Weenink
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * djmw 20081029
  20. * djmw 20081124 +ConstantGainResonator
  21. * djmw 20110304 Thing_new
  22. */
  23. #include "Resonator.h"
  24. Thing_implement (Filter, Daata, 0);
  25. #define SETBC(f,bw) \
  26. double r = exp (-NUMpi * dT * bw); \
  27. c = -(r * r); \
  28. b = 2.0 * r * cos (2.0 * NUMpi * f * dT);
  29. void structFilter :: v_resetMemory () {
  30. p1 = p2 = 0;
  31. }
  32. void structFilter :: v_setFB (double f, double bw) {
  33. SETBC (f, bw)
  34. a = 1.0 - b - c;
  35. }
  36. double structFilter :: v_getOutput (double input) {
  37. double output = a * input + b * p1 + c * p2;
  38. p2 = p1;
  39. p1 = output;
  40. return output;
  41. }
  42. Thing_implement (Resonator, Filter, 0);
  43. void structResonator :: v_setFB (double f, double bw) {
  44. SETBC (f, bw)
  45. a = normalisation == Resonator_NORMALISATION_H0 ? (1.0 - b - c) : (1 + c) * sin (2.0 * NUMpi * f * dT);
  46. }
  47. autoResonator Resonator_create (double dT, int normalisation) {
  48. try {
  49. autoResonator me = Thing_new (Resonator);
  50. my a = 1; // all-pass
  51. my dT = dT;
  52. my normalisation = normalisation;
  53. return me;
  54. } catch (MelderError) {
  55. Melder_throw (U"Resonator not created.");
  56. }
  57. }
  58. Thing_implement (AntiResonator, Filter, 0);
  59. void structAntiResonator :: v_setFB (double f, double bw) {
  60. if (f <= 0 && bw <= 0) {
  61. a = 1; b = -2; c = 1; // all-pass except dc
  62. } else {
  63. SETBC (f, bw)
  64. a = 1 / (1.0 - b - c);
  65. // The next equations are incorporated in the getOutput function
  66. //c *= - a; b *= - a;
  67. }
  68. }
  69. /* y[n] = a * (x[n] - b * x[n-1] - c * x[n-2]) */
  70. double structAntiResonator :: v_getOutput (double input) {
  71. double output = a * (input - b * p1 - c * p2);
  72. p2 = p1;
  73. p1 = input;
  74. return output;
  75. }
  76. Thing_implement (ConstantGainResonator, Filter, 0);
  77. void structConstantGainResonator :: v_resetMemory () {
  78. p1 = p2 = p3 = p4 = 0;
  79. }
  80. void structConstantGainResonator :: v_setFB (double f, double bw) {
  81. SETBC (f, bw)
  82. a = 1 - r;
  83. d = -r;
  84. }
  85. /* y[n] = a * (x[n] + d * x[n-2]) + b * y[n-1] + c * y[n-2] */
  86. double structConstantGainResonator :: v_getOutput (double input) {
  87. double output = a * (input + d * p4) + b * p1 + c * p2;
  88. p2 = p1;
  89. p1 = output;
  90. p4 = p3;
  91. p3 = input;
  92. return output;
  93. }
  94. autoConstantGainResonator ConstantGainResonator_create (double dT) {
  95. try {
  96. autoConstantGainResonator me = Thing_new (ConstantGainResonator);
  97. my a = 1; // all-pass
  98. my dT = dT;
  99. return me;
  100. } catch (MelderError) {
  101. Melder_throw (U"ConstantGainResonator not created.");
  102. }
  103. }
  104. autoAntiResonator AntiResonator_create (double dT) {
  105. try {
  106. autoAntiResonator me = Thing_new (AntiResonator);
  107. my a = 1; // all-pass
  108. my dT = dT;
  109. return me;
  110. } catch (MelderError) {
  111. Melder_throw (U"AntiResonator not created.");
  112. }
  113. }
  114. void Filter_setFB (Filter me, double f, double b) {
  115. my v_setFB (f, b);
  116. }
  117. double Filter_getOutput (Filter me, double input) {
  118. return my v_getOutput (input);
  119. }
  120. void Filter_resetMemory (Filter me) {
  121. my v_resetMemory ();
  122. }
  123. /* End of file Resonator.cpp */