我的朋友們的一個錯誤,我現在編程FFT算法,我有以下程序:在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專業的編譯器。我不知道我的遞歸算法有什麼問題,所以我在VS中使用了調試模式,並且我一行一行地檢查了過程,但它似乎總是給我所有的值爲0。我用普通的FFT算法測試它,它工作得很完美。那麼,任何人都可以幫我看看我的程序嗎?我已經調試了大約4個小時,仍然找不到錯誤。也許,我做了一件非常愚蠢的事情,我沒有注意到。
(只是爲了您的通知,在FFT功能,爲了看到底發生了什麼,我也增添了許多COUT線)
謝謝你幫我出去!
請在這裏發佈您的代碼。 (要格式化,只需選擇它,然後在問題編輯器中單擊** {} **按鈕。) –
您知道如何插入一整塊代碼嗎?我只知道如何逐行插入代碼。謝謝! – Cancan
@OliCharlesworth,謝謝!有效。 – Cancan