pffft.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (c) 2013 Julien Pommier ( [email protected] )
  2. Based on original fortran 77 code from FFTPACKv4 from NETLIB,
  3. authored by Dr Paul Swarztrauber of NCAR, in 1985.
  4. As confirmed by the NCAR fftpack software curators, the following
  5. FFTPACKv5 license applies to FFTPACKv4 sources. My changes are
  6. released under the same terms.
  7. FFTPACK license:
  8. http://www.cisl.ucar.edu/css/software/fftpack5/ftpk.html
  9. Copyright (c) 2004 the University Corporation for Atmospheric
  10. Research ("UCAR"). All rights reserved. Developed by NCAR's
  11. Computational and Information Systems Laboratory, UCAR,
  12. www.cisl.ucar.edu.
  13. Redistribution and use of the Software in source and binary forms,
  14. with or without modification, is permitted provided that the
  15. following conditions are met:
  16. - Neither the names of NCAR's Computational and Information Systems
  17. Laboratory, the University Corporation for Atmospheric Research,
  18. nor the names of its sponsors or contributors may be used to
  19. endorse or promote products derived from this Software without
  20. specific prior written permission.
  21. - Redistributions of source code must retain the above copyright
  22. notices, this list of conditions, and the disclaimer below.
  23. - Redistributions in binary form must reproduce the above copyright
  24. notice, this list of conditions, and the disclaimer below in the
  25. documentation and/or other materials provided with the
  26. distribution.
  27. THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE WARRANTIES OF
  29. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
  31. HOLDERS BE LIABLE FOR ANY CLAIM, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  33. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  35. SOFTWARE.
  36. */
  37. /* PFFFT : a Pretty Fast FFT.
  38. *
  39. * This is basically an adaptation of the single precision fftpack (v4) as
  40. * found on netlib taking advantage of SIMD instructions found on CPUs such as
  41. * Intel x86 (SSE1), PowerPC (Altivec), and Arm (NEON).
  42. *
  43. * For architectures where SIMD instructions aren't available, the code falls
  44. * back to a scalar version.
  45. *
  46. * Restrictions:
  47. *
  48. * - 1D transforms only, with 32-bit single precision.
  49. *
  50. * - supports only transforms for inputs of length N of the form
  51. * N=(2^a)*(3^b)*(5^c), given a >= 5, b >=0, c >= 0 (32, 48, 64, 96, 128, 144,
  52. * 160, etc are all acceptable lengths). Performance is best for 128<=N<=8192.
  53. *
  54. * - all (float*) pointers for the functions below are expected to have a
  55. * "SIMD-compatible" alignment, that is 16 bytes.
  56. *
  57. * You can allocate such buffers with the pffft_aligned_malloc function, and
  58. * deallocate them with pffft_aligned_free (or with stuff like posix_memalign,
  59. * aligned_alloc, etc).
  60. *
  61. * Note that for the z-domain data of real transforms, when in the canonical
  62. * order (as interleaved complex numbers) both 0-frequency and half-frequency
  63. * components, which are real, are assembled in the first entry as
  64. * F(0)+i*F(n/2+1). The original fftpack placed F(n/2+1) at the end of the
  65. * arrays instead.
  66. */
  67. #ifndef PFFFT_H
  68. #define PFFFT_H
  69. #include <cstddef>
  70. #include <memory>
  71. #include "almalloc.h"
  72. /* opaque struct holding internal stuff (precomputed twiddle factors) this
  73. * struct can be shared by many threads as it contains only read-only data.
  74. */
  75. struct PFFFT_Setup;
  76. /* direction of the transform */
  77. enum pffft_direction_t { PFFFT_FORWARD, PFFFT_BACKWARD };
  78. /* type of transform */
  79. enum pffft_transform_t { PFFFT_REAL, PFFFT_COMPLEX };
  80. void pffft_destroy_setup(gsl::owner<PFFFT_Setup*> setup) noexcept;
  81. struct PFFFTSetupDeleter {
  82. void operator()(gsl::owner<PFFFT_Setup*> setup) const noexcept { pffft_destroy_setup(setup); }
  83. };
  84. using PFFFTSetupPtr = std::unique_ptr<PFFFT_Setup,PFFFTSetupDeleter>;
  85. /**
  86. * Prepare for performing transforms of size N -- the returned PFFFT_Setup
  87. * structure is read-only so it can safely be shared by multiple concurrent
  88. * threads.
  89. */
  90. PFFFTSetupPtr pffft_new_setup(unsigned int N, pffft_transform_t transform);
  91. /**
  92. * Perform a Fourier transform. The z-domain data is stored in the most
  93. * efficient order for transforming back or using for convolution, and as
  94. * such, there's no guarantee to the order of the values. If you need to have
  95. * its content sorted in the usual way, that is as an array of interleaved
  96. * complex numbers, either use pffft_transform_ordered, or call pffft_zreorder
  97. * after the forward fft and before the backward fft.
  98. *
  99. * Transforms are not scaled: PFFFT_BACKWARD(PFFFT_FORWARD(x)) = N*x. Typically
  100. * you will want to scale the backward transform by 1/N.
  101. *
  102. * The 'work' pointer must point to an area of N (2*N for complex fft) floats,
  103. * properly aligned. It cannot be NULL.
  104. *
  105. * The input and output parameters may alias.
  106. */
  107. void pffft_transform(const PFFFT_Setup *setup, const float *input, float *output, float *work, pffft_direction_t direction);
  108. /**
  109. * Similar to pffft_transform, but handles the complex values in the usual form
  110. * (interleaved complex numbers). This is similar to calling
  111. * pffft_transform(..., PFFFT_FORWARD) followed by
  112. * pffft_zreorder(..., PFFFT_FORWARD), or
  113. * pffft_zreorder(..., PFFFT_BACKWARD) followed by
  114. * pffft_transform(..., PFFFT_BACKWARD), for the given direction.
  115. *
  116. * The input and output parameters may alias.
  117. */
  118. void pffft_transform_ordered(const PFFFT_Setup *setup, const float *input, float *output, float *work, pffft_direction_t direction);
  119. /**
  120. * Reorder the z-domain data. For PFFFT_FORWARD, it reorders from the internal
  121. * representation to the "canonical" order (as interleaved complex numbers).
  122. * For PFFFT_BACKWARD, it reorders from the canonical order to the internal
  123. * order suitable for pffft_transform(..., PFFFT_BACKWARD) or
  124. * pffft_zconvolve_accumulate.
  125. *
  126. * The input and output parameters should not alias.
  127. */
  128. void pffft_zreorder(const PFFFT_Setup *setup, const float *input, float *output, pffft_direction_t direction);
  129. /**
  130. * Perform a multiplication of the z-domain data in dft_a and dft_b, and scale
  131. * and accumulate into dft_ab. The arrays should have been obtained with
  132. * pffft_transform(..., PFFFT_FORWARD) or pffft_zreorder(..., PFFFT_BACKWARD)
  133. * and should *not* be in the usual order (otherwise just perform the operation
  134. * yourself as the dft coeffs are stored as interleaved complex numbers).
  135. *
  136. * The operation performed is: dft_ab += (dft_a * dft_b)*scaling
  137. *
  138. * The dft_a, dft_b, and dft_ab parameters may alias.
  139. */
  140. void pffft_zconvolve_scale_accumulate(const PFFFT_Setup *setup, const float *dft_a, const float *dft_b, float *dft_ab, float scaling);
  141. /**
  142. * Perform a multiplication of the z-domain data in dft_a and dft_b, and
  143. * accumulate into dft_ab.
  144. *
  145. * The operation performed is: dft_ab += dft_a * dft_b
  146. *
  147. * The dft_a, dft_b, and dft_ab parameters may alias.
  148. */
  149. void pffft_zconvolve_accumulate(const PFFFT_Setup *setup, const float *dft_a, const float *dft_b, float *dft_ab);
  150. struct PFFFTSetup {
  151. PFFFTSetupPtr mSetup{};
  152. PFFFTSetup() = default;
  153. PFFFTSetup(const PFFFTSetup&) = delete;
  154. PFFFTSetup(PFFFTSetup&& rhs) noexcept = default;
  155. explicit PFFFTSetup(std::nullptr_t) noexcept { }
  156. explicit PFFFTSetup(unsigned int n, pffft_transform_t transform)
  157. : mSetup{pffft_new_setup(n, transform)}
  158. { }
  159. ~PFFFTSetup() = default;
  160. PFFFTSetup& operator=(const PFFFTSetup&) = delete;
  161. PFFFTSetup& operator=(PFFFTSetup&& rhs) noexcept = default;
  162. [[nodiscard]] explicit operator bool() const noexcept { return mSetup != nullptr; }
  163. void transform(const float *input, float *output, float *work, pffft_direction_t direction) const
  164. { pffft_transform(mSetup.get(), input, output, work, direction); }
  165. void transform_ordered(const float *input, float *output, float *work,
  166. pffft_direction_t direction) const
  167. { pffft_transform_ordered(mSetup.get(), input, output, work, direction); }
  168. void zreorder(const float *input, float *output, pffft_direction_t direction) const
  169. { pffft_zreorder(mSetup.get(), input, output, direction); }
  170. void zconvolve_scale_accumulate(const float *dft_a, const float *dft_b, float *dft_ab,
  171. float scaling) const
  172. { pffft_zconvolve_scale_accumulate(mSetup.get(), dft_a, dft_b, dft_ab, scaling); }
  173. void zconvolve_accumulate(const float *dft_a, const float *dft_b, float *dft_ab) const
  174. { pffft_zconvolve_accumulate(mSetup.get(), dft_a, dft_b, dft_ab); }
  175. };
  176. #endif // PFFFT_H