File size: 7,326 Bytes
28e6f98 | 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 268 269 270 271 272 273 | """
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import torch
import numpy as np
def complex_mul(x, y):
"""
Complex multiplication.
This multiplies two complex tensors assuming that they are both stored as
real arrays with the last dimension being the complex dimension.
Args:
x (torch.Tensor): A PyTorch tensor with the last dimension of size 2.
y (torch.Tensor): A PyTorch tensor with the last dimension of size 2.
Returns:
torch.Tensor: A PyTorch tensor with the last dimension of size 2.
"""
assert x.shape[-1] == y.shape[-1] == 2
re = x[..., 0] * y[..., 0] - x[..., 1] * y[..., 1]
im = x[..., 0] * y[..., 1] + x[..., 1] * y[..., 0]
return torch.stack((re, im), dim=-1)
def complex_conj(x):
"""
Complex conjugate.
This applies the complex conjugate assuming that the input array has the
last dimension as the complex dimension.
Args:
x (torch.Tensor): A PyTorch tensor with the last dimension of size 2.
y (torch.Tensor): A PyTorch tensor with the last dimension of size 2.
Returns:
torch.Tensor: A PyTorch tensor with the last dimension of size 2.
"""
assert x.shape[-1] == 2
return torch.stack((x[..., 0], -x[..., 1]), dim=-1)
# def fft2c(data):
# """
# Apply centered 2 dimensional Fast Fourier Transform.
# Args:
# data (torch.Tensor): Complex valued input data containing at least 3
# dimensions: dimensions -3 & -2 are spatial dimensions and dimension
# -1 has size 2. All other dimensions are assumed to be batch
# dimensions.
# Returns:
# torch.Tensor: The FFT of the input.
# """
# assert data.size(-1) == 2
# data = ifftshift(data, dim=(-3, -2))
# data = torch.fft(data, 2, normalized=True)
# data = fftshift(data, dim=(-3, -2))
# return data
# def ifft2c(data):
# """
# Apply centered 2-dimensional Inverse Fast Fourier Transform.
# Args:
# data (torch.Tensor): Complex valued input data containing at least 3
# dimensions: dimensions -3 & -2 are spatial dimensions and dimension
# -1 has size 2. All other dimensions are assumed to be batch
# dimensions.
# Returns:
# torch.Tensor: The IFFT of the input.
# """
# assert data.size(-1) == 2
# data = ifftshift(data, dim=(-3, -2))
# # data = torch.ifft(data, 2, normalized=True)
# data = torch.fft.ifft(data, 2, normalized=True)
# data = fftshift(data, dim=(-3, -2))
# return data
def fft2c(data: torch.Tensor, norm: str = "ortho") -> torch.Tensor:
"""
Apply centered 2 dimensional Fast Fourier Transform.
Args:
data: Complex valued input data containing at least 3 dimensions:
dimensions -3 & -2 are spatial dimensions and dimension -1 has size
2. All other dimensions are assumed to be batch dimensions.
norm: Normalization mode. See ``torch.fft.fft``.
Returns:
The FFT of the input.
"""
if not data.shape[-1] == 2:
raise ValueError("Tensor does not have separate complex dim.")
data = ifftshift(data, dim=[-3, -2])
data = torch.view_as_real(
torch.fft.fftn( # type: ignore
torch.view_as_complex(data), dim=(-2, -1), norm=norm
)
)
data = fftshift(data, dim=[-3, -2])
return data
def ifft2c(data: torch.Tensor, norm: str = "ortho") -> torch.Tensor:
"""
Apply centered 2-dimensional Inverse Fast Fourier Transform.
Args:
data: Complex valued input data containing at least 3 dimensions:
dimensions -3 & -2 are spatial dimensions and dimension -1 has size
2. All other dimensions are assumed to be batch dimensions.
norm: Normalization mode. See ``torch.fft.ifft``.
Returns:
The IFFT of the input.
"""
if not data.shape[-1] == 2:
raise ValueError("Tensor does not have separate complex dim.")
data = ifftshift(data, dim=[-3, -2])
data = torch.view_as_real(
torch.fft.ifftn( # type: ignore
torch.view_as_complex(data), dim=(-2, -1), norm=norm
)
)
data = fftshift(data, dim=[-3, -2])
return data
def complex_abs(data):
"""
Compute the absolute value of a complex valued input tensor.
Args:
data (torch.Tensor): A complex valued tensor, where the size of the
final dimension should be 2.
Returns:
torch.Tensor: Absolute value of data.
"""
assert data.size(-1) == 2
return (data ** 2).sum(dim=-1).sqrt()
def complex_abs_numpy(data):
assert data.shape[-1] == 2
return np.sqrt(np.sum(data ** 2, axis=-1))
def complex_abs_sq(data):#multi coil
"""
Compute the squared absolute value of a complex tensor.
Args:
data (torch.Tensor): A complex valued tensor, where the size of the
final dimension should be 2.
Returns:
torch.Tensor: Squared absolute value of data.
"""
assert data.size(-1) == 2
return (data ** 2).sum(dim=-1)
# Helper functions
def roll(x, shift, dim):
"""
Similar to np.roll but applies to PyTorch Tensors.
Args:
x (torch.Tensor): A PyTorch tensor.
shift (int): Amount to roll.
dim (int): Which dimension to roll.
Returns:
torch.Tensor: Rolled version of x.
"""
if isinstance(shift, (tuple, list)):
assert len(shift) == len(dim)
for s, d in zip(shift, dim):
x = roll(x, s, d)
return x
shift = shift % x.size(dim)
if shift == 0:
return x
left = x.narrow(dim, 0, x.size(dim) - shift)
right = x.narrow(dim, x.size(dim) - shift, shift)
return torch.cat((right, left), dim=dim)
def fftshift(x, dim=None):
"""
Similar to np.fft.fftshift but applies to PyTorch Tensors
Args:
x (torch.Tensor): A PyTorch tensor.
dim (int): Which dimension to fftshift.
Returns:
torch.Tensor: fftshifted version of x.
"""
if dim is None:
dim = tuple(range(x.dim()))
shift = [dim // 2 for dim in x.shape]
elif isinstance(dim, int):
shift = x.shape[dim] // 2
else:
shift = [x.shape[i] // 2 for i in dim]
return roll(x, shift, dim)
def ifftshift(x, dim=None):
"""
Similar to np.fft.ifftshift but applies to PyTorch Tensors
Args:
x (torch.Tensor): A PyTorch tensor.
dim (int): Which dimension to ifftshift.
Returns:
torch.Tensor: ifftshifted version of x.
"""
if dim is None:
dim = tuple(range(x.dim()))
shift = [(dim + 1) // 2 for dim in x.shape]
elif isinstance(dim, int):
shift = (x.shape[dim] + 1) // 2
else:
shift = [(x.shape[i] + 1) // 2 for i in dim]
return roll(x, shift, dim)
def tensor_to_complex_np(data):
"""
Converts a complex torch tensor to numpy array.
Args:
data (torch.Tensor): Input data to be converted to numpy.
Returns:
np.array: Complex numpy version of data
"""
data = data.numpy()
return data[..., 0] + 1j * data[..., 1]
|