2d fft numpy/python confusion
<p>I have data in the form x-y-z and want to create a power spectrum along x-y. Here is a basic example I am posting to check where I might be going wrong with my actual data:</p>

<pre><code>import numpy as np
from matplotlib import pyplot as plt

fq = 10; N = 20
x = np.linspace(0,8,N); y = x
space = x[1] -x[0]

xx, yy = np.meshgrid(x,y)
fnc = np.sin(2*np.pi*fq*xx)
ft = np.fft.fft2(fnc)
ft = np.fft.fftshift(ft)

freq_x  = np.fft.fftfreq(ft.shape[0], d=space)
freq_y  = np.fft.fftfreq(ft.shape[1], d=space)
plt.imshow(
    abs(ft),
    aspect='auto',
    extent=(freq_x.min(),freq_x.max(),freq_y.min(),freq_y.max())
)
plt.figure()
plt.imshow(fnc)
</code></pre>

<p>This results in the following <a href="http://i.stack.imgur.com/WWcN5.png" rel="nofollow">function</a> &amp; <a href="http://i.stack.imgur.com/xooyg.png" rel="nofollow">frequency</a> figures with the incorrect frequency. Thanks.</p>

<p>One of your problems is that matplotlib's <code>imshow</code> using a different coordinate system to what you expect. Provide a <code>origin='lower'</code> argument, and the peaks now appear at <code>y=0</code>, as expected.</p>

<p>Another problem that you have is that <code>fftfreq</code> needs to be told your timestep, which in your case is <code>8 / (N - 1)</code></p>

<pre><code>import numpy as np
from matplotlib import pyplot as plt

fq = 10; N = 20
x = np.linspace(0,8,N); y = x
xx, yy = np.meshgrid(x,y)
fnc = np.sin(2*np.pi*fq*xx)
ft = np.fft.fft2(fnc)
ft = np.fft.fftshift(ft)

freq_x  = np.fft.fftfreq(ft.shape[0], d=8 / (N - 1))  # this takes an argument for the timestep
freq_y  = np.fft.fftfreq(ft.shape[1], d=8 / (N - 1))
plt.imshow(
    abs(ft),
    aspect='auto',
    extent=(freq_x.min(),freq_x.max(),freq_y.min(),freq_y.max()),
    origin='lower' ,          # this fixes your problem
    interpolation='nearest',  # this makes it easier to see what is happening
    cmap='viridis'            # let's use a better color map too
)
plt.grid()
plt.show()
</code></pre>

<p><a href="http://i.stack.imgur.com/kv77L.png" rel="nofollow"><img src="http://i.stack.imgur.com/kv77L.png" alt="enter image description here"></a></p>

<p>You may say <em>"but the frequency is 10, not 0.5!"</em> However, if you want to sample a frequency of <code>10</code>, you need to sample a lot faster than <code>8/19</code>! Nyquist's theorem says you need to <strong>exceed</strong> a sampling rate of 20 to have any hope at all</p>

python numpy fft