|
| 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 `scipy.fft` (with some modification) to |
| 29 | +prevent circular dependencies when patching NumPy. |
| 30 | +""" |
| 31 | + |
| 32 | +import numpy as np |
| 33 | + |
| 34 | +from scipy._lib._array_api import array_namespace |
| 35 | + |
| 36 | +__all__ = ["fftshift", "ifftshift", "fftfreq", "rfftfreq"] |
| 37 | + |
| 38 | + |
| 39 | +def fftfreq(n, d=1.0, *, xp=None, device=None): |
| 40 | + """ |
| 41 | + Return the Discrete Fourier Transform sample frequencies. |
| 42 | +
|
| 43 | + For full documentation refer to `scipy.fft.fftfreq`. |
| 44 | +
|
| 45 | + """ |
| 46 | + xp = np if xp is None else xp |
| 47 | + if hasattr(xp, "fft"): |
| 48 | + return xp.fft.fftfreq(n, d=d, device=device) |
| 49 | + return np.fft.fftfreq(n, d=d, device=device) |
| 50 | + |
| 51 | + |
| 52 | +def rfftfreq(n, d=1.0, *, xp=None, device=None): |
| 53 | + """ |
| 54 | + Return the Discrete Fourier Transform sample frequencies (for usage with |
| 55 | + `rfft`, `irfft`). |
| 56 | +
|
| 57 | + For full documentation refer to `scipy.fft.rfftfreq`. |
| 58 | +
|
| 59 | + """ |
| 60 | + xp = np if xp is None else xp |
| 61 | + if hasattr(xp, "fft"): |
| 62 | + return xp.fft.rfftfreq(n, d=d, device=device) |
| 63 | + return np.fft.rfftfreq(n, d=d, device=device) |
| 64 | + |
| 65 | + |
| 66 | +def fftshift(x, axes=None): |
| 67 | + """ |
| 68 | + Shift the zero-frequency component to the center of the spectrum. |
| 69 | +
|
| 70 | + For full documentation refer to `scipy.fft.fftshift`. |
| 71 | +
|
| 72 | + """ |
| 73 | + xp = array_namespace(x) |
| 74 | + if hasattr(xp, "fft"): |
| 75 | + return xp.fft.fftshift(x, axes=axes) |
| 76 | + x = np.asarray(x) |
| 77 | + y = np.fft.fftshift(x, axes=axes) |
| 78 | + return xp.asarray(y) |
| 79 | + |
| 80 | + |
| 81 | +def ifftshift(x, axes=None): |
| 82 | + """ |
| 83 | + The inverse of `fftshift`. Although identical for even-length `x`, the |
| 84 | + functions differ by one sample for odd-length `x`. |
| 85 | +
|
| 86 | + For full documentation refer to `scipy.fft.ifftshift`. |
| 87 | +
|
| 88 | + """ |
| 89 | + xp = array_namespace(x) |
| 90 | + if hasattr(xp, "fft"): |
| 91 | + return xp.fft.ifftshift(x, axes=axes) |
| 92 | + x = np.asarray(x) |
| 93 | + y = np.fft.ifftshift(x, axes=axes) |
| 94 | + return xp.asarray(y) |
0 commit comments