Question concerning fftshift in FFT operations. #301
-
Hello, I have a question concerning the I include my test code below. import numpy as np
from scipy import signal
def test_fft2_noshift():
a = np.random.random((17, 17))
b = np.random.random((17, 17))
c1 = signal.convolve2d(a, b, mode='full')
ar = np.fft.fft2(a, s=(34, 34))
br = np.fft.fft2(b, s=(34, 34), norm='forward')
c2 = np.fft.ifft2(ar * br, s=(34, 34), norm='forward')[:-1, :-1]
assert np.allclose(c1, c2)
def test_fft2_shifted():
a = np.random.random((17, 17))
b = np.random.random((17, 17))
c1 = signal.convolve2d(a, b, mode='full')
ar = np.fft.fft2(np.fft.ifftshift(a), s=(34, 34))
br = np.fft.fft2(np.fft.ifftshift(b), s=(34, 34), norm='forward')
cr = np.fft.fftshift(ar * br)
c2 = np.fft.ifft2(cr, s=(34, 34), norm='forward')[:-1, :-1]
assert not np.allclose(c1, c2) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hello @veritas9872, I think there should be some clarifications of what is going on with FFT. The FFT is well-defined to apply the Discrete Fourier Transform, which by convention always starts at 0 and goes to N-1. Effectively, this means the 0-phase is the first element and the middle element is the highest phase offset (or frequency in Fourier space). Conventionally with images, we don't usually think of the left side of the image as being the 0-phase position, so we use FFT shifts because they're more intuitive and they match our point-of-reference for how complex phase works across MRIs. I think the reason you're getting an incorrect result here is due to the Practically we don't use FFTs for convolutions like this in the repository. We use FFTs because they represent the physics of an MRI scanner, and the code in the repository is configured (and has been extensively tested) to reconstruct real-world MRI data. |
Beta Was this translation helpful? Give feedback.
-
It just depends where you shift! Shifting before the FFT and ittshifting at the very end produces the same results since the convolution acts everywhere the same. You always need to remember: |
Beta Was this translation helpful? Give feedback.
Hello @veritas9872, I think there should be some clarifications of what is going on with FFT. The FFT is well-defined to apply the Discrete Fourier Transform, which by convention always starts at 0 and goes to N-1. Effectively, this means the 0-phase is the first element and the middle element is the highest phase offset (or frequency in Fourier space). Conventionally with images, we don't usually think of the left side of the image as being the 0-phase position, so we use FFT shifts because they're more intuitive and they match our point-of-reference for how complex phase works across MRIs.
I think the reason you're getting an incorrect result here is due to the
s=(34, 34)
, which applies…