1
0

helper.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. """
  2. Discrete Fourier Transforms - helper.py
  3. """
  4. from __future__ import division, absolute_import, print_function
  5. from numpy.compat import integer_types
  6. from numpy.core import integer, empty, arange, asarray, roll
  7. from numpy.core.overrides import array_function_dispatch, set_module
  8. # Created by Pearu Peterson, September 2002
  9. __all__ = ['fftshift', 'ifftshift', 'fftfreq', 'rfftfreq']
  10. integer_types = integer_types + (integer,)
  11. def _fftshift_dispatcher(x, axes=None):
  12. return (x,)
  13. @array_function_dispatch(_fftshift_dispatcher, module='numpy.fft')
  14. def fftshift(x, axes=None):
  15. """
  16. Shift the zero-frequency component to the center of the spectrum.
  17. This function swaps half-spaces for all axes listed (defaults to all).
  18. Note that ``y[0]`` is the Nyquist component only if ``len(x)`` is even.
  19. Parameters
  20. ----------
  21. x : array_like
  22. Input array.
  23. axes : int or shape tuple, optional
  24. Axes over which to shift. Default is None, which shifts all axes.
  25. Returns
  26. -------
  27. y : ndarray
  28. The shifted array.
  29. See Also
  30. --------
  31. ifftshift : The inverse of `fftshift`.
  32. Examples
  33. --------
  34. >>> freqs = np.fft.fftfreq(10, 0.1)
  35. >>> freqs
  36. array([ 0., 1., 2., ..., -3., -2., -1.])
  37. >>> np.fft.fftshift(freqs)
  38. array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.])
  39. Shift the zero-frequency component only along the second axis:
  40. >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3)
  41. >>> freqs
  42. array([[ 0., 1., 2.],
  43. [ 3., 4., -4.],
  44. [-3., -2., -1.]])
  45. >>> np.fft.fftshift(freqs, axes=(1,))
  46. array([[ 2., 0., 1.],
  47. [-4., 3., 4.],
  48. [-1., -3., -2.]])
  49. """
  50. x = asarray(x)
  51. if axes is None:
  52. axes = tuple(range(x.ndim))
  53. shift = [dim // 2 for dim in x.shape]
  54. elif isinstance(axes, integer_types):
  55. shift = x.shape[axes] // 2
  56. else:
  57. shift = [x.shape[ax] // 2 for ax in axes]
  58. return roll(x, shift, axes)
  59. @array_function_dispatch(_fftshift_dispatcher, module='numpy.fft')
  60. def ifftshift(x, axes=None):
  61. """
  62. The inverse of `fftshift`. Although identical for even-length `x`, the
  63. functions differ by one sample for odd-length `x`.
  64. Parameters
  65. ----------
  66. x : array_like
  67. Input array.
  68. axes : int or shape tuple, optional
  69. Axes over which to calculate. Defaults to None, which shifts all axes.
  70. Returns
  71. -------
  72. y : ndarray
  73. The shifted array.
  74. See Also
  75. --------
  76. fftshift : Shift zero-frequency component to the center of the spectrum.
  77. Examples
  78. --------
  79. >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3)
  80. >>> freqs
  81. array([[ 0., 1., 2.],
  82. [ 3., 4., -4.],
  83. [-3., -2., -1.]])
  84. >>> np.fft.ifftshift(np.fft.fftshift(freqs))
  85. array([[ 0., 1., 2.],
  86. [ 3., 4., -4.],
  87. [-3., -2., -1.]])
  88. """
  89. x = asarray(x)
  90. if axes is None:
  91. axes = tuple(range(x.ndim))
  92. shift = [-(dim // 2) for dim in x.shape]
  93. elif isinstance(axes, integer_types):
  94. shift = -(x.shape[axes] // 2)
  95. else:
  96. shift = [-(x.shape[ax] // 2) for ax in axes]
  97. return roll(x, shift, axes)
  98. @set_module('numpy.fft')
  99. def fftfreq(n, d=1.0):
  100. """
  101. Return the Discrete Fourier Transform sample frequencies.
  102. The returned float array `f` contains the frequency bin centers in cycles
  103. per unit of the sample spacing (with zero at the start). For instance, if
  104. the sample spacing is in seconds, then the frequency unit is cycles/second.
  105. Given a window length `n` and a sample spacing `d`::
  106. f = [0, 1, ..., n/2-1, -n/2, ..., -1] / (d*n) if n is even
  107. f = [0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d*n) if n is odd
  108. Parameters
  109. ----------
  110. n : int
  111. Window length.
  112. d : scalar, optional
  113. Sample spacing (inverse of the sampling rate). Defaults to 1.
  114. Returns
  115. -------
  116. f : ndarray
  117. Array of length `n` containing the sample frequencies.
  118. Examples
  119. --------
  120. >>> signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float)
  121. >>> fourier = np.fft.fft(signal)
  122. >>> n = signal.size
  123. >>> timestep = 0.1
  124. >>> freq = np.fft.fftfreq(n, d=timestep)
  125. >>> freq
  126. array([ 0. , 1.25, 2.5 , ..., -3.75, -2.5 , -1.25])
  127. """
  128. if not isinstance(n, integer_types):
  129. raise ValueError("n should be an integer")
  130. val = 1.0 / (n * d)
  131. results = empty(n, int)
  132. N = (n-1)//2 + 1
  133. p1 = arange(0, N, dtype=int)
  134. results[:N] = p1
  135. p2 = arange(-(n//2), 0, dtype=int)
  136. results[N:] = p2
  137. return results * val
  138. @set_module('numpy.fft')
  139. def rfftfreq(n, d=1.0):
  140. """
  141. Return the Discrete Fourier Transform sample frequencies
  142. (for usage with rfft, irfft).
  143. The returned float array `f` contains the frequency bin centers in cycles
  144. per unit of the sample spacing (with zero at the start). For instance, if
  145. the sample spacing is in seconds, then the frequency unit is cycles/second.
  146. Given a window length `n` and a sample spacing `d`::
  147. f = [0, 1, ..., n/2-1, n/2] / (d*n) if n is even
  148. f = [0, 1, ..., (n-1)/2-1, (n-1)/2] / (d*n) if n is odd
  149. Unlike `fftfreq` (but like `scipy.fftpack.rfftfreq`)
  150. the Nyquist frequency component is considered to be positive.
  151. Parameters
  152. ----------
  153. n : int
  154. Window length.
  155. d : scalar, optional
  156. Sample spacing (inverse of the sampling rate). Defaults to 1.
  157. Returns
  158. -------
  159. f : ndarray
  160. Array of length ``n//2 + 1`` containing the sample frequencies.
  161. Examples
  162. --------
  163. >>> signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5, -3, 4], dtype=float)
  164. >>> fourier = np.fft.rfft(signal)
  165. >>> n = signal.size
  166. >>> sample_rate = 100
  167. >>> freq = np.fft.fftfreq(n, d=1./sample_rate)
  168. >>> freq
  169. array([ 0., 10., 20., ..., -30., -20., -10.])
  170. >>> freq = np.fft.rfftfreq(n, d=1./sample_rate)
  171. >>> freq
  172. array([ 0., 10., 20., 30., 40., 50.])
  173. """
  174. if not isinstance(n, integer_types):
  175. raise ValueError("n should be an integer")
  176. val = 1.0/(n*d)
  177. N = n//2 + 1
  178. results = arange(0, N, dtype=int)
  179. return results * val