"Fossies" - the Fresh Open Source Software Archive

Member "numpy-1.16.4/numpy/fft/tests/test_fftpack.py" (27 May 2019, 7019 Bytes) of package /linux/misc/numpy-1.16.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

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