|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2017, Intel Corporation |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright notice, |
| 8 | +# this list of conditions and the following disclaimer. |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# * Neither the name of Intel Corporation nor the names of its contributors |
| 13 | +# may be used to endorse or promote products derived from this software |
| 14 | +# without specific prior written permission. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +""" |
| 28 | +FFT helper functions copied from `numpy.fft` (with some modification) to |
| 29 | +prevent circular dependencies when patching NumPy. |
| 30 | +""" |
| 31 | + |
| 32 | +import numpy as np |
| 33 | + |
| 34 | +__all__ = ["fftshift", "ifftshift", "fftfreq", "rfftfreq"] |
| 35 | + |
| 36 | + |
| 37 | +def fftshift(x, axes=None): |
| 38 | + """ |
| 39 | + Shift the zero-frequency component to the center of the spectrum. |
| 40 | +
|
| 41 | + For full documentation refer to `numpy.fft.fftshift`. |
| 42 | +
|
| 43 | + """ |
| 44 | + x = np.asarray(x) |
| 45 | + if axes is None: |
| 46 | + axes = tuple(range(x.ndim)) |
| 47 | + shift = [dim // 2 for dim in x.shape] |
| 48 | + elif isinstance(axes, (int, np.integer)): |
| 49 | + shift = x.shape[axes] // 2 |
| 50 | + else: |
| 51 | + shift = [x.shape[ax] // 2 for ax in axes] |
| 52 | + |
| 53 | + return np.roll(x, shift, axes) |
| 54 | + |
| 55 | + |
| 56 | +def ifftshift(x, axes=None): |
| 57 | + """ |
| 58 | + The inverse of `fftshift`. Although identical for even-length `x`, the |
| 59 | + functions differ by one sample for odd-length `x`. |
| 60 | +
|
| 61 | + For full documentation refer to `numpy.fft.ifftshift`. |
| 62 | +
|
| 63 | + """ |
| 64 | + x = np.asarray(x) |
| 65 | + if axes is None: |
| 66 | + axes = tuple(range(x.ndim)) |
| 67 | + shift = [-(dim // 2) for dim in x.shape] |
| 68 | + elif isinstance(axes, (int, np.integer)): |
| 69 | + shift = -(x.shape[axes] // 2) |
| 70 | + else: |
| 71 | + shift = [-(x.shape[ax] // 2) for ax in axes] |
| 72 | + |
| 73 | + return np.roll(x, shift, axes) |
| 74 | + |
| 75 | + |
| 76 | +def fftfreq(n, d=1.0, device=None): |
| 77 | + """ |
| 78 | + Return the Discrete Fourier Transform sample frequencies. |
| 79 | +
|
| 80 | + For full documentation refer to `numpy.fft.fftfreq`. |
| 81 | +
|
| 82 | + """ |
| 83 | + if not isinstance(n, (int, np.integer)): |
| 84 | + raise ValueError("n should be an integer") |
| 85 | + val = 1.0 / (n * d) |
| 86 | + results = np.empty(n, int, device=device) |
| 87 | + N = (n - 1) // 2 + 1 |
| 88 | + p1 = np.arange(0, N, dtype=int, device=device) |
| 89 | + results[:N] = p1 |
| 90 | + p2 = np.arange(-(n // 2), 0, dtype=int, device=device) |
| 91 | + results[N:] = p2 |
| 92 | + return results * val |
| 93 | + |
| 94 | + |
| 95 | +def rfftfreq(n, d=1.0, device=None): |
| 96 | + """ |
| 97 | + Return the Discrete Fourier Transform sample frequencies (for usage with |
| 98 | + `rfft`, `irfft`). |
| 99 | +
|
| 100 | + For full documentation refer to `numpy.fft.rfftfreq`. |
| 101 | +
|
| 102 | + """ |
| 103 | + if not isinstance(n, (int, np.integer)): |
| 104 | + raise ValueError("n should be an integer") |
| 105 | + val = 1.0 / (n * d) |
| 106 | + N = n // 2 + 1 |
| 107 | + results = np.arange(0, N, dtype=int, device=device) |
| 108 | + return results * val |
0 commit comments