|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +from functools import partial |
| 17 | +from numpy import asarray |
| 18 | +from numpy.fft._pocketfft import _raw_fft, _raw_fftnd, _get_forward_norm, _get_backward_norm, _cook_nd_args |
| 19 | + |
| 20 | + |
| 21 | +def _fftc2c(a, n=None, axis=-1, norm=None, forward=None): |
| 22 | + a = asarray(a) |
| 23 | + if n is None: |
| 24 | + n = a.shape[axis] |
| 25 | + if forward: |
| 26 | + inv_norm = _get_forward_norm(n, norm) |
| 27 | + else: |
| 28 | + inv_norm = _get_backward_norm(n, norm) |
| 29 | + output = _raw_fft(a, n, axis, False, forward, inv_norm) |
| 30 | + return output |
| 31 | + |
| 32 | + |
| 33 | +def _fftr2c(a, n=None, axis=-1, norm=None, forward=None): |
| 34 | + a = asarray(a) |
| 35 | + if n is None: |
| 36 | + n = a.shape[axis] |
| 37 | + if forward: |
| 38 | + inv_norm = _get_forward_norm(n, norm) |
| 39 | + else: |
| 40 | + inv_norm = _get_backward_norm(n, norm) |
| 41 | + output = _raw_fft(a, n, axis, True, True, inv_norm) |
| 42 | + if not forward: |
| 43 | + output = output.conj() |
| 44 | + return output |
| 45 | + |
| 46 | + |
| 47 | +def _fftc2r(a, n=None, axis=-1, norm=None, forward=None): |
| 48 | + a = asarray(a) |
| 49 | + if n is None: |
| 50 | + n = (a.shape[axis] - 1) * 2 |
| 51 | + if forward: |
| 52 | + inv_norm = _get_forward_norm(n, norm) |
| 53 | + else: |
| 54 | + inv_norm = _get_backward_norm(n, norm) |
| 55 | + output = _raw_fft(a.conj() |
| 56 | + if forward else a, n, axis, True, False, inv_norm) |
| 57 | + return output |
| 58 | + |
| 59 | + |
| 60 | +def fft_c2c(x, axes, normalization, forward): |
| 61 | + f = partial(_fftc2c, forward=forward) |
| 62 | + y = _raw_fftnd(x, s=None, axes=axes, function=f, norm=normalization) |
| 63 | + return y |
| 64 | + |
| 65 | + |
| 66 | +def fft_c2c_backward(dy, axes, normalization, forward): |
| 67 | + f = partial(_fftc2c, forward=forward) |
| 68 | + dx = _raw_fftnd(dy, s=None, axes=axes, function=f, norm=normalization) |
| 69 | + return dx |
| 70 | + |
| 71 | + |
| 72 | +def fft_r2c(x, axes, normalization, forward, onesided): |
| 73 | + a = asarray(x) |
| 74 | + s, axes = _cook_nd_args(a, axes=axes) |
| 75 | + if onesided: |
| 76 | + a = _fftr2c(a, s[-1], axes[-1], normalization, forward) |
| 77 | + for ii in range(len(axes) - 1): |
| 78 | + a = _fftc2c(a, s[ii], axes[ii], normalization, forward) |
| 79 | + else: |
| 80 | + a = fft_c2c(x, axes, normalization, forward) |
| 81 | + return a |
| 82 | + |
| 83 | + |
| 84 | +def fft_r2c_backward(dy, x, axes, normalization, forward, onesided): |
| 85 | + a = dy |
| 86 | + if not onesided: |
| 87 | + a = fft_c2c_backward(a, axes, normalization, forward).real |
| 88 | + else: |
| 89 | + pad_widths = [(0, 0)] * a.ndim |
| 90 | + last_axis = axes[-1] |
| 91 | + if last_axis < 0: |
| 92 | + last_axis += a.ndim |
| 93 | + last_dim_size = a.shape[last_axis] |
| 94 | + pad_widths[last_axis] = (0, x.shape[last_axis] - last_dim_size) |
| 95 | + a = np.pad(a, pad_width=pad_widths) |
| 96 | + a = fft_c2c_backward(a, axes, normalization, forward).real |
| 97 | + return a |
| 98 | + |
| 99 | + |
| 100 | +def fft_c2r(x, axes, normalization, forward, last_dim_size): |
| 101 | + pass |
0 commit comments