__init__.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. """
  2. Discrete Fourier Transform (:mod:`numpy.fft`)
  3. =============================================
  4. .. currentmodule:: numpy.fft
  5. The SciPy module `scipy.fft` is a more comprehensive superset
  6. of ``numpy.fft``, which includes only a basic set of routines.
  7. Standard FFTs
  8. -------------
  9. .. autosummary::
  10. :toctree: generated/
  11. fft Discrete Fourier transform.
  12. ifft Inverse discrete Fourier transform.
  13. fft2 Discrete Fourier transform in two dimensions.
  14. ifft2 Inverse discrete Fourier transform in two dimensions.
  15. fftn Discrete Fourier transform in N-dimensions.
  16. ifftn Inverse discrete Fourier transform in N dimensions.
  17. Real FFTs
  18. ---------
  19. .. autosummary::
  20. :toctree: generated/
  21. rfft Real discrete Fourier transform.
  22. irfft Inverse real discrete Fourier transform.
  23. rfft2 Real discrete Fourier transform in two dimensions.
  24. irfft2 Inverse real discrete Fourier transform in two dimensions.
  25. rfftn Real discrete Fourier transform in N dimensions.
  26. irfftn Inverse real discrete Fourier transform in N dimensions.
  27. Hermitian FFTs
  28. --------------
  29. .. autosummary::
  30. :toctree: generated/
  31. hfft Hermitian discrete Fourier transform.
  32. ihfft Inverse Hermitian discrete Fourier transform.
  33. Helper routines
  34. ---------------
  35. .. autosummary::
  36. :toctree: generated/
  37. fftfreq Discrete Fourier Transform sample frequencies.
  38. rfftfreq DFT sample frequencies (for usage with rfft, irfft).
  39. fftshift Shift zero-frequency component to center of spectrum.
  40. ifftshift Inverse of fftshift.
  41. Background information
  42. ----------------------
  43. Fourier analysis is fundamentally a method for expressing a function as a
  44. sum of periodic components, and for recovering the function from those
  45. components. When both the function and its Fourier transform are
  46. replaced with discretized counterparts, it is called the discrete Fourier
  47. transform (DFT). The DFT has become a mainstay of numerical computing in
  48. part because of a very fast algorithm for computing it, called the Fast
  49. Fourier Transform (FFT), which was known to Gauss (1805) and was brought
  50. to light in its current form by Cooley and Tukey [CT]_. Press et al. [NR]_
  51. provide an accessible introduction to Fourier analysis and its
  52. applications.
  53. Because the discrete Fourier transform separates its input into
  54. components that contribute at discrete frequencies, it has a great number
  55. of applications in digital signal processing, e.g., for filtering, and in
  56. this context the discretized input to the transform is customarily
  57. referred to as a *signal*, which exists in the *time domain*. The output
  58. is called a *spectrum* or *transform* and exists in the *frequency
  59. domain*.
  60. Implementation details
  61. ----------------------
  62. There are many ways to define the DFT, varying in the sign of the
  63. exponent, normalization, etc. In this implementation, the DFT is defined
  64. as
  65. .. math::
  66. A_k = \\sum_{m=0}^{n-1} a_m \\exp\\left\\{-2\\pi i{mk \\over n}\\right\\}
  67. \\qquad k = 0,\\ldots,n-1.
  68. The DFT is in general defined for complex inputs and outputs, and a
  69. single-frequency component at linear frequency :math:`f` is
  70. represented by a complex exponential
  71. :math:`a_m = \\exp\\{2\\pi i\\,f m\\Delta t\\}`, where :math:`\\Delta t`
  72. is the sampling interval.
  73. The values in the result follow so-called "standard" order: If ``A =
  74. fft(a, n)``, then ``A[0]`` contains the zero-frequency term (the sum of
  75. the signal), which is always purely real for real inputs. Then ``A[1:n/2]``
  76. contains the positive-frequency terms, and ``A[n/2+1:]`` contains the
  77. negative-frequency terms, in order of decreasingly negative frequency.
  78. For an even number of input points, ``A[n/2]`` represents both positive and
  79. negative Nyquist frequency, and is also purely real for real input. For
  80. an odd number of input points, ``A[(n-1)/2]`` contains the largest positive
  81. frequency, while ``A[(n+1)/2]`` contains the largest negative frequency.
  82. The routine ``np.fft.fftfreq(n)`` returns an array giving the frequencies
  83. of corresponding elements in the output. The routine
  84. ``np.fft.fftshift(A)`` shifts transforms and their frequencies to put the
  85. zero-frequency components in the middle, and ``np.fft.ifftshift(A)`` undoes
  86. that shift.
  87. When the input `a` is a time-domain signal and ``A = fft(a)``, ``np.abs(A)``
  88. is its amplitude spectrum and ``np.abs(A)**2`` is its power spectrum.
  89. The phase spectrum is obtained by ``np.angle(A)``.
  90. The inverse DFT is defined as
  91. .. math::
  92. a_m = \\frac{1}{n}\\sum_{k=0}^{n-1}A_k\\exp\\left\\{2\\pi i{mk\\over n}\\right\\}
  93. \\qquad m = 0,\\ldots,n-1.
  94. It differs from the forward transform by the sign of the exponential
  95. argument and the default normalization by :math:`1/n`.
  96. Type Promotion
  97. --------------
  98. `numpy.fft` promotes ``float32`` and ``complex64`` arrays to ``float64`` and
  99. ``complex128`` arrays respectively. For an FFT implementation that does not
  100. promote input arrays, see `scipy.fftpack`.
  101. Normalization
  102. -------------
  103. The argument ``norm`` indicates which direction of the pair of direct/inverse
  104. transforms is scaled and with what normalization factor.
  105. The default normalization (``"backward"``) has the direct (forward) transforms
  106. unscaled and the inverse (backward) transforms scaled by :math:`1/n`. It is
  107. possible to obtain unitary transforms by setting the keyword argument ``norm``
  108. to ``"ortho"`` so that both direct and inverse transforms are scaled by
  109. :math:`1/\\sqrt{n}`. Finally, setting the keyword argument ``norm`` to
  110. ``"forward"`` has the direct transforms scaled by :math:`1/n` and the inverse
  111. transforms unscaled (i.e. exactly opposite to the default ``"backward"``).
  112. `None` is an alias of the default option ``"backward"`` for backward
  113. compatibility.
  114. Real and Hermitian transforms
  115. -----------------------------
  116. When the input is purely real, its transform is Hermitian, i.e., the
  117. component at frequency :math:`f_k` is the complex conjugate of the
  118. component at frequency :math:`-f_k`, which means that for real
  119. inputs there is no information in the negative frequency components that
  120. is not already available from the positive frequency components.
  121. The family of `rfft` functions is
  122. designed to operate on real inputs, and exploits this symmetry by
  123. computing only the positive frequency components, up to and including the
  124. Nyquist frequency. Thus, ``n`` input points produce ``n/2+1`` complex
  125. output points. The inverses of this family assumes the same symmetry of
  126. its input, and for an output of ``n`` points uses ``n/2+1`` input points.
  127. Correspondingly, when the spectrum is purely real, the signal is
  128. Hermitian. The `hfft` family of functions exploits this symmetry by
  129. using ``n/2+1`` complex points in the input (time) domain for ``n`` real
  130. points in the frequency domain.
  131. In higher dimensions, FFTs are used, e.g., for image analysis and
  132. filtering. The computational efficiency of the FFT means that it can
  133. also be a faster way to compute large convolutions, using the property
  134. that a convolution in the time domain is equivalent to a point-by-point
  135. multiplication in the frequency domain.
  136. Higher dimensions
  137. -----------------
  138. In two dimensions, the DFT is defined as
  139. .. math::
  140. A_{kl} = \\sum_{m=0}^{M-1} \\sum_{n=0}^{N-1}
  141. a_{mn}\\exp\\left\\{-2\\pi i \\left({mk\\over M}+{nl\\over N}\\right)\\right\\}
  142. \\qquad k = 0, \\ldots, M-1;\\quad l = 0, \\ldots, N-1,
  143. which extends in the obvious way to higher dimensions, and the inverses
  144. in higher dimensions also extend in the same way.
  145. References
  146. ----------
  147. .. [CT] Cooley, James W., and John W. Tukey, 1965, "An algorithm for the
  148. machine calculation of complex Fourier series," *Math. Comput.*
  149. 19: 297-301.
  150. .. [NR] Press, W., Teukolsky, S., Vetterline, W.T., and Flannery, B.P.,
  151. 2007, *Numerical Recipes: The Art of Scientific Computing*, ch.
  152. 12-13. Cambridge Univ. Press, Cambridge, UK.
  153. Examples
  154. --------
  155. For examples, see the various functions.
  156. """
  157. from . import _pocketfft, helper
  158. from ._pocketfft import *
  159. from .helper import *
  160. __all__ = _pocketfft.__all__.copy()
  161. __all__ += helper.__all__
  162. from numpy._pytesttester import PytestTester
  163. test = PytestTester(__name__)
  164. del PytestTester