-
Notifications
You must be signed in to change notification settings - Fork 92
/
fft.py
145 lines (102 loc) · 3.78 KB
/
fft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
"""FFT functions.
This module contains FFT functions that support centered operation.
"""
import numpy as np
from sigpy import backend, config, util
if config.cupy_enabled:
import cupy as cp
def fft(input, oshape=None, axes=None, center=True, norm='ortho'):
"""FFT function that supports centering.
Args:
input (array): input array.
oshape (None or array of ints): output shape.
axes (None or array of ints): Axes over which to compute the FFT.
norm (Nonr or ``"ortho"``): Keyword to specify the normalization mode.
Returns:
array: FFT result of dimension oshape.
See Also:
:func:`numpy.fft.fftn`
"""
device = backend.get_device(input)
xp = device.xp
with device:
if not np.issubdtype(input.dtype, np.complexfloating):
input = input.astype(np.complex)
if center:
output = _fftc(input, oshape=oshape, axes=axes, norm=norm)
else:
output = xp.fft.fftn(input, s=oshape, axes=axes, norm=norm)
if np.issubdtype(input.dtype, np.complexfloating) and input.dtype != output.dtype:
output = output.astype(input.dtype)
return output
def ifft(input, oshape=None, axes=None, center=True, norm='ortho'):
"""IFFT function that supports centering.
Args:
input (array): input array.
oshape (None or array of ints): output shape.
axes (None or array of ints): Axes over which to compute the inverse FFT.
norm (None or ``"ortho"``): Keyword to specify the normalization mode.
Returns:
array of dimension oshape.
See Also:
:func:`numpy.fft.ifftn`
"""
device = backend.get_device(input)
xp = device.xp
with device:
if not np.issubdtype(input.dtype, np.complexfloating):
input = input.astype(np.complex)
if center:
output = _ifftc(input, oshape=oshape, axes=axes, norm=norm)
else:
output = xp.fft.ifftn(input, s=oshape, axes=axes, norm=norm)
if np.issubdtype(input.dtype, np.complexfloating) and input.dtype != output.dtype:
output = output.astype(input.dtype)
return output
def _fftc(input, oshape=None, axes=None, norm='ortho'):
ndim = input.ndim
axes = util._normalize_axes(axes, ndim)
device = backend.get_device(input)
xp = device.xp
if oshape is None:
oshape = input.shape
with device:
tmp = input
tshape = list(input.shape)
for a in axes:
i = oshape[a]
tshape[a] = i
tmp = tmp.swapaxes(a, -1)
tshape[a], tshape[-1] = tshape[-1], tshape[a]
tmp = util.resize(tmp, tshape)
tmp = xp.fft.ifftshift(tmp, axes=-1)
tmp = xp.fft.fft(tmp, axis=-1, norm=norm)
tmp = xp.fft.fftshift(tmp, axes=-1)
tmp = tmp.swapaxes(a, -1)
tshape[a], tshape[-1] = tshape[-1], tshape[a]
output = tmp
return output
def _ifftc(input, oshape=None, axes=None, norm='ortho'):
ndim = input.ndim
axes = util._normalize_axes(axes, ndim)
device = backend.get_device(input)
xp = device.xp
if oshape is None:
oshape = input.shape
with device:
tmp = input
tshape = list(input.shape)
for a in axes:
i = oshape[a]
tshape[a] = i
tmp = tmp.swapaxes(a, -1)
tshape[a], tshape[-1] = tshape[-1], tshape[a]
tmp = util.resize(tmp, tshape)
tmp = xp.fft.ifftshift(tmp, axes=-1)
tmp = xp.fft.ifft(tmp, axis=-1, norm=norm)
tmp = xp.fft.fftshift(tmp, axes=-1)
tmp = tmp.swapaxes(a, -1)
tshape[a], tshape[-1] = tshape[-1], tshape[a]
output = tmp
return output