test_helper.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. """Test functions for fftpack.helper module
  2. Copied from fftpack.helper by Pearu Peterson, October 2005
  3. """
  4. from __future__ import division, absolute_import, print_function
  5. import numpy as np
  6. from numpy.testing import assert_array_almost_equal, assert_equal
  7. from numpy import fft, pi
  8. class TestFFTShift(object):
  9. def test_definition(self):
  10. x = [0, 1, 2, 3, 4, -4, -3, -2, -1]
  11. y = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
  12. assert_array_almost_equal(fft.fftshift(x), y)
  13. assert_array_almost_equal(fft.ifftshift(y), x)
  14. x = [0, 1, 2, 3, 4, -5, -4, -3, -2, -1]
  15. y = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
  16. assert_array_almost_equal(fft.fftshift(x), y)
  17. assert_array_almost_equal(fft.ifftshift(y), x)
  18. def test_inverse(self):
  19. for n in [1, 4, 9, 100, 211]:
  20. x = np.random.random((n,))
  21. assert_array_almost_equal(fft.ifftshift(fft.fftshift(x)), x)
  22. def test_axes_keyword(self):
  23. freqs = [[0, 1, 2], [3, 4, -4], [-3, -2, -1]]
  24. shifted = [[-1, -3, -2], [2, 0, 1], [-4, 3, 4]]
  25. assert_array_almost_equal(fft.fftshift(freqs, axes=(0, 1)), shifted)
  26. assert_array_almost_equal(fft.fftshift(freqs, axes=0),
  27. fft.fftshift(freqs, axes=(0,)))
  28. assert_array_almost_equal(fft.ifftshift(shifted, axes=(0, 1)), freqs)
  29. assert_array_almost_equal(fft.ifftshift(shifted, axes=0),
  30. fft.ifftshift(shifted, axes=(0,)))
  31. assert_array_almost_equal(fft.fftshift(freqs), shifted)
  32. assert_array_almost_equal(fft.ifftshift(shifted), freqs)
  33. def test_uneven_dims(self):
  34. """ Test 2D input, which has uneven dimension sizes """
  35. freqs = [
  36. [0, 1],
  37. [2, 3],
  38. [4, 5]
  39. ]
  40. # shift in dimension 0
  41. shift_dim0 = [
  42. [4, 5],
  43. [0, 1],
  44. [2, 3]
  45. ]
  46. assert_array_almost_equal(fft.fftshift(freqs, axes=0), shift_dim0)
  47. assert_array_almost_equal(fft.ifftshift(shift_dim0, axes=0), freqs)
  48. assert_array_almost_equal(fft.fftshift(freqs, axes=(0,)), shift_dim0)
  49. assert_array_almost_equal(fft.ifftshift(shift_dim0, axes=[0]), freqs)
  50. # shift in dimension 1
  51. shift_dim1 = [
  52. [1, 0],
  53. [3, 2],
  54. [5, 4]
  55. ]
  56. assert_array_almost_equal(fft.fftshift(freqs, axes=1), shift_dim1)
  57. assert_array_almost_equal(fft.ifftshift(shift_dim1, axes=1), freqs)
  58. # shift in both dimensions
  59. shift_dim_both = [
  60. [5, 4],
  61. [1, 0],
  62. [3, 2]
  63. ]
  64. assert_array_almost_equal(fft.fftshift(freqs, axes=(0, 1)), shift_dim_both)
  65. assert_array_almost_equal(fft.ifftshift(shift_dim_both, axes=(0, 1)), freqs)
  66. assert_array_almost_equal(fft.fftshift(freqs, axes=[0, 1]), shift_dim_both)
  67. assert_array_almost_equal(fft.ifftshift(shift_dim_both, axes=[0, 1]), freqs)
  68. # axes=None (default) shift in all dimensions
  69. assert_array_almost_equal(fft.fftshift(freqs, axes=None), shift_dim_both)
  70. assert_array_almost_equal(fft.ifftshift(shift_dim_both, axes=None), freqs)
  71. assert_array_almost_equal(fft.fftshift(freqs), shift_dim_both)
  72. assert_array_almost_equal(fft.ifftshift(shift_dim_both), freqs)
  73. def test_equal_to_original(self):
  74. """ Test that the new (>=v1.15) implementation (see #10073) is equal to the original (<=v1.14) """
  75. from numpy.compat import integer_types
  76. from numpy.core import asarray, concatenate, arange, take
  77. def original_fftshift(x, axes=None):
  78. """ How fftshift was implemented in v1.14"""
  79. tmp = asarray(x)
  80. ndim = tmp.ndim
  81. if axes is None:
  82. axes = list(range(ndim))
  83. elif isinstance(axes, integer_types):
  84. axes = (axes,)
  85. y = tmp
  86. for k in axes:
  87. n = tmp.shape[k]
  88. p2 = (n + 1) // 2
  89. mylist = concatenate((arange(p2, n), arange(p2)))
  90. y = take(y, mylist, k)
  91. return y
  92. def original_ifftshift(x, axes=None):
  93. """ How ifftshift was implemented in v1.14 """
  94. tmp = asarray(x)
  95. ndim = tmp.ndim
  96. if axes is None:
  97. axes = list(range(ndim))
  98. elif isinstance(axes, integer_types):
  99. axes = (axes,)
  100. y = tmp
  101. for k in axes:
  102. n = tmp.shape[k]
  103. p2 = n - (n + 1) // 2
  104. mylist = concatenate((arange(p2, n), arange(p2)))
  105. y = take(y, mylist, k)
  106. return y
  107. # create possible 2d array combinations and try all possible keywords
  108. # compare output to original functions
  109. for i in range(16):
  110. for j in range(16):
  111. for axes_keyword in [0, 1, None, (0,), (0, 1)]:
  112. inp = np.random.rand(i, j)
  113. assert_array_almost_equal(fft.fftshift(inp, axes_keyword),
  114. original_fftshift(inp, axes_keyword))
  115. assert_array_almost_equal(fft.ifftshift(inp, axes_keyword),
  116. original_ifftshift(inp, axes_keyword))
  117. class TestFFTFreq(object):
  118. def test_definition(self):
  119. x = [0, 1, 2, 3, 4, -4, -3, -2, -1]
  120. assert_array_almost_equal(9*fft.fftfreq(9), x)
  121. assert_array_almost_equal(9*pi*fft.fftfreq(9, pi), x)
  122. x = [0, 1, 2, 3, 4, -5, -4, -3, -2, -1]
  123. assert_array_almost_equal(10*fft.fftfreq(10), x)
  124. assert_array_almost_equal(10*pi*fft.fftfreq(10, pi), x)
  125. class TestRFFTFreq(object):
  126. def test_definition(self):
  127. x = [0, 1, 2, 3, 4]
  128. assert_array_almost_equal(9*fft.rfftfreq(9), x)
  129. assert_array_almost_equal(9*pi*fft.rfftfreq(9, pi), x)
  130. x = [0, 1, 2, 3, 4, 5]
  131. assert_array_almost_equal(10*fft.rfftfreq(10), x)
  132. assert_array_almost_equal(10*pi*fft.rfftfreq(10, pi), x)
  133. class TestIRFFTN(object):
  134. def test_not_last_axis_success(self):
  135. ar, ai = np.random.random((2, 16, 8, 32))
  136. a = ar + 1j*ai
  137. axes = (-2,)
  138. # Should not raise error
  139. fft.irfftn(a, axes=axes)