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.

85 lines
2.0KB

  1. #pragma once
  2. #include <math.hpp>
  3. #include <simd/Vector.hpp>
  4. #include <simd/functions.hpp>
  5. namespace rack {
  6. namespace dsp {
  7. /** Hann window function.
  8. p: proportion from [0, 1], usually `i / (len - 1)`
  9. https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
  10. */
  11. template <typename T>
  12. inline T hann(T p) {
  13. return T(0.5) * (1 - simd::cos(2 * T(M_PI) * p));
  14. }
  15. /** Multiplies the Hann window by a signal `x` of length `len` in-place. */
  16. inline void hannWindow(float* x, int len) {
  17. for (int i = 0; i < len; i++) {
  18. x[i] *= hann(float(i) / (len - 1));
  19. }
  20. }
  21. /** Blackman window function.
  22. https://en.wikipedia.org/wiki/Window_function#Blackman_window
  23. A typical alpha value is 0.16.
  24. */
  25. template <typename T>
  26. inline T blackman(T alpha, T p) {
  27. return
  28. + (1 - alpha) / 2
  29. - T(1) / 2 * simd::cos(2 * T(M_PI) * p)
  30. + alpha / 2 * simd::cos(4 * T(M_PI) * p);
  31. }
  32. inline void blackmanWindow(float alpha, float* x, int len) {
  33. for (int i = 0; i < len; i++) {
  34. x[i] *= blackman(alpha, float(i) / (len - 1));
  35. }
  36. }
  37. /** Blackman-Nuttall window function.
  38. https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Nuttall_window
  39. */
  40. template <typename T>
  41. inline T blackmanNuttall(T p) {
  42. return
  43. + T(0.3635819)
  44. - T(0.4891775) * simd::cos(2 * T(M_PI) * p)
  45. + T(0.1365995) * simd::cos(4 * T(M_PI) * p)
  46. - T(0.0106411) * simd::cos(6 * T(M_PI) * p);
  47. }
  48. inline void blackmanNuttallWindow(float* x, int len) {
  49. for (int i = 0; i < len; i++) {
  50. x[i] *= blackmanNuttall(float(i) / (len - 1));
  51. }
  52. }
  53. /** Blackman-Harris window function.
  54. https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Harris_window
  55. */
  56. template <typename T>
  57. inline T blackmanHarris(T p) {
  58. return
  59. + T(0.35875)
  60. - T(0.48829) * simd::cos(2 * T(M_PI) * p)
  61. + T(0.14128) * simd::cos(4 * T(M_PI) * p)
  62. - T(0.01168) * simd::cos(6 * T(M_PI) * p);
  63. }
  64. inline void blackmanHarrisWindow(float* x, int len) {
  65. for (int i = 0; i < len; i++) {
  66. x[i] *= blackmanHarris(float(i) / (len - 1));
  67. }
  68. }
  69. } // namespace dsp
  70. } // namespace rack