There are several problems:
You are assigning complex values to the elements of
curve_abs2
, so it should be declared to be complex, e.g.curve_abs2 = np.empty_like(curve, dtype=np.complex128)
. (And I would recommend using the name, say,curve_fft
instead ofcurve_abs2
.)In python,
range(low, high)
gives the sequence[low, low + 1, ..., high - 2, high - 1]
, so instead ofrange(0, N - 1)
, you must userange(0, N)
(which can be simplified torange(N)
, if you want).You are missing a factor of 2 in your formula. You could fix this by using
z = 2j
.In the expression that is being summed in the inner loop, you are indexing
curve
ascurve[i]
, but this should becurve[k]
.Also in that expression, you don't need to subtract 1 from k, because the
k
loop ranges from 0 to N - 1.Because
k
andN
are integers and you are using Python 2.7, the division in the expression(k-1)/N
will be integer division, and you'll get 0 for allk
. To fix this and the previous problem, you can change that term tok / float(N)
.
If you fix those issues, when the first double loop finishes, the array curve_abs2
(now a complex array) should match the result of np.fft.fft(curve)
. It won't be exactly the same, but the differences should be very small.
You could eliminate that double loop altogether using numpy vectorized calculations, but that is a topic for another question.