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.

79 lines
1.8KB

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