FFT2 Non-zero imaginary part for centro-symmetric object?

Viewed 126

I'm currently testing the fft package from numpy. I created an artificial centro-symmetric image and I made a 2D FFT on it, I took the imaginary part and remarked that this is not equal to zero. I don't understand why, do you have any idea? Maybe there is something that I miss when I goes from 1D radial profile to a 2D image, I really don't know...

NEW UPDATE (new example) :

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt


I_min = 1.774E-6 #min(y_intensity[y_intensity!=0])
y = np.ones(50) #y_intensity[:50]
xv_3 = np.linspace(0,0.005,len(y))*1E3# x_intensity[:50]*1E3

new_r = np.linspace(-100,100,2**8)
interpol_index = interpolate.interp1d(xv_3, y)
X, Y = np.meshgrid(new_r,new_r)
profilegrid3 = np.ones(X.shape, float)
for i, x in enumerate(X[0, :]):
    for k, z in enumerate(Y[:, 0]):
        current_radius = np.sqrt(x**2 + z**2)
        
        cond=np.logical_or(current_radius>=max(xv_3),current_radius<=min(xv_3))
        if cond == True :
            profilegrid3[i, k] = I_min

        else :
            profilegrid3[i, k] = interpol_index(current_radius)

plt.figure()
plt.imshow(np.imag(np.fft.ifftshift(np.fft.fft2(np.fft.fftshift(profilegrid3)))))
plt.colorbar()
plt.show()

Image :

enter image description here

Imaginary Part :

enter image description here

1 Answers

There are two issues with your code:

  1. Use ifftshift to convert an image with the origin in the middle to have the origin in the top-left corner where the FFT expects it. And use fftshift to convert the output of the FFT to have the origin in the middle. (You’re using these the wrong way around.) But note that these two functions do exactly the same thing for even-sized arrays, so you will not notice a difference after you fix this.

  2. Your input must have the origin at size//2. The way you create new_r means that the origin falls right in between pixels. I think that adding endpoint=False to your np.linspace call fixes this.

Related