forked from mikgroup/sigpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfourier.py
267 lines (199 loc) · 8.13 KB
/
fourier.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- coding: utf-8 -*-
"""FFT and non-uniform FFT (NUFFT) functions.
"""
import jax
import jax.numpy as np
from jax.numpy import ceil
from sigpy import backend, interp, util
__all__ = ['fft', 'ifft', 'nufft', 'nufft_adjoint', 'estimate_shape']
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`
"""
xp = backend.get_array_module(input)
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, copy=False)
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`
"""
xp = backend.get_array_module(input)
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 nufft(input, coord, oversamp=1.25, width=4):
"""Non-uniform Fast Fourier Transform.
Args:
input (array): input signal domain array of shape
(..., n_{ndim - 1}, ..., n_1, n_0),
where ndim is specified by coord.shape[-1]. The nufft
is applied on the last ndim axes, and looped over
the remaining axes.
coord (array): Fourier domain coordinate array of shape (..., ndim).
ndim determines the number of dimensions to apply the nufft.
coord[..., i] should be scaled to have its range between
-n_i // 2, and n_i // 2.
oversamp (float): oversampling factor.
width (float): interpolation kernel full-width in terms of
oversampled grid.
n (int): number of sampling points of the interpolation kernel.
Returns:
array: Fourier domain data of shape
input.shape[:-ndim] + coord.shape[:-1].
References:
Fessler, J. A., & Sutton, B. P. (2003).
Nonuniform fast Fourier transforms using min-max interpolation
IEEE Transactions on Signal Processing, 51(2), 560-574.
Beatty, P. J., Nishimura, D. G., & Pauly, J. M. (2005).
Rapid gridding reconstruction with a minimal oversampling ratio.
IEEE transactions on medical imaging, 24(6), 799-808.
"""
ndim = coord.shape[-1]
beta = np.pi * (((width / oversamp) * (oversamp - 0.5))**2 - 0.8)**0.5
os_shape = _get_oversamp_shape(input.shape, ndim, oversamp)
output = input.copy()
# Apodize
_apodize(output, ndim, oversamp, width, beta)
# Zero-pad
output /= util.prod(input.shape[-ndim:])**0.5
output = util.resize(output, os_shape)
# FFT
output = fft(output, axes=range(-ndim, 0), norm=None)
# Interpolate
coord = _scale_coord(coord, input.shape, oversamp)
output = interp.interpolate(
output, coord, kernel='kaiser_bessel', width=width, param=beta)
output /= width**ndim
return output
def estimate_shape(coord):
"""Estimate array shape from coordinates.
Shape is estimated by the different between maximum and minimum of
coordinates in each axis.
Args:
coord (array): Coordinates.
"""
ndim = coord.shape[-1]
with backend.get_device(coord):
shape = [int(coord[..., i].max() - coord[..., i].min())
for i in range(ndim)]
return shape
def nufft_adjoint(input, coord, oshape=None, oversamp=1.25, width=4):
"""Adjoint non-uniform Fast Fourier Transform.
Args:
input (array): input Fourier domain array of shape
(...) + coord.shape[:-1]. That is, the last dimensions
of input must match the first dimensions of coord.
The nufft_adjoint is applied on the last coord.ndim - 1 axes,
and looped over the remaining axes.
coord (array): Fourier domain coordinate array of shape (..., ndim).
ndim determines the number of dimension to apply nufft adjoint.
coord[..., i] should be scaled to have its range between
-n_i // 2, and n_i // 2.
oshape (tuple of ints): output shape of the form
(..., n_{ndim - 1}, ..., n_1, n_0).
oversamp (float): oversampling factor.
width (float): interpolation kernel full-width in terms of
oversampled grid.
n (int): number of sampling points of the interpolation kernel.
Returns:
array: signal domain array with shape specified by oshape.
See Also:
:func:`sigpy.nufft.nufft`
"""
ndim = coord.shape[-1]
beta = np.pi * (((width / oversamp) * (oversamp - 0.5))**2 - 0.8)**0.5
if oshape is None:
oshape = list(input.shape[:-coord.ndim + 1]) + estimate_shape(coord)
else:
oshape = list(oshape)
os_shape = _get_oversamp_shape(oshape, ndim, oversamp)
# Gridding
coord = _scale_coord(coord, oshape, oversamp)
output = interp.gridding(input, coord, os_shape,
kernel='kaiser_bessel', width=width, param=beta)
output /= width**ndim
# IFFT
output = ifft(output, axes=range(-ndim, 0), norm=None)
# Crop
output = util.resize(output, oshape)
output *= util.prod(os_shape[-ndim:]) / util.prod(oshape[-ndim:])**0.5
# Apodize
_apodize(output, ndim, oversamp, width, beta)
return output
def _fftc(input, oshape=None, axes=None, norm='ortho'):
ndim = input.ndim
axes = util._normalize_axes(axes, ndim)
xp = backend.get_array_module(input)
if oshape is None:
oshape = input.shape
tmp = util.resize(input, oshape)
tmp = xp.fft.ifftshift(tmp, axes=axes)
tmp = xp.fft.fftn(tmp, axes=axes, norm=norm)
output = xp.fft.fftshift(tmp, axes=axes)
return output
def _ifftc(input, oshape=None, axes=None, norm='ortho'):
ndim = input.ndim
axes = util._normalize_axes(axes, ndim)
xp = backend.get_array_module(input)
if oshape is None:
oshape = input.shape
tmp = util.resize(input, oshape)
tmp = xp.fft.ifftshift(tmp, axes=axes)
tmp = xp.fft.ifftn(tmp, axes=axes, norm=norm)
output = xp.fft.fftshift(tmp, axes=axes)
return output
def _scale_coord(coord, shape, oversamp):
ndim = coord.shape[-1]
output = coord.copy()
for i in range(-ndim, 0):
scale = ceil(oversamp * shape[i]) / shape[i]
shift = ceil(oversamp * shape[i]) // 2
output[..., i] *= scale
output[..., i] += shift
return output
def _get_oversamp_shape(shape, ndim, oversamp):
return list(shape)[:-ndim] + [ceil(oversamp * i) for i in shape[-ndim:]]
def _apodize(input, ndim, oversamp, width, beta):
xp = backend.get_array_module(input)
output = input
for a in range(-ndim, 0):
i = output.shape[a]
os_i = ceil(oversamp * i)
idx = xp.arange(i, dtype=output.dtype)
# Calculate apodization
apod = (beta**2 - (np.pi * width * (idx - i // 2) / os_i)**2)**0.5
apod /= xp.sinh(apod)
output *= apod.reshape([i] + [1] * (-a - 1))
return output