簡単なfftw 3例:正弦波信号の離散フーリエ変換

3627 ワード

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 int Integer which specifies the array length.
" " -f int The frequency of the intput data.
" " -r int The sample rate.
" " -a float The amplitude.
" , 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:
"); 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