I've implemented DFT and the inverse DFT Function according to the following formulas:
The DFT function works, but when testing the inverse on the output I don't get the original series.
import numpy as np
import random
exp = np.exp
pi = np.pi
def mydft(X):
n = len(X)
out = []
for k in range(n):
temp = 0
for i in range(n):
temp += X[i] * exp(-2j*pi*k*i/n)
out.append(temp)
return np.array(out)
def myidft(X):
n = len(X)
out = []
for k in range(n):
temp = 0
for i in range(n):
temp += X[i] * exp(2j*pi*k*i/n)
out.append(temp)
return (1/n) * np.array(out)
Testing
orig = np.random.random(100)
trans = mydft(orig)
inv = myidft(trans)
print(np.allclose(inv, trans))
>>> False
print(np.allclose(trans, np.fft.fft(orig)))
>>> True
Since the original Function works and the modifications for the inverse are quite simple, I have no clue what went wrong!?