分享

网上找的纯C实现的FFT,与matlab计算结果完全一样

 imelee 2018-08-07

直接上代码了

fft.c

  1. #include "math.h"
  2. #include "fft.h"
  3. //精度0.0001弧度
  4. void conjugate_complex(int n,complex in[],complex out[])
  5. {
  6. int i = 0;
  7. for(i=0;i<n;i++)
  8. {
  9. out[i].imag = -in[i].imag;
  10. out[i].real = in[i].real;
  11. }
  12. }
  13. void c_abs(complex f[],float out[],int n)
  14. {
  15. int i = 0;
  16. float t;
  17. for(i=0;i<n;i++)
  18. {
  19. t = f[i].real * f[i].real + f[i].imag * f[i].imag;
  20. out[i] = sqrt(t);
  21. }
  22. }
  23. void c_plus(complex a,complex b,complex *c)
  24. {
  25. c->real = a.real + b.real;
  26. c->imag = a.imag + b.imag;
  27. }
  28. void c_sub(complex a,complex b,complex *c)
  29. {
  30. c->real = a.real - b.real;
  31. c->imag = a.imag - b.imag;
  32. }
  33. void c_mul(complex a,complex b,complex *c)
  34. {
  35. c->real = a.real * b.real - a.imag * b.imag;
  36. c->imag = a.real * b.imag + a.imag * b.real;
  37. }
  38. void c_div(complex a,complex b,complex *c)
  39. {
  40. c->real = (a.real * b.real + a.imag * b.imag)/(b.real * b.real +b.imag * b.imag);
  41. c->imag = (a.imag * b.real - a.real * b.imag)/(b.real * b.real +b.imag * b.imag);
  42. }
  43. #define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
  44. void Wn_i(int n,int i,complex *Wn,char flag)
  45. {
  46. Wn->real = cos(2*PI*i/n);
  47. if(flag == 1)
  48. Wn->imag = -sin(2*PI*i/n);
  49. else if(flag == 0)
  50. Wn->imag = -sin(2*PI*i/n);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多