为什么我的Cooley-Tukey和蛮力(傅立叶)算法给出的结果非常不同

Why are my Cooley-Tukey and Brute Force (Fourier) algorithms giving very different results?

本文关键字:结果 非常 算法 Cooley-Tukey 我的 傅立叶 为什么      更新时间:2023-10-16

我写完了我的傅里叶alogorithm代码。 它将 cooley-tukey 和蛮力(傅里叶变换(方法实现到文本文件上,其中一列表示时间(索引(,另一列表示以开尔文(值(为单位的温度。 我的代码接收一个 txt 文件,然后执行暴力破解或 cooley-tukey,最后将其输出为 txt 文件。 蛮力只输出实数,而库利-图基输出的是实数和虚数。

我也根本不熟悉物理...我认为这是数学上的一个缺陷:

暴力算法(.cpp文件(:

      void Brute_force<T>::DFT(std::vector<double>* index,
        std::vector<double>* value, 
        std::vector<complex<double>> &result)
      {
      // For each output element
      for (size_t freq = 0; freq < index->size(); freq += 
      this->frequency_step)
      {
        complex<double> sum(0.0, 0.0);
        // For each input element
        for (size_t time = 0; time < index->size(); time++)
        {
          double angle = 2 * M_PI * time * freq / index- 
          >size();
          sum = sum + index->at(time) * exp(-angle);
        }
        result.push_back(sum);
      }
    }
    template class Brute_force<double>;

库利-图基算法 (.cpp 文件(:

            void Cooley_tukey<T>::FFT(std::vector<T>* index, std::vector<T>* value, std::vector<complex<T>>& result)
            {
                std::cout << index->size() << std::endl;
                // Make copy of array and apply window
                for (unsigned int time = 0; time < index->size(); time++)
                {
                    result.push_back(index->at(time));
                    std::cout << result.at(time) << std::endl;
                    //temp.at(time) *= 1; // Window
                }
                // Start recursion function to split up the tasks
                FFT_REC(result, index->size());
            }

            template<typename T>
            void Cooley_tukey<T>::FFT_REC(std::vector<complex<T>>& result, int total_time)
            {
                // Check if it is split up enough
                if (total_time >= 2)
                {
                    // Split even and odds up
            std::vector<complex<T>> odd;
            std::vector<complex<T>> even;
            odd.reserve(total_time/2);
            even.reserve(total_time/2);
            for (int i = 0; i < total_time / 2; i++)
            {
                even.push_back(result.at(i*2));
                odd.push_back(result.at(i*2+1));
            }

                    // DFT portion of FFT - calculates after everything has been split up through FFT_REC
                    for (int frequency = 0; frequency < total_time / 2; frequency += this->frequency_step)
                    {
                        std::complex<T> t = exp(std::complex<T>(0, -2 * M_PI * frequency / total_time)) * odd.at(frequency);
                        //Result of Cooley-Tukey algorithm:
                            //*This gives us the frequency values at certain times
                        result.at(frequency) = even.at(frequency) + t;
                        result.at(total_time / 2 + frequency) = even.at(frequency) - t;
                    }
                }
            }

暴力破解结果(txt 文件((仅其中的一部分(:

    6.003e+06 0
    736788 0
    344823 0
    224583 0
    166575 0
    132447 0
    109977 0
    94064.6 0
    82206.1 0
    73027.7 0
    65713.2 0
    59747.3 0

Cooley-Tukey Results (txt 文件((仅其中的一部分(:

      4001 0
      4004.99 -6.28946
      4008.96 -12.5914
      4012.91 -18.9058
      4016.84 -25.2326
      4020.75 -31.5716
      4024.64 -37.9229
      4028.51 -44.2865
      4032.36 -50.6621
      4036.19 -57.0498
      4040 -63.4494

实值结果非常可疑。所以我快速浏览了一下代码。

您的指数中缺少一个 i。您计算实际值的指数,从而得出实际值。您需要使用

std::exp(std::complex<double>{0,-angle})

我没有看过代码的其余部分。正如我在评论中所说,您应该将结果与您知道正确的现有工具(MATLAB,Python/NumPy等(的结果进行比较,以缩小问题陈述的范围。