I wrote a short Python code to compute the Discrete Fourier Transform of a signal. Here is the code.
#import packages
from numpy import *
import math
import cmath
def computedft(x):
#function to calculate the dft of a signal
#Inputs : x(t) time domain signal
#output : K Fourier coff magnitudes
N=len(x)
#Caluclate omeag for DFT
omega=2* math.pi /N
for k in range(N):
#outer loop is for each Fourier Coff
sum=0
for i in range(N):
sum=sum+x[i]*exp(- math.sqrt(-1)*k*omega*i)
X[k]=sum
return X
Then I went to ipython and gave these commands
In [1]: x=[9,6,7,8,9,-4,-8,-1]
In [2]: import computedft
In [3]: computedft.computedft(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~/Documents/<ipython console> in <module>()
~/Documents/computedft.py in computedft(x)
16 sum=0
17 for i in range(N):
---> 18 sum=sum+x[i]*exp(- math.sqrt(-1)*k*omega*i)
19 X[k]=sum
20 return X
ValueError: math domain error
I dont understand why I am getting this error. I have included the cmath package, so thus it should be able to multiply complex numbers. But why doesnt it work?