Biquad.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "Biquad.h"
  2. #include <cmath> // for pow, cosf, sinf, M_PI, sqrtf, tanf, logf, sinh
  3. using namespace bell;
  4. Biquad::Biquad() {
  5. this->filterType = "biquad";
  6. }
  7. void Biquad::sampleRateChanged(uint32_t sampleRate) {
  8. this->sampleRate = sampleRate;
  9. //this->configure(this->type, this->currentConfig);
  10. }
  11. void Biquad::configure(Type type, std::map<std::string, float>& newConf) {
  12. this->type = type;
  13. this->currentConfig = newConf;
  14. switch (type) {
  15. case Type::Free:
  16. coeffs[0] = newConf["a1"];
  17. coeffs[1] = newConf["a2"];
  18. coeffs[2] = newConf["b0"];
  19. coeffs[3] = newConf["b1"];
  20. coeffs[4] = newConf["b2"];
  21. break;
  22. case Type::Highpass:
  23. highPassCoEffs(newConf["freq"], newConf["q"]);
  24. break;
  25. case Type::HighpassFO:
  26. highPassFOCoEffs(newConf["freq"]);
  27. break;
  28. case Type::Lowpass:
  29. lowPassCoEffs(newConf["freq"], newConf["q"]);
  30. break;
  31. case Type::LowpassFO:
  32. lowPassFOCoEffs(newConf["freq"]);
  33. break;
  34. case Type::Highshelf:
  35. // check if config has slope key
  36. if (newConf.find("slope") != newConf.end()) {
  37. highShelfCoEffsSlope(newConf["freq"], newConf["gain"],
  38. newConf["slope"]);
  39. } else {
  40. highShelfCoEffs(newConf["freq"], newConf["gain"], newConf["q"]);
  41. }
  42. break;
  43. case Type::HighshelfFO:
  44. highShelfFOCoEffs(newConf["freq"], newConf["gain"]);
  45. break;
  46. case Type::Lowshelf:
  47. // check if config has slope key
  48. if (newConf.find("slope") != newConf.end()) {
  49. lowShelfCoEffsSlope(newConf["freq"], newConf["gain"], newConf["slope"]);
  50. } else {
  51. lowShelfCoEffs(newConf["freq"], newConf["gain"], newConf["q"]);
  52. }
  53. break;
  54. case Type::LowshelfFO:
  55. lowShelfFOCoEffs(newConf["freq"], newConf["gain"]);
  56. break;
  57. case Type::Peaking:
  58. // check if config has bandwidth key
  59. if (newConf.find("bandwidth") != newConf.end()) {
  60. peakCoEffsBandwidth(newConf["freq"], newConf["gain"],
  61. newConf["bandwidth"]);
  62. } else {
  63. peakCoEffs(newConf["freq"], newConf["gain"], newConf["q"]);
  64. }
  65. break;
  66. case Type::Notch:
  67. if (newConf.find("bandwidth") != newConf.end()) {
  68. notchCoEffsBandwidth(newConf["freq"], newConf["gain"],
  69. newConf["bandwidth"]);
  70. } else {
  71. notchCoEffs(newConf["freq"], newConf["gain"], newConf["q"]);
  72. }
  73. break;
  74. case Type::Bandpass:
  75. if (newConf.find("bandwidth") != newConf.end()) {
  76. bandPassCoEffsBandwidth(newConf["freq"], newConf["bandwidth"]);
  77. } else {
  78. bandPassCoEffs(newConf["freq"], newConf["q"]);
  79. }
  80. break;
  81. case Type::Allpass:
  82. if (newConf.find("bandwidth") != newConf.end()) {
  83. allPassCoEffsBandwidth(newConf["freq"], newConf["bandwidth"]);
  84. } else {
  85. allPassCoEffs(newConf["freq"], newConf["q"]);
  86. }
  87. break;
  88. case Type::AllpassFO:
  89. allPassFOCoEffs(newConf["freq"]);
  90. break;
  91. }
  92. }
  93. // coefficients for a high pass biquad filter
  94. void Biquad::highPassCoEffs(float f, float q) {
  95. float w0 = 2 * M_PI * f / this->sampleRate;
  96. float c = cosf(w0);
  97. float s = sinf(w0);
  98. float alpha = s / (2 * q);
  99. float b0 = (1 + c) / 2;
  100. float b1 = -(1 + c);
  101. float b2 = b0;
  102. float a0 = 1 + alpha;
  103. float a1 = -2 * c;
  104. float a2 = 1 - alpha;
  105. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  106. }
  107. // coefficients for a high pass first order biquad filter
  108. void Biquad::highPassFOCoEffs(float f) {
  109. float w0 = 2 * M_PI * f / this->sampleRate;
  110. float k = tanf(w0 / 2.0);
  111. float alpha = 1.0 + k;
  112. float b0 = 1.0 / alpha;
  113. float b1 = -1.0 / alpha;
  114. float b2 = 0.0;
  115. float a0 = 1.0;
  116. float a1 = -(1.0 - k) / alpha;
  117. float a2 = 0.0;
  118. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  119. }
  120. // coefficients for a low pass biquad filter
  121. void Biquad::lowPassCoEffs(float f, float q) {
  122. float w0 = 2 * M_PI * f / this->sampleRate;
  123. float c = cosf(w0);
  124. float s = sinf(w0);
  125. float alpha = s / (2 * q);
  126. float b0 = (1 - c) / 2;
  127. float b1 = 1 - c;
  128. float b2 = b0;
  129. float a0 = 1 + alpha;
  130. float a1 = -2 * c;
  131. float a2 = 1 - alpha;
  132. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  133. }
  134. // coefficients for a low pass first order biquad filter
  135. void Biquad::lowPassFOCoEffs(float f) {
  136. float w0 = 2 * M_PI * f / this->sampleRate;
  137. float k = tanf(w0 / 2.0);
  138. float alpha = 1.0 + k;
  139. float b0 = k / alpha;
  140. float b1 = k / alpha;
  141. float b2 = 0.0;
  142. float a0 = 1.0;
  143. float a1 = -(1.0 - k) / alpha;
  144. float a2 = 0.0;
  145. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  146. }
  147. // coefficients for a peak biquad filter
  148. void Biquad::peakCoEffs(float f, float gain, float q) {
  149. float w0 = 2 * M_PI * f / this->sampleRate;
  150. float c = cosf(w0);
  151. float s = sinf(w0);
  152. float alpha = s / (2 * q);
  153. float ampl = std::pow(10.0f, gain / 40.0f);
  154. float b0 = 1.0 + (alpha * ampl);
  155. float b1 = -2.0 * c;
  156. float b2 = 1.0 - (alpha * ampl);
  157. float a0 = 1 + (alpha / ampl);
  158. float a1 = -2 * c;
  159. float a2 = 1 - (alpha / ampl);
  160. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  161. }
  162. void Biquad::peakCoEffsBandwidth(float f, float gain, float bandwidth) {
  163. float w0 = 2 * M_PI * f / this->sampleRate;
  164. float c = cosf(w0);
  165. float s = sinf(w0);
  166. float alpha = s * sinh(logf(2.0) / 2.0 * bandwidth * w0 / s);
  167. float ampl = std::pow(10.0f, gain / 40.0f);
  168. float b0 = 1.0 + (alpha * ampl);
  169. float b1 = -2.0 * c;
  170. float b2 = 1.0 - (alpha * ampl);
  171. float a0 = 1 + (alpha / ampl);
  172. float a1 = -2 * c;
  173. float a2 = 1 - (alpha / ampl);
  174. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  175. }
  176. void Biquad::highShelfCoEffs(float f, float gain, float q) {
  177. float A = std::pow(10.0f, gain / 40.0f);
  178. float w0 = 2 * M_PI * f / this->sampleRate;
  179. float c = cosf(w0);
  180. float s = sinf(w0);
  181. float alpha = s / (2 * q);
  182. float beta = s * sqrtf(A) / q;
  183. float b0 = A * ((A + 1.0) + (A - 1.0) * c + beta);
  184. float b1 = -2.0 * A * ((A - 1.0) + (A + 1.0) * c);
  185. float b2 = A * ((A + 1.0) + (A - 1.0) * c - beta);
  186. float a0 = (A + 1.0) - (A - 1.0) * c + beta;
  187. float a1 = 2.0 * ((A - 1.0) - (A + 1.0) * c);
  188. float a2 = (A + 1.0) - (A - 1.0) * c - beta;
  189. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  190. }
  191. void Biquad::highShelfCoEffsSlope(float f, float gain, float slope) {
  192. float A = std::pow(10.0f, gain / 40.0f);
  193. float w0 = 2 * M_PI * f / this->sampleRate;
  194. float c = cosf(w0);
  195. float s = sinf(w0);
  196. float alpha =
  197. s / 2.0 * sqrtf((A + 1.0 / A) * (1.0 / (slope / 12.0) - 1.0) + 2.0);
  198. float beta = 2.0 * sqrtf(A) * alpha;
  199. float b0 = A * ((A + 1.0) + (A - 1.0) * c + beta);
  200. float b1 = -2.0 * A * ((A - 1.0) + (A + 1.0) * c);
  201. float b2 = A * ((A + 1.0) + (A - 1.0) * c - beta);
  202. float a0 = (A + 1.0) - (A - 1.0) * c + beta;
  203. float a1 = 2.0 * ((A - 1.0) - (A + 1.0) * c);
  204. float a2 = (A + 1.0) - (A - 1.0) * c - beta;
  205. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  206. }
  207. void Biquad::highShelfFOCoEffs(float f, float gain) {
  208. float A = std::pow(10.0f, gain / 40.0f);
  209. float w0 = 2 * M_PI * f / this->sampleRate;
  210. float tn = tanf(w0 / 2.0);
  211. float b0 = A * tn + std::pow(A, 2);
  212. float b1 = A * tn - std::pow(A, 2);
  213. float b2 = 0.0;
  214. float a0 = A * tn + 1.0;
  215. float a1 = A * tn - 1.0;
  216. float a2 = 0.0;
  217. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  218. }
  219. void Biquad::lowShelfCoEffs(float f, float gain, float q) {
  220. float A = std::pow(10.0f, gain / 40.0f);
  221. float w0 = 2 * M_PI * f / this->sampleRate;
  222. float c = cosf(w0);
  223. float s = sinf(w0);
  224. float beta = s * sqrtf(A) / q;
  225. float b0 = A * ((A + 1.0) - (A - 1.0) * c + beta);
  226. float b1 = 2.0 * A * ((A - 1.0) - (A + 1.0) * c);
  227. float b2 = A * ((A + 1.0) - (A - 1.0) * c - beta);
  228. float a0 = (A + 1.0) + (A - 1.0) * c + beta;
  229. float a1 = -2.0 * ((A - 1.0) + (A + 1.0) * c);
  230. float a2 = (A + 1.0) + (A - 1.0) * c - beta;
  231. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  232. }
  233. void Biquad::lowShelfCoEffsSlope(float f, float gain, float slope) {
  234. float A = std::pow(10.0f, gain / 40.0f);
  235. float w0 = 2 * M_PI * f / this->sampleRate;
  236. float c = cosf(w0);
  237. float s = sinf(w0);
  238. float alpha =
  239. s / 2.0 * sqrtf((A + 1.0 / A) * (1.0 / (slope / 12.0) - 1.0) + 2.0);
  240. float beta = 2.0 * sqrtf(A) * alpha;
  241. float b0 = A * ((A + 1.0) - (A - 1.0) * c + beta);
  242. float b1 = 2.0 * A * ((A - 1.0) - (A + 1.0) * c);
  243. float b2 = A * ((A + 1.0) - (A - 1.0) * c - beta);
  244. float a0 = (A + 1.0) + (A - 1.0) * c + beta;
  245. float a1 = -2.0 * ((A - 1.0) + (A + 1.0) * c);
  246. float a2 = (A + 1.0) + (A - 1.0) * c - beta;
  247. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  248. }
  249. void Biquad::lowShelfFOCoEffs(float f, float gain) {
  250. float A = std::pow(10.0f, gain / 40.0f);
  251. float w0 = 2 * M_PI * f / this->sampleRate;
  252. float tn = tanf(w0 / 2.0);
  253. float b0 = std::pow(A, 2) * tn + A;
  254. float b1 = std::pow(A, 2) * tn - A;
  255. float b2 = 0.0;
  256. float a0 = tn + A;
  257. float a1 = tn - A;
  258. float a2 = 0.0;
  259. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  260. }
  261. void Biquad::notchCoEffs(float f, float gain, float q) {
  262. float A = std::pow(10.0f, gain / 40.0f);
  263. float w0 = 2 * M_PI * f / this->sampleRate;
  264. float c = cosf(w0);
  265. float s = sinf(w0);
  266. float alpha = s / (2.0 * q);
  267. float b0 = 1.0;
  268. float b1 = -2.0 * c;
  269. float b2 = 1.0;
  270. float a0 = 1.0 + alpha;
  271. float a1 = -2.0 * c;
  272. float a2 = 1.0 - alpha;
  273. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  274. }
  275. void Biquad::notchCoEffsBandwidth(float f, float gain, float bandwidth) {
  276. float A = std::pow(10.0f, gain / 40.0f);
  277. float w0 = 2 * M_PI * f / this->sampleRate;
  278. float c = cosf(w0);
  279. float s = sinf(w0);
  280. float alpha = s * sinh(logf(2.0) / 2.0 * bandwidth * w0 / s);
  281. float b0 = 1.0;
  282. float b1 = -2.0 * c;
  283. float b2 = 1.0;
  284. float a0 = 1.0 + alpha;
  285. float a1 = -2.0 * c;
  286. float a2 = 1.0 - alpha;
  287. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  288. }
  289. void Biquad::bandPassCoEffs(float f, float q) {
  290. float w0 = 2 * M_PI * f / this->sampleRate;
  291. float c = cosf(w0);
  292. float s = sinf(w0);
  293. float alpha = s / (2.0 * q);
  294. float b0 = alpha;
  295. float b1 = 0.0;
  296. float b2 = -alpha;
  297. float a0 = 1.0 + alpha;
  298. float a1 = -2.0 * c;
  299. float a2 = 1.0 - alpha;
  300. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  301. }
  302. void Biquad::bandPassCoEffsBandwidth(float f, float bandwidth) {
  303. float w0 = 2 * M_PI * f / this->sampleRate;
  304. float c = cosf(w0);
  305. float s = sinf(w0);
  306. float alpha = s * sinh(logf(2.0) / 2.0 * bandwidth * w0 / s);
  307. float b0 = alpha;
  308. float b1 = 0.0;
  309. float b2 = -alpha;
  310. float a0 = 1.0 + alpha;
  311. float a1 = -2.0 * c;
  312. float a2 = 1.0 - alpha;
  313. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  314. }
  315. void Biquad::allPassCoEffs(float f, float q) {
  316. float w0 = 2 * M_PI * f / this->sampleRate;
  317. float c = cosf(w0);
  318. float s = sinf(w0);
  319. float alpha = s / (2.0 * q);
  320. float b0 = 1.0 - alpha;
  321. float b1 = -2.0 * c;
  322. float b2 = 1.0 + alpha;
  323. float a0 = 1.0 + alpha;
  324. float a1 = -2.0 * c;
  325. float a2 = 1.0 - alpha;
  326. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  327. }
  328. void Biquad::allPassCoEffsBandwidth(float f, float bandwidth) {
  329. float w0 = 2 * M_PI * f / this->sampleRate;
  330. float c = cosf(w0);
  331. float s = sinf(w0);
  332. float alpha = s * sinh(logf(2.0) / 2.0 * bandwidth * w0 / s);
  333. float b0 = 1.0 - alpha;
  334. float b1 = -2.0 * c;
  335. float b2 = 1.0 + alpha;
  336. float a0 = 1.0 + alpha;
  337. float a1 = -2.0 * c;
  338. float a2 = 1.0 - alpha;
  339. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  340. }
  341. void Biquad::allPassFOCoEffs(float f) {
  342. float w0 = 2 * M_PI * f / this->sampleRate;
  343. float tn = tanf(w0 / 2.0);
  344. float alpha = (tn + 1.0) / (tn - 1.0);
  345. float b0 = 1.0;
  346. float b1 = alpha;
  347. float b2 = 0.0;
  348. float a0 = alpha;
  349. float a1 = 1.0;
  350. float a2 = 0.0;
  351. this->normalizeCoEffs(a0, a1, a2, b0, b1, b2);
  352. }
  353. void Biquad::normalizeCoEffs(float a0, float a1, float a2, float b0, float b1,
  354. float b2) {
  355. coeffs[0] = b0 / a0;
  356. coeffs[1] = b1 / a0;
  357. coeffs[2] = b2 / a0;
  358. coeffs[3] = a1 / a0;
  359. coeffs[4] = a2 / a0;
  360. }
  361. std::unique_ptr<StreamInfo> Biquad::process(
  362. std::unique_ptr<StreamInfo> stream) {
  363. std::scoped_lock lock(accessMutex);
  364. auto input = stream->data[this->channel];
  365. auto numSamples = stream->numSamples;
  366. #ifdef ESP_PLATFORM
  367. dsps_biquad_f32_ae32(input, input, numSamples, coeffs, w);
  368. #else
  369. // Apply the set coefficients
  370. for (int i = 0; i < numSamples; i++) {
  371. float d0 = input[i] - coeffs[3] * w[0] - coeffs[4] * w[1];
  372. input[i] = coeffs[0] * d0 + coeffs[1] * w[0] + coeffs[2] * w[1];
  373. w[1] = w[0];
  374. w[0] = d0;
  375. }
  376. #endif
  377. return stream;
  378. };