如果迭代,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函数。样本量不大,所以没关系。

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[:]
    #...

最新更新