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.

374 lines
7.9KB

  1. #pragma once
  2. #include <dsp/common.hpp>
  3. namespace rack {
  4. namespace dsp {
  5. /** The simplest possible analog filter using an Euler solver.
  6. https://en.wikipedia.org/wiki/RC_circuit
  7. Use two RC filters in series for a bandpass filter.
  8. */
  9. template <typename T = float>
  10. struct TRCFilter {
  11. T c = 0.f;
  12. T xstate[1];
  13. T ystate[1];
  14. TRCFilter() {
  15. reset();
  16. }
  17. void reset() {
  18. xstate[0] = 0.f;
  19. ystate[0] = 0.f;
  20. }
  21. /** Sets the cutoff angular frequency in radians.
  22. */
  23. void setCutoff(T r) {
  24. c = 2.f / r;
  25. }
  26. /** Sets the cutoff frequency.
  27. `f` is the ratio between the cutoff frequency and sample rate, i.e. f = f_c / f_s
  28. */
  29. void setCutoffFreq(T f) {
  30. setCutoff(2.f * M_PI * f);
  31. }
  32. void process(T x) {
  33. T y = (x + xstate[0] - ystate[0] * (1 - c)) / (1 + c);
  34. xstate[0] = x;
  35. ystate[0] = y;
  36. }
  37. T lowpass() {
  38. return ystate[0];
  39. }
  40. T highpass() {
  41. return xstate[0] - ystate[0];
  42. }
  43. };
  44. typedef TRCFilter<> RCFilter;
  45. /** Applies exponential smoothing to a signal with the ODE
  46. \f$ \frac{dy}{dt} = x \lambda \f$.
  47. */
  48. template <typename T = float>
  49. struct TExponentialFilter {
  50. T out = 0.f;
  51. T lambda = 0.f;
  52. void reset() {
  53. out = 0.f;
  54. }
  55. void setLambda(T lambda) {
  56. this->lambda = lambda;
  57. }
  58. T process(T deltaTime, T in) {
  59. T y = out + (in - out) * lambda * deltaTime;
  60. // If no change was made between the old and new output, assume T granularity is too small and snap output to input
  61. out = simd::ifelse(out == y, in, y);
  62. return out;
  63. }
  64. DEPRECATED T process(T in) {
  65. return process(1.f, in);
  66. }
  67. };
  68. typedef TExponentialFilter<> ExponentialFilter;
  69. /** Like ExponentialFilter but jumps immediately to higher values.
  70. */
  71. template <typename T = float>
  72. struct TPeakFilter {
  73. T out = 0.f;
  74. T lambda = 0.f;
  75. void reset() {
  76. out = 0.f;
  77. }
  78. void setLambda(T lambda) {
  79. this->lambda = lambda;
  80. }
  81. T process(T deltaTime, T in) {
  82. T y = out + (in - out) * lambda * deltaTime;
  83. out = simd::fmax(y, in);
  84. return out;
  85. }
  86. /** Use the return value of process() instead. */
  87. DEPRECATED T peak() {
  88. return out;
  89. }
  90. /** Use setLambda() instead. */
  91. DEPRECATED void setRate(T r) {
  92. lambda = 1.f - r;
  93. }
  94. DEPRECATED T process(T x) {
  95. return process(1.f, x);
  96. }
  97. };
  98. typedef TPeakFilter<> PeakFilter;
  99. template <typename T = float>
  100. struct TSlewLimiter {
  101. T out = 0.f;
  102. T rise = 0.f;
  103. T fall = 0.f;
  104. void reset() {
  105. out = 0.f;
  106. }
  107. void setRiseFall(T rise, T fall) {
  108. this->rise = rise;
  109. this->fall = fall;
  110. }
  111. T process(T deltaTime, T in) {
  112. out = simd::clamp(in, out - fall * deltaTime, out + rise * deltaTime);
  113. return out;
  114. }
  115. DEPRECATED T process(T in) {
  116. return process(1.f, in);
  117. }
  118. };
  119. typedef TSlewLimiter<> SlewLimiter;
  120. template <typename T = float>
  121. struct TExponentialSlewLimiter {
  122. T out = 0.f;
  123. T riseLambda = 0.f;
  124. T fallLambda = 0.f;
  125. void reset() {
  126. out = 0.f;
  127. }
  128. void setRiseFall(T riseLambda, T fallLambda) {
  129. this->riseLambda = riseLambda;
  130. this->fallLambda = fallLambda;
  131. }
  132. T process(T deltaTime, T in) {
  133. T lambda = simd::ifelse(in > out, riseLambda, fallLambda);
  134. T y = out + (in - out) * lambda * deltaTime;
  135. // If the change from the old out to the new out is too small for floats, set `in` directly.
  136. out = simd::ifelse(out == y, in, y);
  137. return out;
  138. }
  139. DEPRECATED T process(T in) {
  140. return process(1.f, in);
  141. }
  142. };
  143. typedef TExponentialSlewLimiter<> ExponentialSlewLimiter;
  144. template <typename T = float>
  145. struct TBiquadFilter {
  146. /** input state */
  147. T x[2];
  148. /** output state */
  149. T y[2];
  150. /** transfer function denominator coefficients: a_1, a_2
  151. a_0 is fixed to 1.
  152. */
  153. float a[2];
  154. /** transfer function numerator coefficients: b_0, b_1, b_2 */
  155. float b[3];
  156. enum Type {
  157. LOWPASS_1POLE,
  158. HIGHPASS_1POLE,
  159. LOWPASS,
  160. HIGHPASS,
  161. LOWSHELF,
  162. HIGHSHELF,
  163. BANDPASS,
  164. PEAK,
  165. NOTCH,
  166. NUM_TYPES
  167. };
  168. Type type = LOWPASS;
  169. TBiquadFilter() {
  170. reset();
  171. setParameters(0.f, 0.f, 1.f);
  172. }
  173. void reset() {
  174. std::memset(x, 0, sizeof(x));
  175. std::memset(y, 0, sizeof(y));
  176. }
  177. T process(T in) {
  178. // Advance IIR
  179. T out = b[0] * in + b[1] * x[0] + b[2] * x[1]
  180. - a[0] * y[0] - a[1] * y[1];
  181. // Push input
  182. x[1] = x[0];
  183. x[0] = in;
  184. // Push output
  185. y[1] = y[0];
  186. y[0] = out;
  187. return out;
  188. }
  189. /** Calculates and sets the biquad transfer function coefficients.
  190. f: normalized frequency (cutoff frequency / sample rate), must be less than 0.5
  191. Q: quality factor
  192. V: gain
  193. */
  194. void setParameters(float f, float Q, float V) {
  195. float K = std::tan(M_PI * f);
  196. switch (type) {
  197. case LOWPASS_1POLE: {
  198. a[0] = -std::exp(-2.f * M_PI * f);
  199. a[1] = 0.f;
  200. b[0] = 1.f + a[0];
  201. b[1] = 0.f;
  202. b[2] = 0.f;
  203. } break;
  204. case HIGHPASS_1POLE: {
  205. a[0] = std::exp(-2.f * M_PI * (0.5f - f));
  206. a[1] = 0.f;
  207. b[0] = 1.f - a[0];
  208. b[1] = 0.f;
  209. b[2] = 0.f;
  210. } break;
  211. case LOWPASS: {
  212. float norm = 1.f / (1.f + K / Q + K * K);
  213. b[0] = K * K * norm;
  214. b[1] = 2.f * b[0];
  215. b[2] = b[0];
  216. a[0] = 2.f * (K * K - 1.f) * norm;
  217. a[1] = (1.f - K / Q + K * K) * norm;
  218. } break;
  219. case HIGHPASS: {
  220. float norm = 1.f / (1.f + K / Q + K * K);
  221. b[0] = norm;
  222. b[1] = -2.f * b[0];
  223. b[2] = b[0];
  224. a[0] = 2.f * (K * K - 1.f) * norm;
  225. a[1] = (1.f - K / Q + K * K) * norm;
  226. } break;
  227. case LOWSHELF: {
  228. float sqrtV = std::sqrt(V);
  229. if (V >= 1.f) {
  230. float norm = 1.f / (1.f + M_SQRT2 * K + K * K);
  231. b[0] = (1.f + M_SQRT2 * sqrtV * K + V * K * K) * norm;
  232. b[1] = 2.f * (V * K * K - 1.f) * norm;
  233. b[2] = (1.f - M_SQRT2 * sqrtV * K + V * K * K) * norm;
  234. a[0] = 2.f * (K * K - 1.f) * norm;
  235. a[1] = (1.f - M_SQRT2 * K + K * K) * norm;
  236. }
  237. else {
  238. float norm = 1.f / (1.f + M_SQRT2 / sqrtV * K + K * K / V);
  239. b[0] = (1.f + M_SQRT2 * K + K * K) * norm;
  240. b[1] = 2.f * (K * K - 1) * norm;
  241. b[2] = (1.f - M_SQRT2 * K + K * K) * norm;
  242. a[0] = 2.f * (K * K / V - 1.f) * norm;
  243. a[1] = (1.f - M_SQRT2 / sqrtV * K + K * K / V) * norm;
  244. }
  245. } break;
  246. case HIGHSHELF: {
  247. float sqrtV = std::sqrt(V);
  248. if (V >= 1.f) {
  249. float norm = 1.f / (1.f + M_SQRT2 * K + K * K);
  250. b[0] = (V + M_SQRT2 * sqrtV * K + K * K) * norm;
  251. b[1] = 2.f * (K * K - V) * norm;
  252. b[2] = (V - M_SQRT2 * sqrtV * K + K * K) * norm;
  253. a[0] = 2.f * (K * K - 1.f) * norm;
  254. a[1] = (1.f - M_SQRT2 * K + K * K) * norm;
  255. }
  256. else {
  257. float norm = 1.f / (1.f / V + M_SQRT2 / sqrtV * K + K * K);
  258. b[0] = (1.f + M_SQRT2 * K + K * K) * norm;
  259. b[1] = 2.f * (K * K - 1.f) * norm;
  260. b[2] = (1.f - M_SQRT2 * K + K * K) * norm;
  261. a[0] = 2.f * (K * K - 1.f / V) * norm;
  262. a[1] = (1.f / V - M_SQRT2 / sqrtV * K + K * K) * norm;
  263. }
  264. } break;
  265. case BANDPASS: {
  266. float norm = 1.f / (1.f + K / Q + K * K);
  267. b[0] = K / Q * norm;
  268. b[1] = 0.f;
  269. b[2] = -b[0];
  270. a[0] = 2.f * (K * K - 1.f) * norm;
  271. a[1] = (1.f - K / Q + K * K) * norm;
  272. } break;
  273. case PEAK: {
  274. if (V >= 1.f) {
  275. float norm = 1.f / (1.f + K / Q + K * K);
  276. b[0] = (1.f + K / Q * V + K * K) * norm;
  277. b[1] = 2.f * (K * K - 1.f) * norm;
  278. b[2] = (1.f - K / Q * V + K * K) * norm;
  279. a[0] = b[1];
  280. a[1] = (1.f - K / Q + K * K) * norm;
  281. }
  282. else {
  283. float norm = 1.f / (1.f + K / Q / V + K * K);
  284. b[0] = (1.f + K / Q + K * K) * norm;
  285. b[1] = 2.f * (K * K - 1.f) * norm;
  286. b[2] = (1.f - K / Q + K * K) * norm;
  287. a[0] = b[1];
  288. a[1] = (1.f - K / Q / V + K * K) * norm;
  289. }
  290. } break;
  291. case NOTCH: {
  292. float norm = 1.f / (1.f + K / Q + K * K);
  293. b[0] = (1.f + K * K) * norm;
  294. b[1] = 2.f * (K * K - 1.f) * norm;
  295. b[2] = b[0];
  296. a[0] = b[1];
  297. a[1] = (1.f - K / Q + K * K) * norm;
  298. } break;
  299. default: break;
  300. }
  301. }
  302. /** Computes the gain of a particular frequency
  303. f: normalized frequency
  304. */
  305. float getFrequencyResponse(float f) {
  306. // Compute sum(a_k e^(-i k f))
  307. std::complex<float> bsum = b[0];
  308. std::complex<float> asum = 1.f;
  309. for (int i = 1; i < 3; i++) {
  310. float p = 2 * M_PI * -i * f;
  311. std::complex<float> e(std::cos(p), std::sin(p));
  312. bsum += b[i] * e;
  313. asum += a[i - 1] * e;
  314. }
  315. return std::abs(bsum / asum);
  316. }
  317. };
  318. typedef TBiquadFilter<> BiquadFilter;
  319. } // namespace dsp
  320. } // namespace rack