Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit c2432ff

Browse files
authored
Add normalization options for FFT (#146)
* Add normalization options for FFT * Harmonize function signatures
1 parent 0da79ae commit c2432ff

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

fastmri/fftc.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,70 @@
1414
import torch.fft # type: ignore
1515

1616

17-
def fft2c_old(data: torch.Tensor) -> torch.Tensor:
17+
def fft2c_old(data: torch.Tensor, norm: str = "ortho") -> torch.Tensor:
1818
"""
1919
Apply centered 2 dimensional Fast Fourier Transform.
2020
2121
Args:
2222
data: Complex valued input data containing at least 3 dimensions:
2323
dimensions -3 & -2 are spatial dimensions and dimension -1 has size
2424
2. All other dimensions are assumed to be batch dimensions.
25+
norm: Whether to include normalization. Must be one of ``"backward"``
26+
or ``"ortho"``. See ``torch.fft.fft`` on PyTorch 1.9.0 for details.
2527
2628
Returns:
2729
The FFT of the input.
2830
"""
2931
if not data.shape[-1] == 2:
3032
raise ValueError("Tensor does not have separate complex dim.")
33+
if norm not in ("ortho", "backward"):
34+
raise ValueError("norm must be 'ortho' or 'backward'.")
35+
normalized = True if norm == "ortho" else False
3136

3237
data = ifftshift(data, dim=[-3, -2])
33-
data = torch.fft(data, 2, normalized=True)
38+
data = torch.fft(data, 2, normalized=normalized)
3439
data = fftshift(data, dim=[-3, -2])
3540

3641
return data
3742

3843

39-
def ifft2c_old(data: torch.Tensor) -> torch.Tensor:
44+
def ifft2c_old(data: torch.Tensor, norm: str = "ortho") -> torch.Tensor:
4045
"""
4146
Apply centered 2-dimensional Inverse Fast Fourier Transform.
4247
4348
Args:
4449
data: Complex valued input data containing at least 3 dimensions:
4550
dimensions -3 & -2 are spatial dimensions and dimension -1 has size
4651
2. All other dimensions are assumed to be batch dimensions.
52+
norm: Whether to include normalization. Must be one of ``"backward"``
53+
or ``"ortho"``. See ``torch.fft.ifft`` on PyTorch 1.9.0 for
54+
details.
4755
4856
Returns:
4957
The IFFT of the input.
5058
"""
5159
if not data.shape[-1] == 2:
5260
raise ValueError("Tensor does not have separate complex dim.")
61+
if norm not in ("ortho", "backward"):
62+
raise ValueError("norm must be 'ortho' or 'backward'.")
63+
normalized = True if norm == "ortho" else False
5364

5465
data = ifftshift(data, dim=[-3, -2])
55-
data = torch.ifft(data, 2, normalized=True)
66+
data = torch.ifft(data, 2, normalized=normalized)
5667
data = fftshift(data, dim=[-3, -2])
5768

5869
return data
5970

6071

61-
def fft2c_new(data: torch.Tensor) -> torch.Tensor:
72+
def fft2c_new(data: torch.Tensor, norm: str = "ortho") -> torch.Tensor:
6273
"""
6374
Apply centered 2 dimensional Fast Fourier Transform.
6475
6576
Args:
6677
data: Complex valued input data containing at least 3 dimensions:
6778
dimensions -3 & -2 are spatial dimensions and dimension -1 has size
6879
2. All other dimensions are assumed to be batch dimensions.
80+
norm: Normalization mode. See ``torch.fft.fft``.
6981
7082
Returns:
7183
The FFT of the input.
@@ -76,22 +88,23 @@ def fft2c_new(data: torch.Tensor) -> torch.Tensor:
7688
data = ifftshift(data, dim=[-3, -2])
7789
data = torch.view_as_real(
7890
torch.fft.fftn( # type: ignore
79-
torch.view_as_complex(data), dim=(-2, -1), norm="ortho"
91+
torch.view_as_complex(data), dim=(-2, -1), norm=norm
8092
)
8193
)
8294
data = fftshift(data, dim=[-3, -2])
8395

8496
return data
8597

8698

87-
def ifft2c_new(data: torch.Tensor) -> torch.Tensor:
99+
def ifft2c_new(data: torch.Tensor, norm: str = "ortho") -> torch.Tensor:
88100
"""
89101
Apply centered 2-dimensional Inverse Fast Fourier Transform.
90102
91103
Args:
92104
data: Complex valued input data containing at least 3 dimensions:
93105
dimensions -3 & -2 are spatial dimensions and dimension -1 has size
94106
2. All other dimensions are assumed to be batch dimensions.
107+
norm: Normalization mode. See ``torch.fft.ifft``.
95108
96109
Returns:
97110
The IFFT of the input.
@@ -102,7 +115,7 @@ def ifft2c_new(data: torch.Tensor) -> torch.Tensor:
102115
data = ifftshift(data, dim=[-3, -2])
103116
data = torch.view_as_real(
104117
torch.fft.ifftn( # type: ignore
105-
torch.view_as_complex(data), dim=(-2, -1), norm="ortho"
118+
torch.view_as_complex(data), dim=(-2, -1), norm=norm
106119
)
107120
)
108121
data = fftshift(data, dim=[-3, -2])

0 commit comments

Comments
 (0)