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.

83 lines
1.9KB

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