当前位置:   article > 正文

一个简单的fftw3例子:正弦信号的离散傅里叶变换_p=fftw_plan_dft_1d

p=fftw_plan_dft_1d

Table of Contents

1.源代码

2.编译运行

CMakeLists.txt

编译

运行

致谢


1.源代码

  1. #include <complex.h>
  2. #include <fftw3.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <getopt.h>
  7. #include <math.h>
  8. #ifndef M_PI
  9. #define M_PI 3.141592653589793
  10. #endif
  11. int main(int argc, char* argv[]) {
  12. fftw_complex *in, *out;
  13. fftw_plan p;
  14. int n = 128;
  15. int f = 440;
  16. int r = 16000;
  17. int m = 1;
  18. double a = 1.;
  19. /* Read input */
  20. int c;
  21. while (1) {
  22. c = getopt(argc, argv, "n:f:a:r:m:h");
  23. if (c==-1) break;
  24. switch (c) {
  25. case 'n':
  26. n = atoi(optarg);
  27. if (n<2)
  28. return 1;
  29. break;
  30. case 'f':
  31. f = atoi(optarg);
  32. if (f<0)
  33. return 1;
  34. break;
  35. case 'r':
  36. r = atoi(optarg);
  37. if (r<0)
  38. return 1;
  39. break;
  40. case 'm':
  41. m = atoi(optarg);
  42. if (m<0)
  43. return 1;
  44. break;
  45. case 'a':
  46. a = atof(optarg);
  47. if (a<1.0)
  48. return 1;
  49. break;
  50. case 'h':
  51. printf("usage: %s [options]\n"
  52. " -n int Integer which specifies the array length.\n"
  53. " -f int The frequency of the intput data.\n"
  54. " -r int The sample rate.\n"
  55. " -a float The amplitude.\n"
  56. , argv[0]);
  57. return 0;
  58. }
  59. }
  60. in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
  61. /* Build input */
  62. double dt=1./r;
  63. double omega=2*f*M_PI;
  64. printf("Input:\n");
  65. int i;
  66. for (i=0; i<n; i++) {
  67. double t=i*dt;
  68. double y = a*sin(omega*t);
  69. in[i] = y;
  70. /* printf("%f %f %f\n", t, creal(in[i]), cimag(in[i]));*/
  71. }
  72. out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
  73. p = fftw_plan_dft_1d(n, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
  74. for (int j=0; j<m; j++) {
  75. fftw_execute(p); /* repeat as needed */
  76. /* Print output */
  77. printf("Output Run %i:\n", j);
  78. for (i=0; i<n; i++) {
  79. int f=i*r/n;
  80. double a=creal(out[i])/n;
  81. double b=cimag(out[i])/n;
  82. double r=sqrt(a*a+b*b);
  83. printf("%d %f\n", f, r);
  84. }
  85. }
  86. fftw_destroy_plan(p);
  87. fftw_free(in); fftw_free(out);
  88. return 0;
  89. }

2.编译运行

CMakeLists.txt

  1. cmake_minimum_required (VERSION 3.0)
  2. project (main)
  3. add_executable(main main.c)
  4. target_link_libraries(main fftw3)
  5. target_link_libraries(main m)
  6. set_property(TARGET main PROPERTY C_STANDARD 99)

编译

  1. $ cmake .
  2. -- Configuring done
  3. -- Generating done
  4. -- Build files have been written to: /home/Toa/fftw3/demo1
  5. $ make
  6. [ 50%] Building C object CMakeFiles/main.dir/main.c.o
  7. [100%] Linking C executable main.exe
  8. [100%] Built target main

运行

  1. $ ./main.exe -h
  2. usage: ./main [options]
  3. -n int Integer which specifies the array length.
  4. -f int The frequency of the intput data.
  5. -r int The sample rate.
  6. -a float The amplitude.
  7. $ ./main.exe -n 10 -f 100 -a 10
  8. Input:
  9. Output Run 0:
  10. 0 1.746801
  11. 1600 0.623929
  12. 3200 0.327044
  13. 4800 0.237484
  14. 6400 0.201983
  15. 8000 0.192089
  16. 9600 0.201983
  17. 11200 0.237484
  18. 12800 0.327044
  19. 14400 0.623929

致谢

cspiel1

https://github.com/cspiel1/fftw_demos

相关:https://github.com/JabariBooker/FFTW-Demo

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号