您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> C十十 >> 辅导 >> 正文    
  C++基础(快速傅里叶变换(FFT)源代码) 【注册男护士专用博客】          

C++基础(快速傅里叶变换(FFT)源代码)

www.nanhushi.com     佚名   不详 

  为了看明白那堆积分变换,不得不把复变函数扫了一遍,可看完了,才发现原来这堆变换说白了只是一些数字游戏,Examda提示: 也没用到啥复变函数的知识。最后,用C++程序实现了下FFT,也算告一段落,代码如下:
  #include <iostream>
  #include <fstream>
  #include <math.h>
  using namespace std;
  const double PI = 3.14159265358979323846;
  int n; // 数据个数 = 2的logn次方
  int logn;
  /// 复数结构体
  struct stCompNum
  {
  double re;
  double im;
  };
  stCompNum* pData1 = NULL;
  stCompNum* pData2 = NULL;
  /// Examda提示: 正整数位逆序后输出
  int reverseBits(int value, int bitCnt)
  {
  int i;
  int ret = 0;
  for(i=0; i<bitCnt; i++)
  {
  ret |= (value & 0x1) << (bitCnt - 1 - i);
  value >>= 1;
  }
  return ret;
  }
  void main()
  {
  ifstream fin("data.txt");
  int i,j,k;
  // input logn
  fin>>logn;
  // calculate n
  for(i=0, n=1; i<logn; i++) n *= 2;
  // malloc memory space
  pData1 = new stCompNum[n];
  pData2 = new stCompNum[n];
  // input raw data
  for(i=0; i<n; i++) fin>>pData1[i].re;
  for(i=0; i<n; i++) fin>>pData1[i].im;
  // FFT transform
  int cnt = 1;
  for(k=0; k<logn; k++)
  {
  for(j=0; j<cnt; j++)
  {
  int len = n / cnt;
  double c = - 2 * PI / len;
  for(i=0; i<len/2; i++)
  {
  int idx = len * j + i;
  pData2[idx].re = pData1[idx].re + pData1[idx + len/2].re;
  pData2[idx].im = pData1[idx].im + pData1[idx + len/2].im;
  }
  for(i=len/2; i<len; i++)
  {
  double wcos = cos(c * (i - len/2));
  double wsin = sin(c * (i - len/2));
  int idx = len * j + i;
  stCompNum tmp;
  tmp.re = pData1[idx - len/2].re - pData1[idx].re;
  tmp.im = pData1[idx - len/2].im - pData1[idx].im;
  pData2[idx].re = tmp.re * wcos - tmp.im * wsin;
  pData2[idx].im = tmp.re * wsin + tmp.im * wcos;
  }
  }
  cnt <<= 1;
  stCompNum* pTmp = NULL;
  pTmp = pData1;
  pData1 = pData2;
  pData2 = pTmp;
  }
  // resort
  for(i=0; i<n; i++)
  {
  int rev = reverseBits(i, logn);
  stCompNum tmp;
  if(rev > i)
  {
  tmp = pData1[i];
  pData1[i] = pData1[rev];
  pData1[rev] = tmp;
  }
  }
  // output result data
  for(i=0; i<n; i++) cout<<pData1[i].re<<"\t";
  cout<<endl;
  for(i=0; i<n; i++) cout<<pData1[i].im<<"\t";
  cout<<endl;
  // free memory space
  delete []pData1;
  delete []pData2;
  fin.close();
  system("pause");
  }
  输入文件data.txt的内容如下:
  4
  2.2 4.5 6.7 8.5 10.2 12.3 14.5 16.2 19.3 21.2 25.2 29.4 36.4 39.2 45.2 50.1
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 来源:考

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目

      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)                            【进男护士社区逛逛】
    姓 名:
    * 游客填写  ·注册用户 ·忘记密码
    主 页:

    评 分:
    1分 2分 3分 4分 5分
    评论内容:
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。