您的位置:首页 > 编程语言 > C语言/C++

基-2 FFT算法的C++程序

2016-05-19 21:34 239 查看
基-2 FFT算法的C++程序,按时间抽选、输入倒位序、输出自然顺序,N=2LN=2^L

看了几天的其他资料,才慢慢清醒,教科书上的代码~~呵呵

#include <complex>
//教科书无该行代码
using namespace std;
//教科书int fft(complex *a,int l)
int fft(complex<double> *a,int l)
{
const double pai = 3.141592653589793;
complex<double> u,w,t;
unsigned n=1,nv2,nm1,k,le,lei,ip;
unsigned i,j,m;
double tmp;

n<<= 1;
nv2=n>>1;
nm1=n-1;
j = 0;    //教科书 i = 0;
for (i = 0; i < nm1; i++)
{
if (i < j)
{
t = a[j];
a[j] = a[i];
a[i] = t;
}
k = nv2;
while (k <= j)
{
j-=k;
k>>=1;
};
j+=k;
};
le=1;
for (m = 1; m <= 1; m++)
{
lei = le;
le <<= 1;
u = complex<double>(1, 0);
tmp = pai / lei;
w = complex<double>(cos(tmp), -sin(tmp));
for (j = 0; j < lei; j++)
{
for (i = j;i<n;i+=le)
{
ip = i + lei;
t = a[ip] * u;
a[ip] = a[i] - t;
a[i] += t;
}
u *= w;
}
}
return 0;
}


有助于对基-2 FFT算法的理解和掌握,对以后的时域/频域处理大有裨益。

有问题的代码,需要继续修改

#include <complex>
#include <iostream>
using namespace std;
int fft(complex<double> *a,int l)
{
const double pai = 3.141592653589793;
complex<double> u,w,t;
unsigned n=1,nv2,nm1,k,le,lei,ip;
unsigned i,j,m;
double tmp;

n<<= 1;
nv2=n>>1;
nm1=n-1;
j = 0;
for (i = 0; i < nm1; i++)
{
if (i < j)
{
t = a[j];
a[j] = a[i];
a[i] = t;
}
k = nv2;
while (k <= j)
{
j-=k;
k>>=1;
};
j+=k;
};
le=1;
for (m = 1; m <= 1; m++)
{
lei = le;
le <<= 1;
u = complex<double>(1, 0);
tmp = pai / lei;
w = complex<double>(cos(tmp), -sin(tmp));
for (j = 0; j < lei; j++)
{
for (i = j;i<n;i+=le)
{
ip = i + lei;
t = a[ip] * u;
a[ip] = a[i] - t;
a[i] += t;
}
u *= w;
}
}
return 0;
}
//int main()
//{
//complex<double> a[8] = {0,1,2,3,4,5,6,7};
//complex<double> *p;
//p = a;
//int fft(complex<double> *a, int l);
//fft( p,3);
//cout << "End" <<endl;
//return 0;
//system("pause");
}


.〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓.

复数z=a+bi,可用点Z(a,b)表示z=a+bi,可用点Z(a,b)表示

// complex constructor example
#include <iostream>     // std::cout
#include <complex>      // std::complex
using namespace std;

int main()
{
complex<double> first(2.0, 2.0);
complex<double> second(first);
complex<long double> third(second);

cout << "first= " << first << endl;
cout << "second= " << second << endl;
cout << "third= " << third  << endl;
system ("pause");
system ("cmd");
return 0;
}


.〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓.

FFT倒位序算法

#include <iostream>
using namespace std;

int main()
{
int array[8]={0,1,2,3,4,5,6,7};
int i,j,k;
int N = 8;
int temp;
j = 0;

for(i = 0; i < N -1; i ++)
{
if(i < j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}

k = N >> 1;

while( k <= j)
{
j = j - k;
k >>= 1;
}

j = j + k;
}

for( i = 0; i < N; i ++)
printf("%d ",array[i]);

printf("\n");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: