I am trying to calculate the power spectrum (1D) of a regular distributed 3D density database. But I keep getting the peak of power spectrum at 0. I am appreciating for your help!
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
bins = 1.
tbins = 400.
density = np.random.rand(400,400,400)
x, y, z = np.mgrid[0:400, 0:400, 0:400]
x = x - 199.5
y = y - 199.5
z = z - 199.5
dist = np.sqrt(x**2+y**2+z**2)
FT = np.fft.fftn(density)
power = FT.real*FT.real + FT.imag*FT.imag
P = power.reshape(np.size(power))
dist = dist.reshape(np.size(dist))
intervals = np.array([nn*bins for nn in range(0,int(tbins)+1)])
p = np.histogram(dist, bins=intervals, weights=P)[0]
pd = np.histogram(dist, bins=intervals)[0]
pd.astype('float')
p = p/pd
plt.figure()
plt.plot(2.*np.pi/intervals[1:], p)
plt.show()