如果迭代,DFT会给出不同的结果

时间:2012-04-30 01:41:15

标签: python math dft

我创建了一个简单的积分函数和DFT函数,我可以将其与我编写的其他代码一起使用。

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)

此代码的输出为249.028500022,接近预期的250值。然而,当我迭代DFT时,我得到一个完全不同的变换值为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

此代码的输出是: 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

谁能告诉我导致这个问题的原因是什么?提前谢谢。

注意:此时我并不关心使用更高效的fft功能。样本量不大,所以没关系。

1 个答案:

答案 0 :(得分:1)

partialdft功能会修改x。这是第一个循环之后的x

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

调用函数后,这是x

>>> 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]

为避免覆盖x,请复制一份:

def partialdft(d,step,w):
    i = 0
    x = d[:]
    #...
相关问题