_basic.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. """
  2. Discrete Fourier Transforms - _basic.py
  3. """
  4. # Created by Pearu Peterson, August,September 2002
  5. __all__ = ['fft','ifft','fftn','ifftn','rfft','irfft',
  6. 'fft2','ifft2']
  7. from scipy.fft import _pocketfft
  8. from ._helper import _good_shape
  9. def fft(x, n=None, axis=-1, overwrite_x=False):
  10. """
  11. Return discrete Fourier transform of real or complex sequence.
  12. The returned complex array contains ``y(0), y(1),..., y(n-1)``, where
  13. ``y(j) = (x * exp(-2*pi*sqrt(-1)*j*np.arange(n)/n)).sum()``.
  14. Parameters
  15. ----------
  16. x : array_like
  17. Array to Fourier transform.
  18. n : int, optional
  19. Length of the Fourier transform. If ``n < x.shape[axis]``, `x` is
  20. truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
  21. default results in ``n = x.shape[axis]``.
  22. axis : int, optional
  23. Axis along which the fft's are computed; the default is over the
  24. last axis (i.e., ``axis=-1``).
  25. overwrite_x : bool, optional
  26. If True, the contents of `x` can be destroyed; the default is False.
  27. Returns
  28. -------
  29. z : complex ndarray
  30. with the elements::
  31. [y(0),y(1),..,y(n/2),y(1-n/2),...,y(-1)] if n is even
  32. [y(0),y(1),..,y((n-1)/2),y(-(n-1)/2),...,y(-1)] if n is odd
  33. where::
  34. y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k* 2*pi/n), j = 0..n-1
  35. See Also
  36. --------
  37. ifft : Inverse FFT
  38. rfft : FFT of a real sequence
  39. Notes
  40. -----
  41. The packing of the result is "standard": If ``A = fft(a, n)``, then
  42. ``A[0]`` contains the zero-frequency term, ``A[1:n/2]`` contains the
  43. positive-frequency terms, and ``A[n/2:]`` contains the negative-frequency
  44. terms, in order of decreasingly negative frequency. So ,for an 8-point
  45. transform, the frequencies of the result are [0, 1, 2, 3, -4, -3, -2, -1].
  46. To rearrange the fft output so that the zero-frequency component is
  47. centered, like [-4, -3, -2, -1, 0, 1, 2, 3], use `fftshift`.
  48. Both single and double precision routines are implemented. Half precision
  49. inputs will be converted to single precision. Non-floating-point inputs
  50. will be converted to double precision. Long-double precision inputs are
  51. not supported.
  52. This function is most efficient when `n` is a power of two, and least
  53. efficient when `n` is prime.
  54. Note that if ``x`` is real-valued, then ``A[j] == A[n-j].conjugate()``.
  55. If ``x`` is real-valued and ``n`` is even, then ``A[n/2]`` is real.
  56. If the data type of `x` is real, a "real FFT" algorithm is automatically
  57. used, which roughly halves the computation time. To increase efficiency
  58. a little further, use `rfft`, which does the same calculation, but only
  59. outputs half of the symmetrical spectrum. If the data is both real and
  60. symmetrical, the `dct` can again double the efficiency by generating
  61. half of the spectrum from half of the signal.
  62. Examples
  63. --------
  64. >>> import numpy as np
  65. >>> from scipy.fftpack import fft, ifft
  66. >>> x = np.arange(5)
  67. >>> np.allclose(fft(ifft(x)), x, atol=1e-15) # within numerical accuracy.
  68. True
  69. """
  70. return _pocketfft.fft(x, n, axis, None, overwrite_x)
  71. def ifft(x, n=None, axis=-1, overwrite_x=False):
  72. """
  73. Return discrete inverse Fourier transform of real or complex sequence.
  74. The returned complex array contains ``y(0), y(1),..., y(n-1)``, where
  75. ``y(j) = (x * exp(2*pi*sqrt(-1)*j*np.arange(n)/n)).mean()``.
  76. Parameters
  77. ----------
  78. x : array_like
  79. Transformed data to invert.
  80. n : int, optional
  81. Length of the inverse Fourier transform. If ``n < x.shape[axis]``,
  82. `x` is truncated. If ``n > x.shape[axis]``, `x` is zero-padded.
  83. The default results in ``n = x.shape[axis]``.
  84. axis : int, optional
  85. Axis along which the ifft's are computed; the default is over the
  86. last axis (i.e., ``axis=-1``).
  87. overwrite_x : bool, optional
  88. If True, the contents of `x` can be destroyed; the default is False.
  89. Returns
  90. -------
  91. ifft : ndarray of floats
  92. The inverse discrete Fourier transform.
  93. See Also
  94. --------
  95. fft : Forward FFT
  96. Notes
  97. -----
  98. Both single and double precision routines are implemented. Half precision
  99. inputs will be converted to single precision. Non-floating-point inputs
  100. will be converted to double precision. Long-double precision inputs are
  101. not supported.
  102. This function is most efficient when `n` is a power of two, and least
  103. efficient when `n` is prime.
  104. If the data type of `x` is real, a "real IFFT" algorithm is automatically
  105. used, which roughly halves the computation time.
  106. Examples
  107. --------
  108. >>> from scipy.fftpack import fft, ifft
  109. >>> import numpy as np
  110. >>> x = np.arange(5)
  111. >>> np.allclose(ifft(fft(x)), x, atol=1e-15) # within numerical accuracy.
  112. True
  113. """
  114. return _pocketfft.ifft(x, n, axis, None, overwrite_x)
  115. def rfft(x, n=None, axis=-1, overwrite_x=False):
  116. """
  117. Discrete Fourier transform of a real sequence.
  118. Parameters
  119. ----------
  120. x : array_like, real-valued
  121. The data to transform.
  122. n : int, optional
  123. Defines the length of the Fourier transform. If `n` is not specified
  124. (the default) then ``n = x.shape[axis]``. If ``n < x.shape[axis]``,
  125. `x` is truncated, if ``n > x.shape[axis]``, `x` is zero-padded.
  126. axis : int, optional
  127. The axis along which the transform is applied. The default is the
  128. last axis.
  129. overwrite_x : bool, optional
  130. If set to true, the contents of `x` can be overwritten. Default is
  131. False.
  132. Returns
  133. -------
  134. z : real ndarray
  135. The returned real array contains::
  136. [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2))] if n is even
  137. [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2)),Im(y(n/2))] if n is odd
  138. where::
  139. y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k*2*pi/n)
  140. j = 0..n-1
  141. See Also
  142. --------
  143. fft, irfft, scipy.fft.rfft
  144. Notes
  145. -----
  146. Within numerical accuracy, ``y == rfft(irfft(y))``.
  147. Both single and double precision routines are implemented. Half precision
  148. inputs will be converted to single precision. Non-floating-point inputs
  149. will be converted to double precision. Long-double precision inputs are
  150. not supported.
  151. To get an output with a complex datatype, consider using the newer
  152. function `scipy.fft.rfft`.
  153. Examples
  154. --------
  155. >>> from scipy.fftpack import fft, rfft
  156. >>> a = [9, -9, 1, 3]
  157. >>> fft(a)
  158. array([ 4. +0.j, 8.+12.j, 16. +0.j, 8.-12.j])
  159. >>> rfft(a)
  160. array([ 4., 8., 12., 16.])
  161. """
  162. return _pocketfft.rfft_fftpack(x, n, axis, None, overwrite_x)
  163. def irfft(x, n=None, axis=-1, overwrite_x=False):
  164. """
  165. Return inverse discrete Fourier transform of real sequence x.
  166. The contents of `x` are interpreted as the output of the `rfft`
  167. function.
  168. Parameters
  169. ----------
  170. x : array_like
  171. Transformed data to invert.
  172. n : int, optional
  173. Length of the inverse Fourier transform.
  174. If n < x.shape[axis], x is truncated.
  175. If n > x.shape[axis], x is zero-padded.
  176. The default results in n = x.shape[axis].
  177. axis : int, optional
  178. Axis along which the ifft's are computed; the default is over
  179. the last axis (i.e., axis=-1).
  180. overwrite_x : bool, optional
  181. If True, the contents of `x` can be destroyed; the default is False.
  182. Returns
  183. -------
  184. irfft : ndarray of floats
  185. The inverse discrete Fourier transform.
  186. See Also
  187. --------
  188. rfft, ifft, scipy.fft.irfft
  189. Notes
  190. -----
  191. The returned real array contains::
  192. [y(0),y(1),...,y(n-1)]
  193. where for n is even::
  194. y(j) = 1/n (sum[k=1..n/2-1] (x[2*k-1]+sqrt(-1)*x[2*k])
  195. * exp(sqrt(-1)*j*k* 2*pi/n)
  196. + c.c. + x[0] + (-1)**(j) x[n-1])
  197. and for n is odd::
  198. y(j) = 1/n (sum[k=1..(n-1)/2] (x[2*k-1]+sqrt(-1)*x[2*k])
  199. * exp(sqrt(-1)*j*k* 2*pi/n)
  200. + c.c. + x[0])
  201. c.c. denotes complex conjugate of preceding expression.
  202. For details on input parameters, see `rfft`.
  203. To process (conjugate-symmetric) frequency-domain data with a complex
  204. datatype, consider using the newer function `scipy.fft.irfft`.
  205. Examples
  206. --------
  207. >>> from scipy.fftpack import rfft, irfft
  208. >>> a = [1.0, 2.0, 3.0, 4.0, 5.0]
  209. >>> irfft(a)
  210. array([ 2.6 , -3.16405192, 1.24398433, -1.14955713, 1.46962473])
  211. >>> irfft(rfft(a))
  212. array([1., 2., 3., 4., 5.])
  213. """
  214. return _pocketfft.irfft_fftpack(x, n, axis, None, overwrite_x)
  215. def fftn(x, shape=None, axes=None, overwrite_x=False):
  216. """
  217. Return multidimensional discrete Fourier transform.
  218. The returned array contains::
  219. y[j_1,..,j_d] = sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
  220. x[k_1,..,k_d] * prod[i=1..d] exp(-sqrt(-1)*2*pi/n_i * j_i * k_i)
  221. where d = len(x.shape) and n = x.shape.
  222. Parameters
  223. ----------
  224. x : array_like
  225. The (N-D) array to transform.
  226. shape : int or array_like of ints or None, optional
  227. The shape of the result. If both `shape` and `axes` (see below) are
  228. None, `shape` is ``x.shape``; if `shape` is None but `axes` is
  229. not None, then `shape` is ``numpy.take(x.shape, axes, axis=0)``.
  230. If ``shape[i] > x.shape[i]``, the ith dimension is padded with zeros.
  231. If ``shape[i] < x.shape[i]``, the ith dimension is truncated to
  232. length ``shape[i]``.
  233. If any element of `shape` is -1, the size of the corresponding
  234. dimension of `x` is used.
  235. axes : int or array_like of ints or None, optional
  236. The axes of `x` (`y` if `shape` is not None) along which the
  237. transform is applied.
  238. The default is over all axes.
  239. overwrite_x : bool, optional
  240. If True, the contents of `x` can be destroyed. Default is False.
  241. Returns
  242. -------
  243. y : complex-valued N-D NumPy array
  244. The (N-D) DFT of the input array.
  245. See Also
  246. --------
  247. ifftn
  248. Notes
  249. -----
  250. If ``x`` is real-valued, then
  251. ``y[..., j_i, ...] == y[..., n_i-j_i, ...].conjugate()``.
  252. Both single and double precision routines are implemented. Half precision
  253. inputs will be converted to single precision. Non-floating-point inputs
  254. will be converted to double precision. Long-double precision inputs are
  255. not supported.
  256. Examples
  257. --------
  258. >>> import numpy as np
  259. >>> from scipy.fftpack import fftn, ifftn
  260. >>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
  261. >>> np.allclose(y, fftn(ifftn(y)))
  262. True
  263. """
  264. shape = _good_shape(x, shape, axes)
  265. return _pocketfft.fftn(x, shape, axes, None, overwrite_x)
  266. def ifftn(x, shape=None, axes=None, overwrite_x=False):
  267. """
  268. Return inverse multidimensional discrete Fourier transform.
  269. The sequence can be of an arbitrary type.
  270. The returned array contains::
  271. y[j_1,..,j_d] = 1/p * sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
  272. x[k_1,..,k_d] * prod[i=1..d] exp(sqrt(-1)*2*pi/n_i * j_i * k_i)
  273. where ``d = len(x.shape)``, ``n = x.shape``, and ``p = prod[i=1..d] n_i``.
  274. For description of parameters see `fftn`.
  275. See Also
  276. --------
  277. fftn : for detailed information.
  278. Examples
  279. --------
  280. >>> from scipy.fftpack import fftn, ifftn
  281. >>> import numpy as np
  282. >>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
  283. >>> np.allclose(y, ifftn(fftn(y)))
  284. True
  285. """
  286. shape = _good_shape(x, shape, axes)
  287. return _pocketfft.ifftn(x, shape, axes, None, overwrite_x)
  288. def fft2(x, shape=None, axes=(-2,-1), overwrite_x=False):
  289. """
  290. 2-D discrete Fourier transform.
  291. Return the 2-D discrete Fourier transform of the 2-D argument
  292. `x`.
  293. See Also
  294. --------
  295. fftn : for detailed information.
  296. Examples
  297. --------
  298. >>> import numpy as np
  299. >>> from scipy.fftpack import fft2, ifft2
  300. >>> y = np.mgrid[:5, :5][0]
  301. >>> y
  302. array([[0, 0, 0, 0, 0],
  303. [1, 1, 1, 1, 1],
  304. [2, 2, 2, 2, 2],
  305. [3, 3, 3, 3, 3],
  306. [4, 4, 4, 4, 4]])
  307. >>> np.allclose(y, ifft2(fft2(y)))
  308. True
  309. """
  310. return fftn(x,shape,axes,overwrite_x)
  311. def ifft2(x, shape=None, axes=(-2,-1), overwrite_x=False):
  312. """
  313. 2-D discrete inverse Fourier transform of real or complex sequence.
  314. Return inverse 2-D discrete Fourier transform of
  315. arbitrary type sequence x.
  316. See `ifft` for more information.
  317. See Also
  318. --------
  319. fft2, ifft
  320. Examples
  321. --------
  322. >>> import numpy as np
  323. >>> from scipy.fftpack import fft2, ifft2
  324. >>> y = np.mgrid[:5, :5][0]
  325. >>> y
  326. array([[0, 0, 0, 0, 0],
  327. [1, 1, 1, 1, 1],
  328. [2, 2, 2, 2, 2],
  329. [3, 3, 3, 3, 3],
  330. [4, 4, 4, 4, 4]])
  331. >>> np.allclose(y, fft2(ifft2(y)))
  332. True
  333. """
  334. return ifftn(x,shape,axes,overwrite_x)