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.

170 lines
3.8KB

  1. //
  2. // Biquad.cpp
  3. //
  4. // Created by Nigel Redmon on 11/24/12
  5. // EarLevel Engineering: earlevel.com
  6. // Copyright 2012 Nigel Redmon
  7. //
  8. // For a complete explanation of the Biquad code:
  9. // http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/
  10. //
  11. // License:
  12. //
  13. // This source code is provided as is, without warranty.
  14. // You may copy and distribute verbatim copies of this document.
  15. // You may modify and use this source code to create binary code
  16. // for your own purposes, free or commercial.
  17. //
  18. #include <math.h>
  19. #include "Biquad.h"
  20. namespace rack_plugin_Autodafe {
  21. Biquad::Biquad() {
  22. type = bq_type_lowpass;
  23. a0 = 1.0;
  24. a1 = a2 = b1 = b2 = 0.0;
  25. Fc = 0.50;
  26. Q = 0.707;
  27. peakGain = 0.0;
  28. z1 = z2 = 0.0;
  29. }
  30. Biquad::Biquad(int type, double Fc, double Q, double peakGainDB) {
  31. setBiquad(type, Fc, Q, peakGainDB);
  32. z1 = z2 = 0.0;
  33. }
  34. Biquad::~Biquad() {
  35. }
  36. void Biquad::setType(int type) {
  37. this->type = type;
  38. calcBiquad();
  39. }
  40. void Biquad::setQ(double Q) {
  41. this->Q = Q;
  42. calcBiquad();
  43. }
  44. void Biquad::setFc(double Fc) {
  45. this->Fc = Fc;
  46. calcBiquad();
  47. }
  48. void Biquad::setPeakGain(double peakGainDB) {
  49. this->peakGain = peakGainDB;
  50. calcBiquad();
  51. }
  52. void Biquad::setBiquad(int type, double Fc, double Q, double peakGainDB) {
  53. this->type = type;
  54. this->Q = Q;
  55. this->Fc = Fc;
  56. setPeakGain(peakGainDB);
  57. }
  58. void Biquad::calcBiquad(void) {
  59. double norm;
  60. double V = pow(10, fabs(peakGain) / 20.0);
  61. double K = tan(M_PI * Fc);
  62. switch (this->type) {
  63. case bq_type_lowpass:
  64. norm = 1 / (1 + K / Q + K * K);
  65. a0 = K * K * norm;
  66. a1 = 2 * a0;
  67. a2 = a0;
  68. b1 = 2 * (K * K - 1) * norm;
  69. b2 = (1 - K / Q + K * K) * norm;
  70. break;
  71. case bq_type_highpass:
  72. norm = 1 / (1 + K / Q + K * K);
  73. a0 = 1 * norm;
  74. a1 = -2 * a0;
  75. a2 = a0;
  76. b1 = 2 * (K * K - 1) * norm;
  77. b2 = (1 - K / Q + K * K) * norm;
  78. break;
  79. case bq_type_bandpass:
  80. norm = 1 / (1 + K / Q + K * K);
  81. a0 = K / Q * norm;
  82. a1 = 0;
  83. a2 = -a0;
  84. b1 = 2 * (K * K - 1) * norm;
  85. b2 = (1 - K / Q + K * K) * norm;
  86. break;
  87. case bq_type_notch:
  88. norm = 1 / (1 + K / Q + K * K);
  89. a0 = (1 + K * K) * norm;
  90. a1 = 2 * (K * K - 1) * norm;
  91. a2 = a0;
  92. b1 = a1;
  93. b2 = (1 - K / Q + K * K) * norm;
  94. break;
  95. case bq_type_peak:
  96. if (peakGain >= 0) { // boost
  97. norm = 1 / (1 + 1 / Q * K + K * K);
  98. a0 = (1 + V / Q * K + K * K) * norm;
  99. a1 = 2 * (K * K - 1) * norm;
  100. a2 = (1 - V / Q * K + K * K) * norm;
  101. b1 = a1;
  102. b2 = (1 - 1 / Q * K + K * K) * norm;
  103. }
  104. else { // cut
  105. norm = 1 / (1 + V / Q * K + K * K);
  106. a0 = (1 + 1 / Q * K + K * K) * norm;
  107. a1 = 2 * (K * K - 1) * norm;
  108. a2 = (1 - 1 / Q * K + K * K) * norm;
  109. b1 = a1;
  110. b2 = (1 - V / Q * K + K * K) * norm;
  111. }
  112. break;
  113. case bq_type_lowshelf:
  114. if (peakGain >= 0) { // boost
  115. norm = 1 / (1 + sqrt(2) * K + K * K);
  116. a0 = (1 + sqrt(2 * V) * K + V * K * K) * norm;
  117. a1 = 2 * (V * K * K - 1) * norm;
  118. a2 = (1 - sqrt(2 * V) * K + V * K * K) * norm;
  119. b1 = 2 * (K * K - 1) * norm;
  120. b2 = (1 - sqrt(2) * K + K * K) * norm;
  121. }
  122. else { // cut
  123. norm = 1 / (1 + sqrt(2 * V) * K + V * K * K);
  124. a0 = (1 + sqrt(2) * K + K * K) * norm;
  125. a1 = 2 * (K * K - 1) * norm;
  126. a2 = (1 - sqrt(2) * K + K * K) * norm;
  127. b1 = 2 * (V * K * K - 1) * norm;
  128. b2 = (1 - sqrt(2 * V) * K + V * K * K) * norm;
  129. }
  130. break;
  131. case bq_type_highshelf:
  132. if (peakGain >= 0) { // boost
  133. norm = 1 / (1 + sqrt(2) * K + K * K);
  134. a0 = (V + sqrt(2 * V) * K + K * K) * norm;
  135. a1 = 2 * (K * K - V) * norm;
  136. a2 = (V - sqrt(2 * V) * K + K * K) * norm;
  137. b1 = 2 * (K * K - 1) * norm;
  138. b2 = (1 - sqrt(2) * K + K * K) * norm;
  139. }
  140. else { // cut
  141. norm = 1 / (V + sqrt(2 * V) * K + K * K);
  142. a0 = (1 + sqrt(2) * K + K * K) * norm;
  143. a1 = 2 * (K * K - 1) * norm;
  144. a2 = (1 - sqrt(2) * K + K * K) * norm;
  145. b1 = 2 * (K * K - V) * norm;
  146. b2 = (V - sqrt(2 * V) * K + K * K) * norm;
  147. }
  148. break;
  149. }
  150. return;
  151. }
  152. } // namespace rack_plugin_Autodafe