简体   繁体   English

如果重复,DFT会给出不同的结果

[英]DFT gives different results if iterated

I created a simple integral function and DFT function that I can use with some other code I wrote. 我创建了一个简单的积分函数和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)

This code gives an output of 249.028500022 which is close to the expected 250 value. 此代码的输出为249.028500022,接近预期的250值。 However when I iterate the DFT i get an entirely different value for the transform at 4. 但是,当我迭代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

The output for this code is: 0 0.0514628731431 该代码的输出是:0 0.0514628731431

0.1 0.0514628731431 0.1 0.0514628731431

0.2 0.0514628731431 0.2 0.0514628731431

. . . .

4.0 0.0514628731431 4.0 0.0514628731431

. . . .

9.8 0.0514628731431 9.8 0.0514628731431

9.9 0.0514628731431 9.9 0.0514628731431

10.0 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. 注意:目前,我不在乎使用更高效的fft函数。 The sample size isn't large so it doesn't matter. 样本数量不大,所以没关系。

partialdft function modifies x . partialdft函数修改x Here's x after the first loop: 这是第一个循环后的x

>>> 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: 调用函数后为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]

To avoid overwriting x , make a copy: 为避免覆盖x ,请复制:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM