4

我尝试计算这个数组的 DFT x_1。它一定很简单,但我的价值观太低了。我的代码有什么问题?

不要链接到其他示例 - 只是为我自己的代码寻找修复。

#include <iostream>
#include <complex>
#include <cassert>

int main ()
{
    const unsigned int N = 20;

    const double x_1[N] = {0, 0.3, 0.6, 0.8, 1, 1, 0.9, 0.7, 0.5, 0.2, 0.2, 0.5, 0.7, 0.9, 1, 1, 0.8, 0.6, 0.3, 0};

    for(unsigned int k = 0; k < N; k++)
    {
        std::complex<double> sum(0.0,0.0);
        for(unsigned int j = 0; j < N; j++)
        {
            int integers = -2*j*k;
            std::complex<double> my_exponent(0.0, M_PI/N*(double)integers);
            sum += x_1[j] * std::exp(my_exponent);
        }
        std::cout << abs(sum)/N << std::endl;
    }
    return 0;
} 
4

2 回答 2

5
std::cout << abs(sum)/N << std::endl;

你为什么要除以N?

系数没有除法。见维基

这些是我用 Matlab 得到的值:

12.0000000000000 + 0.00000000000000i
-0.971586454726535 - 0.153884176858763i
-4.26246117974981 - 1.38495759172886i   
-0.0712959999079796 - 0.0363271264002681i   
-0.473606797749979 - 0.344095480117793i
0.00000000000000 + 0.00000000000000i
-0.237538820250189 - 0.326944137602412i
0.0185095954079375 + 0.0363271264002681i
-0.0263932022500213 - 0.0812299240582274i
0.0243728592265771 + 0.153884176858763i
0.00000000000000 + 0.00000000000000i    
0.0243728592265771 - 0.153884176858763i
-0.0263932022500213 + 0.0812299240582274i
0.0185095954079375 - 0.0363271264002681i    
-0.237538820250189 + 0.326944137602412i 
0.00000000000000 + 0.00000000000000i    
-0.473606797749979 + 0.344095480117793i 
-0.0712959999079796 + 0.0363271264002681i
-4.26246117974981 + 1.38495759172886i   
-0.971586454726535 + 0.153884176858763i

当我使用 std::cout << sum << std::endl; 打印时,我看到或多或少相同的值

于 2012-05-03T15:07:12.893 回答
0

我认为这条线integers = -2*j*k不应该有减号(-),因为这不是逆变换

于 2014-07-04T08:03:35.230 回答