为什么我的Cooley-Tukey和Brute Force(傅立叶)算法给出了截然不同的结果?

问题描述 投票:-1回答:1

我写完了我的傅里叶算法代码。它将cooley-tukey和蛮力(傅里叶变换)方法实现到文本文件中,其中一列用于时间(索引),另一列用于开尔文(值)的温度。我的代码接收一个txt文件,然后执行暴力破解或cooley-tukey,最后将其输出为txt文件。 Brute Force只出现实数,而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
c++ algorithm fft complex-numbers
1个回答
2
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.