图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.0 KiB

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