Pregunta

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.

¿Fue útil?

Solución

partialdft function modifies x. Here's x after the first loop:

>>> x[0:10]
[0.0, 0.62666616782152129, 1.243449435824274, 1.8406227634233896, 2.4087683705085765, 2.9389262614623659, 3.4227355296434436, 3.852566213878946, 4.2216396275100756, 4.5241352623300983]

Here's x after you call the function:

>>> partialdft(x, 0.01, y)
0.051462873158853464
>>> x[0:10]
[0.0, -2.8072359998573911e-13, 1.114040042207106e-12, -2.4744131119314365e-12, 4.316161702819329e-12, -6.5865746141630883e-12, 9.202604511389696e-12, -1.2082375495190468e-11, 1.5129125329320302e-11, -8.1617793532956823e-23]

To avoid overwriting x, make a copy:

def partialdft(d,step,w):
    i = 0
    x = d[:]
    #...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top