问题
import numpy as np
import matplotlib.pyplot as pp
curve = np.genfromtxt('C:\Users\latel\Desktop\kool\Neuro\prax2\data\curve.csv',dtype = 'float', delimiter = ',')
curve_abs2 = np.empty_like(curve)
z = 1j
N = len(curve)
for i in range(0,N-1):
curve_abs2[i] =0
for k in range(0,N-1):
curve_abs2[i] += (curve[i]*np.exp((-1)*z*(np.pi)*i*((k-1)/N)))
for i in range(0,N):
curve_abs2[i] = abs(curve_abs2[i])/(2*len(curve_abs2))
#curve_abs = (np.abs(np.fft.fft(curve)))
#pp.plot(curve_abs)
pp.plot(curve_abs2)
pp.show()
The code behind # gives me 3 values. But this is just ... different
Wrong ^^ this code: http://www.upload.ee/image/3922681/Ex5problem.png
Correct using numpy.fft.fft(): http://www.upload.ee/image/3922682/Ex5numpyformulas.png
回答1:
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_fftinstead 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
curveascurve[i], but this should becurve[k].Also in that expression, you don't need to subtract 1 from k, because the
kloop ranges from 0 to N - 1.Because
kandNare integers and you are using Python 2.7, the division in the expression(k-1)/Nwill 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.
来源:https://stackoverflow.com/questions/22172619/manual-fft-not-giving-me-same-results-as-fft