FFT 算法中的一个错误
A bug in FFT Algorithm
我的朋友们,我现在正在编写FFT算法,我有以下程序:
#include<iostream>
#include<vector>
#include<complex>
#include<cmath>
#define _USE_MATH_DEFINES
#include<math.h>
#include<stdlib.h>
using namespace std;
const complex<double> I(0,1);
const int pi = M_PI;
//This function will check if a number is a power of certain number
bool checkpower(float base,float num){
float a = ceil(log(num)/log(base))-floor(log(num)/log(base));
if (a==0){
return 1;
}
else{
return 0;
}
}
//Fast Fourier Transform for DFT
vector<complex<double>> FFT_DFT(vector<complex<double>> samples){
int N = samples.size();
cout << N << endl;
if(!checkpower(2,N)){
cout << "Please make sure the sample size is of 2^n!" << endl;
exit (EXIT_FAILURE);
}
else{
vector<complex<double>> F(N);
if(N==1){
F.push_back(samples[0]);
}
else{
int M = N/2;
cout << M << endl;
vector<complex<double>> O; //vector to store the odd elements
vector<complex<double>> E; //vector to store the even elements
//Reoder the samples
for(int l=0;l<M;l++){
E.push_back(samples[2*l]);
O.push_back(samples[2*l+1]);
}
vector<complex<double>> ODFT;
cout << "Start recursive for odd" << endl;
ODFT = FFT_DFT(O);
vector<complex<double>> EDFT;
cout << "Start recursive for even" << endl;
EDFT = FFT_DFT(E);
for(int k=0;k<M;k++){
cout << real(EDFT[k]) << " + "<< imag(EDFT[k]) << "I" << endl;
cout << real(ODFT[k]) << " + "<< imag(ODFT[k]) << "I" << endl;
F[k] = EDFT[k]+exp(-2.0*pi*k*I/(double)N)*ODFT[k];
F[k+M] = EDFT[k]-exp(-2.0*pi*k*I/(double)N)*ODFT[k];
cout << real(F[k]) << " + "<< imag(F[k]) << "I" << endl;
cout << real(F[k+M]) << " + "<< imag(F[k+M]) << "I" << endl;
}
}
return F;
}
}
int main(){
vector<complex<double>> samples;
samples.push_back(8.0);
samples.push_back(4.0);
samples.push_back(8.0);
samples.push_back(0.0);
vector<complex<double>> dft = FFT_DFT(samples);
vector<complex<double>>::iterator item;
for(item=dft.begin();item!=dft.end();item++){
cout << real(*item) << " + "<< imag(*item) << "I" << endl;
}
return 0;
}
我使用Visual Studio 2010 Professional作为编译器。我不知道我的递归算法出了什么问题,所以我在 VS 中使用了调试模式并逐行检查了过程,但似乎它总是为所有值给我 0。我用普通的FFT算法测试了它,它工作得很好。 那么,谁能帮我看看我的程序?我已经调试了大约 4 个小时,但仍然找不到错误。也许,我做了一件非常愚蠢的事情,我没有注意到。
(只是为了引起您的注意,在FFT功能中,为了看看到底发生了什么,我还添加了很多cout线(
谢谢你帮助我!
替换
F.push_back(samples[0]);
将一个元素添加到F
- 已经有一个元素,零 - 与
F[0] = samples[0];
您将使用一个向量结束递归,该向量以零作为其第一个元素,样本作为其第二个元素。
由于您后来只使用第一个元素,因此所有内容都变为零。
我不能立即说出您的问题是什么,但您至少有一个严重的错误。这里:
float a = ceil(log(num)/log(base))-floor(log(num)/log(base));
if (a==0){
return 1;
}
除了有效的比较是0.0f
之外,您不应该进行一些浮点计算并期望该值是一个特定的数字。在你期望0.0
的地方,它实际上可能是类似0.00000000132
的东西。因为这就是浮点数的工作方式。不幸的是,他们总是这样工作,并且会这样工作。
至于你的FFT计算,我会更仔细地研究你分配F[k]
和F[k+M]
的行。在这些中,您将值乘以ODFT[k]
,我怀疑ODFT[k]
可能在那里0
。
相关文章:
- 试图修复一个错误,该错误不会让我开始编程其余部分
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 我正在尝试一个傻瓜 C++ 练习,我遇到了一个错误,说类 'GraduateStudent' 没有任何名为 'advisor' 的字段
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- 在Cython中使用C库时,我遇到了一个错误
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 我试图用c++编写递归fibonacci序列,但当我编译时,我遇到了一个错误
- 从system()调用G++会返回一个错误
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 使用声明:GCC 和 Clang 的另一个错误?
- 全球免费给出一个错误.调试器不解释
- 相对于继承的构造函数,gcc 编译器是否还有一个错误?
- 为什么直接传递"this"指针来存档是一个错误,而另一个相同类型的指针是可以的?
- 为什么第三板有一个错误
- 如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
- 使用本地类型声明的G lambda被使用但从未定义 - 确实是一个错误
- 我需要在 Android Studio 中构建 NDK. 但它返回一个错误