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

Table of Contents

1.源代码

2.编译运行

CMakeLists.txt

编译

运行

致谢


1.源代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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

2.编译运行

CMakeLists.txt

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

致谢

cspiel1

https://github.com/cspiel1/fftw_demos

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

 

你可能感兴趣的:(C/C++,Assembly)