zoukankan      html  css  js  c++  java
  • 使用FFTW3做二维DFT的示例代码

    刚才重新用fftw3写了个helloword,经测试结果正确。代码如下:

    fftw_test.c

    #include <fftw3.h>
    #include 
    <stdio.h>

    #define N 3
    #define ELEM(r,c) (r*N+c)

    int showresult(fftw_complex* in, fftw_complex* out)
    {
        
    int i, j;
        printf(
    "In:\n");
        
    for (i=0; i<N; i++{
            
    for (j=0; j<N; j++{
                printf(
    "%lf\t"in[ELEM(i, j)][0]);
            }

            printf(
    "\n");
        }

        printf(
    "Out:\n");
        
    for (i=0; i<N; i++{
            
    for (j=0; j<N; j++{
                printf(
    "%lf\t"out[ELEM(i, j)][0]);
            }

            printf(
    "\n");
        }

        
    return 1;
    }


    int main()
    {
        fftw_complex 
    *in*out;
        fftw_plan p;
        
    int i, j;

        
    // 分配存储空间
        in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N * N);
        
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N * N);


        
    // 设置变换计划
        p = fftw_plan_dft_2d(N, N, inout, FFTW_FORWARD, FFTW_ESTIMATE);


        
    // 设置测试数据
        for (i=0; i<N; i++{
            
    for (j=0; j<N; j++{
                
    in[ELEM(i, j)][0= 1;
                
    in[ELEM(i, j)][1= 0;
            }

        }

        
    in[ELEM(12)][0= 5;
        
    in[ELEM(22)][0= 3;

        
    // 执行变换
        fftw_execute(p); /* repeat as needed */

        
    // 显示测试结果
        showresult(inout);

        
    // 释放内存
        fftw_destroy_plan(p);
        fftw_free(
    in);
        fftw_free(
    out);

        
    return 1;
    }

    makefile文件:

    all: fft_test

    fft_test: fft_test.c
        gcc -o fft_test fft_test.c -lfftw3

    clean:
        rm -f fft_test

    输出结果:

    In:
    1.000000    1.000000    1.000000    
    1.000000    1.000000    5.000000    
    1.000000    1.000000    3.000000    
    Out:
    15.000000    -3.000000    -3.000000    
    -
    3.000000    3.000000    0.000000    
    -
    3.000000    0.000000    3.000000    

  • 相关阅读:
    迷 宫
    车厢调度
    快速幂
    2804 最大最小数质因数
    3022 西天收费站
    2291 糖果堆
    1464 装箱问题 2
    Exists/In/Any/All/Contains操作符
    window.onscroll
    zIndex 属性设置元素的堆叠顺序。
  • 原文地址:https://www.cnblogs.com/nicebear/p/1180289.html
Copyright © 2011-2022 走看看