#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
double Real[1030];
double Imag[1030];
double GetReal[1030];
double GetImag[1030];
#define PI 3.141592654
void fft(double *Real,double *Imag,double *GetReal,double *GetImag,int n,int k);
int main()
{
int i, n, N, L;
while(1)
{
memset(Real, 0, sizeof(Real));
memset(Imag, 0, sizeof(Imag));
memset(GetReal, 0, sizeof(GetReal));
memset(GetImag, 0, sizeof(GetImag));
printf("Please input the number of xi/n");
scanf("%d",&n);
printf("Please input the Real and Imag/n");
for(i = 0; i< n ; i ++)
{
scanf("%lf",&Real[i]); //输入实部
// scanf("%lf",&Imag[i]); //输入虚部
}
for(i = 0; i < 11; i ++)
{
if(pow(2, i) >= n)
{
N = pow(2, i);
L = i;
break;
}
}
fft(Real, Imag, GetReal, GetImag, N, L);
for(i = 0; i < N; i ++)
{
if(GetImag[i] < 0)
printf("%lf-j%lf/n",GetReal[i],-GetImag[i]);
else
printf("%lf+j%lf/n",GetReal[i],GetImag[i]);
}
printf("/n**********************another test**************************/n");
}
return 0;
}
void fft(double *Real,double *Imag,double *GetReal,double *GetImag,int n,int k)
{
int i,j,temp,temp1,temp2;
double a,b;
double PodReal,PodImag;
for (j=0; j<=n-1; j++)
{
temp2 = j;
temp1 = 0;
for(i = 0; i <= k-1; i++)
{
temp = temp2/2;
temp1 = 2*temp1+temp2-2*temp;
temp2 = temp;
}
GetReal[j] = Real[temp1];
GetImag[j] = Imag[temp1];
}
a = PI * 2/(1.0*n);
Real[0] = 1.0;
Imag[0] = 0.0;
Real[1] = cos(a);
Imag[1] = -sin(a);
for(i = 2; i < n; i++)
{
Real[i] = Real[i-1] * Real[1] - Imag[i-1] * Imag[1];
Imag[i] = Real[i-1] * Imag[1] + Imag[i-1] * Real[1];
}
for(i=0; i<n-1; i+=2)
{
a = GetReal[i];
b = GetImag[i];
GetReal[i] = a + GetReal[i+1];
GetImag[i] = b + GetImag[i+1];
GetReal[i+1] = a - GetReal[i+1];
GetImag[i+1] = b - GetImag[i+1];
}
n = n/2;
temp1 = 2;
for(temp=0; temp<k-1; temp++)
{
n = n/2;
temp1=2*temp1;
for(i=0; i<=(n-1)*temp1; i=i+temp1)
{
for (j=0; j<(temp1/2); j++)
{
PodReal = Real[n*j] * GetReal[i+j+temp1/2] - Imag[n*j] * GetImag[i+j+temp1/2];
PodImag = Real[n*j] * GetImag[i+j+temp1/2] - Imag[n*j] * GetReal[i+j+temp1/2];
GetReal[i+j+temp1/2] = GetReal[i+j] - PodReal;
GetImag[i+j+temp1/2] = GetImag[i+j] - PodImag;
GetReal[i + j] = GetReal[i+j] + PodReal;
GetImag[i + j] = GetImag[i+j] + PodImag;
}
}
}
}