pffft.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. struct PFFFTSetupDeleter {
  81. void operator()(gsl::owner<PFFFT_Setup*> setup) const noexcept;
  82. };
  83. using PFFFTSetupPtr = std::unique_ptr<PFFFT_Setup,PFFFTSetupDeleter>;
  84. /**
  85. * Prepare for performing transforms of size N -- the returned PFFFT_Setup
  86. * structure is read-only so it can safely be shared by multiple concurrent
  87. * threads.
  88. */
  89. PFFFTSetupPtr pffft_new_setup(unsigned int N, pffft_transform_t transform);
  90. /**
  91. * Perform a Fourier transform. The z-domain data is stored in the most
  92. * efficient order for transforming back or using for convolution, and as
  93. * such, there's no guarantee to the order of the values. If you need to have
  94. * its content sorted in the usual way, that is as an array of interleaved
  95. * complex numbers, either use pffft_transform_ordered, or call pffft_zreorder
  96. * after the forward fft and before the backward fft.
  97. *
  98. * Transforms are not scaled: PFFFT_BACKWARD(PFFFT_FORWARD(x)) = N*x. Typically
  99. * you will want to scale the backward transform by 1/N.
  100. *
  101. * The 'work' pointer must point to an area of N (2*N for complex fft) floats,
  102. * properly aligned. It cannot be NULL.
  103. *
  104. * The input and output parameters may alias.
  105. */
  106. void pffft_transform(const PFFFT_Setup *setup, const float *input, float *output, float *work, pffft_direction_t direction);
  107. /**
  108. * Similar to pffft_transform, but handles the complex values in the usual form
  109. * (interleaved complex numbers). This is similar to calling
  110. * pffft_transform(..., PFFFT_FORWARD) followed by
  111. * pffft_zreorder(..., PFFFT_FORWARD), or
  112. * pffft_zreorder(..., PFFFT_BACKWARD) followed by
  113. * pffft_transform(..., PFFFT_BACKWARD), for the given direction.
  114. *
  115. * The input and output parameters may alias.
  116. */
  117. void pffft_transform_ordered(const PFFFT_Setup *setup, const float *input, float *output, float *work, pffft_direction_t direction);
  118. /**
  119. * Reorder the z-domain data. For PFFFT_FORWARD, it reorders from the internal
  120. * representation to the "canonical" order (as interleaved complex numbers).
  121. * For PFFFT_BACKWARD, it reorders from the canonical order to the internal
  122. * order suitable for pffft_transform(..., PFFFT_BACKWARD) or
  123. * pffft_zconvolve_accumulate.
  124. *
  125. * The input and output parameters should not alias.
  126. */
  127. void pffft_zreorder(const PFFFT_Setup *setup, const float *input, float *output, pffft_direction_t direction);
  128. /**
  129. * Perform a multiplication of the z-domain data in dft_a and dft_b, and scale
  130. * and accumulate into dft_ab. The arrays should have been obtained with
  131. * pffft_transform(..., PFFFT_FORWARD) or pffft_zreorder(..., PFFFT_BACKWARD)
  132. * and should *not* be in the usual order (otherwise just perform the operation
  133. * yourself as the dft coeffs are stored as interleaved complex numbers).
  134. *
  135. * The operation performed is: dft_ab += (dft_a * dft_b)*scaling
  136. *
  137. * The dft_a, dft_b, and dft_ab parameters may alias.
  138. */
  139. void pffft_zconvolve_scale_accumulate(const PFFFT_Setup *setup, const float *dft_a, const float *dft_b, float *dft_ab, float scaling);
  140. /**
  141. * Perform a multiplication of the z-domain data in dft_a and dft_b, and
  142. * accumulate into dft_ab.
  143. *
  144. * The operation performed is: dft_ab += dft_a * dft_b
  145. *
  146. * The dft_a, dft_b, and dft_ab parameters may alias.
  147. */
  148. void pffft_zconvolve_accumulate(const PFFFT_Setup *setup, const float *dft_a, const float *dft_b, float *dft_ab);
  149. struct PFFFTSetup {
  150. PFFFTSetupPtr mSetup;
  151. PFFFTSetup() = default;
  152. PFFFTSetup(const PFFFTSetup&) = delete;
  153. PFFFTSetup(PFFFTSetup&& rhs) noexcept = default;
  154. explicit PFFFTSetup(std::nullptr_t) noexcept { }
  155. explicit PFFFTSetup(unsigned int n, pffft_transform_t transform)
  156. : mSetup{pffft_new_setup(n, transform)}
  157. { }
  158. ~PFFFTSetup() = default;
  159. PFFFTSetup& operator=(const PFFFTSetup&) = delete;
  160. PFFFTSetup& operator=(PFFFTSetup&& rhs) noexcept = default;
  161. [[nodiscard]] explicit operator bool() const noexcept { return mSetup != nullptr; }
  162. void transform(const float *input, float *output, float *work, pffft_direction_t direction) const
  163. { pffft_transform(mSetup.get(), input, output, work, direction); }
  164. void transform_ordered(const float *input, float *output, float *work,
  165. pffft_direction_t direction) const
  166. { pffft_transform_ordered(mSetup.get(), input, output, work, direction); }
  167. void zreorder(const float *input, float *output, pffft_direction_t direction) const
  168. { pffft_zreorder(mSetup.get(), input, output, direction); }
  169. void zconvolve_scale_accumulate(const float *dft_a, const float *dft_b, float *dft_ab,
  170. float scaling) const
  171. { pffft_zconvolve_scale_accumulate(mSetup.get(), dft_a, dft_b, dft_ab, scaling); }
  172. void zconvolve_accumulate(const float *dft_a, const float *dft_b, float *dft_ab) const
  173. { pffft_zconvolve_accumulate(mSetup.get(), dft_a, dft_b, dft_ab); }
  174. };
  175. #endif // PFFFT_H