赞
踩
Table of Contents
- #include <complex.h>
- #include <fftw3.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <math.h>
-
- #ifndef M_PI
- #define M_PI 3.141592653589793
- #endif
-
- int main(int argc, char* argv[]) {
- fftw_complex *in, *out;
- fftw_plan p;
- int n = 128;
- int f = 440;
- int r = 16000;
- int m = 1;
- double a = 1.;
-
- /* Read input */
- int c;
- while (1) {
- c = getopt(argc, argv, "n:f:a:r:m:h");
- if (c==-1) break;
-
- switch (c) {
- case 'n':
- n = atoi(optarg);
- if (n<2)
- return 1;
- break;
- case 'f':
- f = atoi(optarg);
- if (f<0)
- return 1;
- break;
- case 'r':
- r = atoi(optarg);
- if (r<0)
- return 1;
- break;
- case 'm':
- m = atoi(optarg);
- if (m<0)
- return 1;
- break;
- case 'a':
- a = atof(optarg);
- if (a<1.0)
- return 1;
- break;
- case 'h':
- printf("usage: %s [options]\n"
- " -n int Integer which specifies the array length.\n"
- " -f int The frequency of the intput data.\n"
- " -r int The sample rate.\n"
- " -a float The amplitude.\n"
- , argv[0]);
- return 0;
- }
- }
-
- in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
-
- /* Build input */
- double dt=1./r;
- double omega=2*f*M_PI;
- printf("Input:\n");
- int i;
- for (i=0; i<n; i++) {
- double t=i*dt;
- double y = a*sin(omega*t);
- in[i] = y;
- /* printf("%f %f %f\n", t, creal(in[i]), cimag(in[i]));*/
- }
-
- out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
- p = fftw_plan_dft_1d(n, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
-
- for (int j=0; j<m; j++) {
- fftw_execute(p); /* repeat as needed */
-
- /* Print output */
- printf("Output Run %i:\n", j);
- for (i=0; i<n; i++) {
- int f=i*r/n;
- double a=creal(out[i])/n;
- double b=cimag(out[i])/n;
- double r=sqrt(a*a+b*b);
- printf("%d %f\n", f, r);
- }
- }
-
- fftw_destroy_plan(p);
- fftw_free(in); fftw_free(out);
-
- return 0;
- }

- cmake_minimum_required (VERSION 3.0)
- project (main)
- add_executable(main main.c)
-
- target_link_libraries(main fftw3)
- target_link_libraries(main m)
- set_property(TARGET main PROPERTY C_STANDARD 99)
- $ cmake .
- -- Configuring done
- -- Generating done
- -- Build files have been written to: /home/Toa/fftw3/demo1
-
- $ make
- [ 50%] Building C object CMakeFiles/main.dir/main.c.o
- [100%] Linking C executable main.exe
- [100%] Built target main
- $ ./main.exe -h
- usage: ./main [options]
- -n int Integer which specifies the array length.
- -f int The frequency of the intput data.
- -r int The sample rate.
- -a float The amplitude.
-
- $ ./main.exe -n 10 -f 100 -a 10
- Input:
- Output Run 0:
- 0 1.746801
- 1600 0.623929
- 3200 0.327044
- 4800 0.237484
- 6400 0.201983
- 8000 0.192089
- 9600 0.201983
- 11200 0.237484
- 12800 0.327044
- 14400 0.623929

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。