为什么我的Cooley-Tukey和Brute Force(Fourier)算法给出的结果大不相同?

时间:2019-04-28 21:03:27

标签: c++ algorithm fft complex-numbers

我写完了傅里叶算法。它在文本文件上实现了Cooley-Tukey和蛮力(Fourier变换)方法,其中一列表示时间(索引),另一列表示温度(开尔文)(值)。我的代码接收一个txt文件,然后执行暴力破解或cooley-tukey,最后将其输出为txt文件。蛮力只输出实数,Cooley-Tukey输出实数和虚数。

我也完全不熟悉物理学...我认为这是数学上的缺陷:

蛮力算法(.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>;

Cooley-Tukey算法(.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结果(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

1 个答案:

答案 0 :(得分:2)

真实价值的结果非常可疑。因此,我快速浏览了该代码。

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

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

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