DFT迭代得到不同的结果

2024-04-26 13:41:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个简单的积分函数和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处得到了一个完全不同的值。在

^{pr2}$

此代码的输出为: 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函数。样本量不大,所以没关系。在


Tags: 函数代码fromimportreturntimedefstep
1条回答
网友
1楼 · 发布于 2024-04-26 13:41:42

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如下:

^{pr2}$

要避免覆盖x,请制作一个副本:

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

相关问题 更多 >