fftw3算法库

库版本:fftw-3.3.10

1、头文件

#include <fftw3.h>

//实事求事,算法库很NB,但这接口设计方式真心对使用者不友好,完全不能直观的使用接口,还得去仔细去分解那些写得相对复杂的宏原理

2、创建输入输出内存块

double *r2cIn = (double *)fftw_malloc(sizeof(double) * (FFT_LEN + 1));
fftw_complex *r2cOut = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * (FFT_LEN + 1));

网上建议使用fftw_malloc申请内存,主要是涉及到内存对齐相关的内容,方便后续高性能指令齐优化使用

3、创建rfft/irfft句柄

fftw_plan handleP = fftw_plan_dft_r2c_1d(FFT_LEN, r2cIn, r2cOut, FFTW_ESTIMATE);
fftw_plan handleB = fftw_plan_dft_c2r_1d(window, dout, din, FFTW_ESTIMATE);

***fftw_plan_dft_c2r_1d对应的算法输出结果需要除以FFT_LEN才能得到实际的频域数据***
根据使用及该库的接口设计,基本确定r2cIn和r2cOut为常驻内存块,给handleP专用,

4、填充数据
创建句柄handleP后,给r2cIn输入内存块添加待处理的信号

5、执行处理程序

fftw_execute(handleP);

每次填充完成数据后,调用该接口

6、销毁
xxxxx

示例:

   #include <stdio.h>
   #include <unistd.h>
   #include <string.h>
   #include <stdlib.h>
   #include <fftw3.h>
   #include <sys/time.h>
    /* python np.rfft的结果
[ 900.          +0.j          -75.52811087+135.48370223j
  -89.26960212 -36.89088019j -141.76211763 +52.35641639j
  144.53910524 -47.87005769j  -18.87933361+144.12556982j
  -37.07400191 +56.05170075j  -63.94721021+121.1031161j
   14.         -46.j          -31.86080989 +67.67386715j
  -31.16863878 +12.15220581j  -10.16093173 -15.92191832j
   -2.53910524  -5.87005769j  -87.62402389 +45.36275212j
   29.51224281 -12.79037513j   -2.23746216 +59.10827716j
  -92.          +0.j        ]
*/
  
  //采用的double对齐的接口
  int main(int args, char *argv[]) {
      int window = 32;
  
      int ret = 0;
      int index = 0;
      struct timeval tv, tv1;
      short int16_t_buf[32] = {
          0, 1, 10, 20, 31, 21, 29, 30,
          32, 41, 12, 13, 15, 15, 14, 12,
          54, 65, 43, 32, 12, 43, 12, 43,
          54, 64, 43, 31, 11, 21, 32, 44
      };
  
      double* din  = (double *)fftw_malloc(sizeof(double)*(window + 1));
      fftw_complex* dout = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*(window + 1));
  	 //fftw_complex == >  typedef double fftw_complex[2]
  	 //fftw_complex等同于一个一维两数据的double数组

  	 //创建rfft句柄 
      fftw_plan p = fftw_plan_dft_r2c_1d(window, din, dout, FFTW_ESTIMATE);
	  //创建irfft句柄
	  fftw_plan b = fftw_plan_dft_c2r_1d(window, dout, din, FFTW_ESTIMATE);
	  //测试代码rfft与irfft共用内存块了(dout,din)

	 //如果有多次数据处理,每次填充完成,再调用fftw_execute即可
      for (index = 0; index < window; index++) {
          din[index] = (double)int16_t_buf[index];
      }
    
     //执行rfft的具体流程
     fftw_execute(p);
     //fftw3库的rfft的输出虚部数据是正序的
     //fftw2库的rfft的输出虚部数据是逆序的(也就是反的)
     
     //打印实部
      printf ("\nreal\n");
      for (index = 0; index < window/2+1; index++) {
          if (index % 8 == 0) printf("\n");
          printf ("%5.5lf ", dout[index][0]);
      }
      //打印虚部
      printf ("\nimag\n");
      for (index = 1; index < window/2; index++) {
          if (index % 8 == 0) printf("\n");
          printf ("%5.5lf ", dout[index][1]);
      }
      printf ("\n");
     
      //对rfft输出数据进行irfft逆变
      fftw_execute(b);
  
      for (index = 0; index < window; index++) {
          if (index % 8 == 0) printf("\n");
          printf ("%.10lf ", din[index]/window);
      }
      printf ("\n");

	//.......
  }
输出结果如下:
real

900.00000 -75.52811 -89.26960 -141.76212 144.53911 -18.87933 -37.07400 -63.94721 
14.00000 -31.86081 -31.16864 -10.16093 -2.53911 -87.62402 29.51224 -2.23746 
-92.00000 
imag
135.48370 -36.89088 52.35642 -47.87006 144.12557 56.05170 121.10312 
-46.00000 67.67387 12.15221 -15.92192 -5.87006 45.36275 -12.79038 59.10828 

0.0000000000 1.0000000000 10.0000000000 20.0000000000 31.0000000000 21.0000000000 29.0000000000 30.0000000000 
32.0000000000 41.0000000000 12.0000000000 13.0000000000 15.0000000000 15.0000000000 14.0000000000 12.0000000000 
54.0000000000 65.0000000000 43.0000000000 32.0000000000 12.0000000000 43.0000000000 12.0000000000 43.0000000000 
54.0000000000 64.0000000000 43.0000000000 31.0000000000 11.0000000000 21.0000000000 32.0000000000 44.0000000000 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值