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

### C语言实现快速傅里叶变换(FFT) #### 使用递归方法实现FFT 为了有效地处理复数运算和支持递归或分治策略,在C语言中实现快速傅里叶变换(FFT),可以通过定义必要的数据结构和函数来完成。下面提供了一个简化版本的一维离散傅里叶变换的快速算法示例[^2]。 ```c #include <stdio.h> #include <complex.h> #include <math.h> #define PI 3.14159265358979323846 void fft(complex double *a, int n, int inv) { if (n == 1) return; complex double even[n / 2], odd[n / 2]; for(int i = 0; i < n/2; ++i){ even[i] = a[i*2]; odd[i] = a[i*2+1]; } fft(even, n / 2, inv); fft(odd , n / 2, inv); for(int k = 0; k < n/2; ++k){ complex double t = cexp(-inv * 2.0j * PI*k/n)*odd[k]; a[k ] = even[k]+t; a[k+n/2 ] = even[k]-t; } } int main() { const int N = 8; complex double input[N] = { /* 初始化输入数组 */ }; fft(input, N, -1); // 正向变换 printf("Transformed data:\n"); for(int i=0;i<N;++i){ printf("%f + %fi\n", creal(input[i]), cimag(input[i])); } return 0; } ``` 此代码片段展示了如何利用`<complex.h>`库来进行复杂数字的操作,并通过递归来分割原始序列直到达到基本情况,即长度为1的情况。之后再逐步组合子问题的结果得到最终解法。 对于资源受限环境下的应用,比如嵌入式系统中的单片机,可能需要考虑使用定点算术代替浮点操作以节省时间和空间开销。在这种情况下,可以调整上述代码以便适应特定硬件平台的要求[^5]。 另外值得注意的是,虽然这里给出的例子采用了递归方式实现FFT,但在某些场景下迭代版可能会更高效;而且当面对较大规模的数据集时,还需要特别注意内存分配等问题[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值