I created a simple integral function and DFT function that I can use with some other code I wrote.
from math import sin,pi
from time import time
def aintegral(d,step):
return sum(d)*step
def partialdft(d,step,w):
i = 0
x = d
while i/step < len(d):
x[int(i/step)]*=sin(w*pi*i)
i+=step
return aintegral(x,step)
x = []
y = 0
while y<100:
x.append(5*sin(4*pi*y))
y+=.01
print partialdft(x,.01,4)
This code gives an output of 249.028500022 which is close to the expected 250 value. However when I iterate the DFT i get an entirely different value for the transform at 4.
from math import sin,pi
from time import time
def aintegral(d,step):
return sum(d)*step
def partialdft(d,step,w):
i = 0
x = d
while i/step < len(d):
x[int(i/step)]*=sin(w*pi*i)
i+=step
return aintegral(x,step)
x = []
y = 0
while y<100:
x.append(5*sin(4*pi*y))
y+=.01
y = 0
while y<10.:
print y,partialdft(x,.01,y)
y+=.1
The output for this code is:
0 0.0514628731431
0.1 0.0514628731431
0.2 0.0514628731431
.
.
.
.
4.0 0.0514628731431
.
.
.
.
9.8 0.0514628731431
9.9 0.0514628731431
10.0 0.0514628731431
Can anyone tell me what is causing this problem? Thanks in advance.
Note: At this time I don’t care about using a more efficient fft function. The sample size isn’t large so it doesn’t matter.
partialdftfunction modifiesx. Here’sxafter the first loop:Here’s
xafter you call the function:To avoid overwriting
x, make a copy: