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.

window.hpp 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 0.5f * (1.f - simd::cos(2*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.f
  27. - 1 / 2.f * simd::cos(2*M_PI * p)
  28. + alpha / 2.f * simd::cos(4*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. + 0.3635819f
  42. - 0.4891775f * simd::cos(2*M_PI * p)
  43. + 0.1365995f * simd::cos(4*M_PI * p)
  44. - 0.0106411f * simd::cos(6*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. + 0.35875f
  58. - 0.48829f * simd::cos(2*M_PI * p)
  59. + 0.14128f * simd::cos(4*M_PI * p)
  60. - 0.01168f * simd::cos(6*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