'Values of k to compute the derivative with fft

I get some trouble when I try to get the derivative of phi(x) by using the FFT.

l1=nk(T,n0)
l2=nk(T,n0)
l3=tetak(T,n0)
l4=tetak(T,n0)

dn=np.fft.irfft(l1+complex(0,1)*l2)*(N-2)/4
dtk=np.fft.irfft(l3+complex(0,1)*l4)*(N-2)/4

phi_x=np.sqrt(dn)*np.exp(complex(0,1)*dtk)

Here it is how I get phi(x) : I create my function phi(k) in the fourier space, and it is a symmetric function such that his fft has to be real. This is why I used np.fft.irfft. So my function is defined as phi(x)= np.fft.irfft(np.real(phi(k))+ 1j*np.imag(phi_k)) (I have written here phi(k) just to be clear).

l1,l2,l3,l4 are just lists of my fourier coefficients with length L, and the number (N-2)/4 is just to normalize.

And N=1000 here

Then I want to compute the second derivative. Thus, I apply a fft; then I multiply by -k**2 and then i take the ifft :

derivative=np.fft.ifft(-k*k*np.fft.fft(phi_x))

I know I have to be careful because np.fft.irfft gives me a length of (2*N-2).

I've already tried :

  1. k_tf1=np.arange(-999,999)*(2*np.pi/L)

999 because of the 2N-2 length of phi(x)

It doesn't work at all

  1. k_tf1 = np.fft.ifftshift(np.arange(-999, 999)) it doesnt'work too

  2. k_tf1=np.fft.fftfreq(phi_x.size,2*np.pi/(L)) It seems to work, but my values are too high.

I also try to add k_tf1=np.fft.fftshift(k_tf1), but it doesn't work.

If someone know the solution ,i would really appreciate !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source