Hey all, so I’m implementing the simple version of the discrete fourier transform in python for a class, but something strange is happening to my output and I have no idea why. For example if my input is [a,b,c,d] the values are getting scrambled like [a’,d’,c’,b’] with the head of the list in the same place, but the other values reversed. This is my code:
def DFT(pts, carr):
t = time.clock()
F =[0]*pts
for k in range(pts):
c = 2j*k*math.pi/pts
for n in range(pts):
F[k] += complex(carr[n]) * cmath.exp(n*c)
t = time.clock()-t
print(str(pts) + " point DFT finished in " + str(t) + "s")
return F
It’s super simple, but for some reason it’s flipping the array around all willy-nilly and I have no idea why. Does someone know what’s going on here? I’m certain the input is in the right order.
I believe you’re missing a negative sign in the calculation of
c(see this link).With your code:
With the missing negative sign: