fft的c语言程序

#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;
   }
  }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值