test_fftpack.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. from __future__ import division, absolute_import, print_function
  2. import numpy as np
  3. from numpy.random import random
  4. from numpy.testing import (
  5. assert_array_almost_equal, assert_array_equal, assert_raises,
  6. )
  7. import threading
  8. import sys
  9. if sys.version_info[0] >= 3:
  10. import queue
  11. else:
  12. import Queue as queue
  13. def fft1(x):
  14. L = len(x)
  15. phase = -2j*np.pi*(np.arange(L)/float(L))
  16. phase = np.arange(L).reshape(-1, 1) * phase
  17. return np.sum(x*np.exp(phase), axis=1)
  18. class TestFFTShift(object):
  19. def test_fft_n(self):
  20. assert_raises(ValueError, np.fft.fft, [1, 2, 3], 0)
  21. class TestFFT1D(object):
  22. def test_fft(self):
  23. x = random(30) + 1j*random(30)
  24. assert_array_almost_equal(fft1(x), np.fft.fft(x))
  25. assert_array_almost_equal(fft1(x) / np.sqrt(30),
  26. np.fft.fft(x, norm="ortho"))
  27. def test_ifft(self):
  28. x = random(30) + 1j*random(30)
  29. assert_array_almost_equal(x, np.fft.ifft(np.fft.fft(x)))
  30. assert_array_almost_equal(
  31. x, np.fft.ifft(np.fft.fft(x, norm="ortho"), norm="ortho"))
  32. def test_fft2(self):
  33. x = random((30, 20)) + 1j*random((30, 20))
  34. assert_array_almost_equal(np.fft.fft(np.fft.fft(x, axis=1), axis=0),
  35. np.fft.fft2(x))
  36. assert_array_almost_equal(np.fft.fft2(x) / np.sqrt(30 * 20),
  37. np.fft.fft2(x, norm="ortho"))
  38. def test_ifft2(self):
  39. x = random((30, 20)) + 1j*random((30, 20))
  40. assert_array_almost_equal(np.fft.ifft(np.fft.ifft(x, axis=1), axis=0),
  41. np.fft.ifft2(x))
  42. assert_array_almost_equal(np.fft.ifft2(x) * np.sqrt(30 * 20),
  43. np.fft.ifft2(x, norm="ortho"))
  44. def test_fftn(self):
  45. x = random((30, 20, 10)) + 1j*random((30, 20, 10))
  46. assert_array_almost_equal(
  47. np.fft.fft(np.fft.fft(np.fft.fft(x, axis=2), axis=1), axis=0),
  48. np.fft.fftn(x))
  49. assert_array_almost_equal(np.fft.fftn(x) / np.sqrt(30 * 20 * 10),
  50. np.fft.fftn(x, norm="ortho"))
  51. def test_ifftn(self):
  52. x = random((30, 20, 10)) + 1j*random((30, 20, 10))
  53. assert_array_almost_equal(
  54. np.fft.ifft(np.fft.ifft(np.fft.ifft(x, axis=2), axis=1), axis=0),
  55. np.fft.ifftn(x))
  56. assert_array_almost_equal(np.fft.ifftn(x) * np.sqrt(30 * 20 * 10),
  57. np.fft.ifftn(x, norm="ortho"))
  58. def test_rfft(self):
  59. x = random(30)
  60. for n in [x.size, 2*x.size]:
  61. for norm in [None, 'ortho']:
  62. assert_array_almost_equal(
  63. np.fft.fft(x, n=n, norm=norm)[:(n//2 + 1)],
  64. np.fft.rfft(x, n=n, norm=norm))
  65. assert_array_almost_equal(np.fft.rfft(x, n=n) / np.sqrt(n),
  66. np.fft.rfft(x, n=n, norm="ortho"))
  67. def test_irfft(self):
  68. x = random(30)
  69. assert_array_almost_equal(x, np.fft.irfft(np.fft.rfft(x)))
  70. assert_array_almost_equal(
  71. x, np.fft.irfft(np.fft.rfft(x, norm="ortho"), norm="ortho"))
  72. def test_rfft2(self):
  73. x = random((30, 20))
  74. assert_array_almost_equal(np.fft.fft2(x)[:, :11], np.fft.rfft2(x))
  75. assert_array_almost_equal(np.fft.rfft2(x) / np.sqrt(30 * 20),
  76. np.fft.rfft2(x, norm="ortho"))
  77. def test_irfft2(self):
  78. x = random((30, 20))
  79. assert_array_almost_equal(x, np.fft.irfft2(np.fft.rfft2(x)))
  80. assert_array_almost_equal(
  81. x, np.fft.irfft2(np.fft.rfft2(x, norm="ortho"), norm="ortho"))
  82. def test_rfftn(self):
  83. x = random((30, 20, 10))
  84. assert_array_almost_equal(np.fft.fftn(x)[:, :, :6], np.fft.rfftn(x))
  85. assert_array_almost_equal(np.fft.rfftn(x) / np.sqrt(30 * 20 * 10),
  86. np.fft.rfftn(x, norm="ortho"))
  87. def test_irfftn(self):
  88. x = random((30, 20, 10))
  89. assert_array_almost_equal(x, np.fft.irfftn(np.fft.rfftn(x)))
  90. assert_array_almost_equal(
  91. x, np.fft.irfftn(np.fft.rfftn(x, norm="ortho"), norm="ortho"))
  92. def test_hfft(self):
  93. x = random(14) + 1j*random(14)
  94. x_herm = np.concatenate((random(1), x, random(1)))
  95. x = np.concatenate((x_herm, x[::-1].conj()))
  96. assert_array_almost_equal(np.fft.fft(x), np.fft.hfft(x_herm))
  97. assert_array_almost_equal(np.fft.hfft(x_herm) / np.sqrt(30),
  98. np.fft.hfft(x_herm, norm="ortho"))
  99. def test_ihttf(self):
  100. x = random(14) + 1j*random(14)
  101. x_herm = np.concatenate((random(1), x, random(1)))
  102. x = np.concatenate((x_herm, x[::-1].conj()))
  103. assert_array_almost_equal(x_herm, np.fft.ihfft(np.fft.hfft(x_herm)))
  104. assert_array_almost_equal(
  105. x_herm, np.fft.ihfft(np.fft.hfft(x_herm, norm="ortho"),
  106. norm="ortho"))
  107. def test_all_1d_norm_preserving(self):
  108. # verify that round-trip transforms are norm-preserving
  109. x = random(30)
  110. x_norm = np.linalg.norm(x)
  111. n = x.size * 2
  112. func_pairs = [(np.fft.fft, np.fft.ifft),
  113. (np.fft.rfft, np.fft.irfft),
  114. # hfft: order so the first function takes x.size samples
  115. # (necessary for comparison to x_norm above)
  116. (np.fft.ihfft, np.fft.hfft),
  117. ]
  118. for forw, back in func_pairs:
  119. for n in [x.size, 2*x.size]:
  120. for norm in [None, 'ortho']:
  121. tmp = forw(x, n=n, norm=norm)
  122. tmp = back(tmp, n=n, norm=norm)
  123. assert_array_almost_equal(x_norm,
  124. np.linalg.norm(tmp))
  125. class TestFFTThreadSafe(object):
  126. threads = 16
  127. input_shape = (800, 200)
  128. def _test_mtsame(self, func, *args):
  129. def worker(args, q):
  130. q.put(func(*args))
  131. q = queue.Queue()
  132. expected = func(*args)
  133. # Spin off a bunch of threads to call the same function simultaneously
  134. t = [threading.Thread(target=worker, args=(args, q))
  135. for i in range(self.threads)]
  136. [x.start() for x in t]
  137. [x.join() for x in t]
  138. # Make sure all threads returned the correct value
  139. for i in range(self.threads):
  140. assert_array_equal(q.get(timeout=5), expected,
  141. 'Function returned wrong value in multithreaded context')
  142. def test_fft(self):
  143. a = np.ones(self.input_shape) * 1+0j
  144. self._test_mtsame(np.fft.fft, a)
  145. def test_ifft(self):
  146. a = np.ones(self.input_shape) * 1+0j
  147. self._test_mtsame(np.fft.ifft, a)
  148. def test_rfft(self):
  149. a = np.ones(self.input_shape)
  150. self._test_mtsame(np.fft.rfft, a)
  151. def test_irfft(self):
  152. a = np.ones(self.input_shape) * 1+0j
  153. self._test_mtsame(np.fft.irfft, a)