# inverse discrete fourier transfor with plain python

I am trying to calculate inverse discrete fourier transform for an array of signals.

I am using the following formula:

And my python code looks as follow.

def IFT(array):
array = np.asarray(array, dtype=float)
# array length
N = array.shape[0]
# new array of lenght N [0, N-1]
n = np.arange(N)
k = n.reshape((N, 1))
# Calculate the exponential of all elements in the input array.
M = np.exp(2j * np.pi * k * n / N)
return 1 / N * np.dot(M, array)

y = 2*np.sin(2*np.pi*f1*t+0.2) + 3*np.cos(2*np.pi*f2*t+0.3) + noise*5
IFT(sc.fft(y))

array([ 3.42732136e+00+0.00000000e+00j,  4.81582993e+00-4.10374772e-16j,
5.86456023e+00+3.88824673e-16j,  4.07942919e+00-1.48609552e-15j,
5.25077594e+00+6.70968395e-16j,  6.45823471e+00-1.22977116e-15j,
...
sc.ifft(sc.fft(y))

array([ 3.42732136-8.59756710e-16j,  5.34401724+4.97379915e-17j,
5.46939384-9.87654403e-16j,  3.31504852-4.17443857e-16j,
6.52888175+6.39488462e-16j,  6.90804001-3.10862447e-16j,
...


but if I compare array returned from Scipy.ifft with the one returned from my own function IFT, I get completely different array. Am I missing something?

• Are you confusing n and N? – MBaz May 28 at 19:26
• You are right, but still there is something wrong – Rokas.ma May 28 at 19:29
• I tested your function's body code and it works fine ? (checked with Matlab's ifft)... May be you are mis-using scipy.ifft ? – Fat32 May 28 at 23:52
• Really? I added my comparison with Scipy, results are slightly different – Rokas.ma May 31 at 6:37

First of all, I think it'd be good to include your result vs the result given by the mentioned library.

Here is the code of scipy's ifft. In the docs it states that the function returns a

complex array contains y(0), y(1),..., y(n-1) where

y(j) = (x * exp(2*pi*sqrt(-1)*j*np.arange(n)/n)).mean().

I suspect that you're trying to write the imaginary unit as $$j$$, and I'm not sure that works fine... try replacing it with sqrt(-1).

PS: Just noticed, but you want to compute idft on an array of signals? Or an array which represents a signal?

• I added the results – Rokas.ma May 29 at 6:48