__init__.py 7.4 KB

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