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.

129 lines
3.2KB

  1. #pragma once
  2. #include <pffft.h>
  3. #include <dsp/common.hpp>
  4. namespace rack {
  5. namespace dsp {
  6. /** Real-valued FFT context.
  7. Wrapper for [PFFFT](https://bitbucket.org/jpommier/pffft/)
  8. `length` must be a multiple of 32.
  9. Buffers must be aligned to 16-byte boundaries. new[] and malloc() do this for you.
  10. */
  11. struct RealFFT {
  12. PFFFT_Setup* setup;
  13. int length;
  14. RealFFT(size_t length) {
  15. this->length = length;
  16. setup = pffft_new_setup(length, PFFFT_REAL);
  17. }
  18. ~RealFFT() {
  19. pffft_destroy_setup(setup);
  20. }
  21. /** Performs the real FFT.
  22. Input and output must be aligned using the above align*() functions.
  23. Input is `length` elements. Output is `2*length` elements.
  24. Output is arbitrarily ordered for performance reasons.
  25. However, this ordering is consistent, so element-wise multiplication with line up with other results, and the inverse FFT will return a correctly ordered result.
  26. */
  27. void rfftUnordered(const float* input, float* output) {
  28. pffft_transform(setup, input, output, NULL, PFFFT_FORWARD);
  29. }
  30. /** Performs the inverse real FFT.
  31. Input is `2*length` elements. Output is `length` elements.
  32. Scaling is such that IRFFT(RFFT(x)) = N*x.
  33. */
  34. void irfftUnordered(const float* input, float* output) {
  35. pffft_transform(setup, input, output, NULL, PFFFT_BACKWARD);
  36. }
  37. /** Slower than the above methods, but returns results in the "canonical" FFT order as follows.
  38. output[0] = F(0)
  39. output[1] = F(n/2)
  40. output[2] = real(F(1))
  41. output[3] = imag(F(1))
  42. output[4] = real(F(2))
  43. output[5] = imag(F(2))
  44. ...
  45. output[length - 2] = real(F(n/2 - 1))
  46. output[length - 1] = imag(F(n/2 - 1))
  47. */
  48. void rfft(const float* input, float* output) {
  49. pffft_transform_ordered(setup, input, output, NULL, PFFFT_FORWARD);
  50. }
  51. void irfft(const float* input, float* output) {
  52. pffft_transform_ordered(setup, input, output, NULL, PFFFT_BACKWARD);
  53. }
  54. /** Scales the RFFT so that `scale(IFFT(FFT(x))) = x`.
  55. */
  56. void scale(float* x) {
  57. float a = 1.f / length;
  58. for (int i = 0; i < length; i++) {
  59. x[i] *= a;
  60. }
  61. }
  62. };
  63. /** Complex-valued FFT context.
  64. `length` must be a multiple of 16.
  65. */
  66. struct ComplexFFT {
  67. PFFFT_Setup* setup;
  68. int length;
  69. ComplexFFT(size_t length) {
  70. this->length = length;
  71. setup = pffft_new_setup(length, PFFFT_COMPLEX);
  72. }
  73. ~ComplexFFT() {
  74. pffft_destroy_setup(setup);
  75. }
  76. /** Performs the complex FFT.
  77. Input and output must be aligned using the above align*() functions.
  78. Input is `2*length` elements. Output is `2*length` elements.
  79. */
  80. void fftUnordered(const float* input, float* output) {
  81. pffft_transform(setup, input, output, NULL, PFFFT_FORWARD);
  82. }
  83. /** Performs the inverse complex FFT.
  84. Input is `2*length` elements. Output is `2*length` elements.
  85. Scaling is such that FFT(IFFT(x)) = N*x.
  86. */
  87. void ifftUnordered(const float* input, float* output) {
  88. pffft_transform(setup, input, output, NULL, PFFFT_BACKWARD);
  89. }
  90. void fft(const float* input, float* output) {
  91. pffft_transform_ordered(setup, input, output, NULL, PFFFT_FORWARD);
  92. }
  93. void ifft(const float* input, float* output) {
  94. pffft_transform_ordered(setup, input, output, NULL, PFFFT_BACKWARD);
  95. }
  96. void scale(float* x) {
  97. float a = 1.f / length;
  98. for (int i = 0; i < length; i++) {
  99. x[2 * i + 0] *= a;
  100. x[2 * i + 1] *= a;
  101. }
  102. }
  103. };
  104. } // namespace dsp
  105. } // namespace rack