为什么我的Cooley-Tukey和蛮力(傅立叶)算法给出的结果非常不同
Why are my Cooley-Tukey and Brute Force (Fourier) algorithms giving very different results?
我写完了我的傅里叶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等(的结果进行比较,以缩小问题陈述的范围。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- OpenGL大的3D纹理(>2GB)非常慢
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在更改for循环的第三部分后,未使用for循环结果
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 我在运行函数 GetVolumeInformation() 时得到非常随机的结果
- rand() 和 srand() 给出了奇怪的相似结果.来自 rand() 的返回非常相似
- 为什么我的Cooley-Tukey和蛮力(傅立叶)算法给出的结果非常不同
- (欧拉计划#3)试图将Python中的解决方案复制到C++,结果非常糟糕,不知道如何
- C++非常令人昏昏欲睡的奇怪结果
- std::sort给出了非常奇怪的结果
- 使用SURF和FLANN跟踪标记的结果非常糟糕
- MPI_Allreduce:非常奇怪的错误结果