Skip to content

Fix: correctly apply fftshift to real-valued data inputs #8407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions monai/networks/blocks/fft_utils_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,17 @@ def ifftn_centered_t(ksp: Tensor, spatial_dims: int, is_complex: bool = True) ->
output2 = ifftn_centered(ksp, spatial_dims=2, is_complex=True)
"""
# define spatial dims to perform ifftshift, fftshift, and ifft
shift = list(range(-spatial_dims, 0))
dims = list(range(-spatial_dims, 0))
if is_complex:
if ksp.shape[-1] != 2:
raise ValueError(f"ksp.shape[-1] is not 2 ({ksp.shape[-1]}).")
shift = list(range(-spatial_dims - 1, -1))
dims = list(range(-spatial_dims, 0))

x = ifftshift(ksp, shift)

if is_complex:
x = torch.view_as_real(torch.fft.ifftn(torch.view_as_complex(x), dim=dims, norm="ortho"))
x = torch.view_as_complex(ifftshift(ksp, [d - 1 for d in dims]))
else:
x = torch.view_as_real(torch.fft.ifftn(x, dim=dims, norm="ortho"))
x = ifftshift(ksp, dims)

x = torch.view_as_real(torch.fft.ifftn(x, dim=dims, norm="ortho"))

out: Tensor = fftshift(x, shift)
out: Tensor = fftshift(x, [d - 1 for d in dims])

return out

Expand Down Expand Up @@ -187,20 +183,16 @@ def fftn_centered_t(im: Tensor, spatial_dims: int, is_complex: bool = True) -> T
output2 = fftn_centered(im, spatial_dims=2, is_complex=True)
"""
# define spatial dims to perform ifftshift, fftshift, and fft
shift = list(range(-spatial_dims, 0))
dims = list(range(-spatial_dims, 0))
if is_complex:
if im.shape[-1] != 2:
raise ValueError(f"img.shape[-1] is not 2 ({im.shape[-1]}).")
shift = list(range(-spatial_dims - 1, -1))
dims = list(range(-spatial_dims, 0))

x = ifftshift(im, shift)

if is_complex:
x = torch.view_as_real(torch.fft.fftn(torch.view_as_complex(x), dim=dims, norm="ortho"))
x = torch.view_as_complex(ifftshift(im, [d - 1 for d in dims]))
else:
x = torch.view_as_real(torch.fft.fftn(x, dim=dims, norm="ortho"))
x = ifftshift(im, dims)

x = torch.view_as_real(torch.fft.fftn(x, dim=dims, norm="ortho"))

out: Tensor = fftshift(x, shift)
out: Tensor = fftshift(x, [d - 1 for d in dims])

return out
2 changes: 1 addition & 1 deletion tests/data/test_fft_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#
im = [[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]]
res = [
[[[0.0, 0.0], [0.0, 3.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]]
[[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [3.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]]
]
TESTS = []
for p in TEST_NDARRAYS:
Expand Down
Loading