fftw 的安装和使用


安装:ubuntu使用新立得安装 fftw3 fftw3-dev fftw3-doc, 非常顺利。

下面的是如何使用的一个例子
/*****************************
* filename: test_fftw.cpp
* author : Tiao Lu
* Company : School of Mathematical Sciences, Peking University
* 编译命令: g++ -o test_fftw.exe test_fftw.cpp -lfftw3
* Date    : September 30th, 2007
* Description: This code is an example to show the use of the free code FFTW, which implements the Fast Fourier Trasformation algorithm.
*/

#include <complex>
#include <fftw3.h>
#include <math.h>
#include <iostream>

#define N 10
using namespace std;
int main(int argc, char * argv[]){

    fftw_complex in[N], out[N];
    fftw_plan p;

//一维dft,in 输入,out输出,FFTW_FORWARD 表示 exp 上指数是负号
// out = F in
//where out and in are two vectors of the same length n, and F is a n-by-n matrix with the (j,k) element
// F jk = exp(-i 2 pi j k /n). i 是虚数单位. 

    p=fftw_plan_dft_1d(N,in,out,FFTW_FORWARD,FFTW_MEASURE);
    for(int i=0;i <N;i ++) {
        in[i][0]=i;
        in[i][1]=0.0;
    }
   

    fftw_execute(p);

    for(int i=0;i <N;i ++){
        cout<<out[i][0]<<" "<<out[i][1]<<endl;
    }

    //验证是否 out[3] = \sum_{k=0}^{N-1}exp(-i 2pi 3 k/N)in[k]
    complex<double> temp = 0.0;
    for(int k =0; k < N; k ++){
        double pi = 4*atan(1.0);
        temp += exp(complex<double>(0.0,-2.0*pi*3*k/N))*complex<double>(in[k][0],in[k][1]);
    }
    cout<<"out[3] is "<<temp<<endl;

    fftw_complex out1[N];

    fftw_plan p1;
//一维dft,in 输入,out输出,FFTW_BACKWARD 表示 exp 上指数是正号
// out = IF in
//where out and in are two vectors of the same length n, and IF is a n-by-n matrix with the (j,k) element
// IF jk = exp( +i 2 pi jk/n). i 是虚数单位.

    p1=fftw_plan_dft_1d(N,out1,in,FFTW_BACKWARD,FFTW_MEASURE);

    for(int i=0;i <N;i ++){
        out1[i][0]=out[i][0];
        out1[i][1]=out[i][1];
    }

    fftw_execute(p1);
//注意这里得到的 in 并不是和原来的in 的数值不同,比较之后发现,现在的in
// 是原来的 in 的 N 倍。原因是这里的定义的逆傅立叶变换没有除以 N.
//这和课本中定义的逆傅立叶变换不同。
    for(int i=0;i <N;i ++){
        cout<<in[i][0]<<" "<<in[i][1]<<endl;
    }



    fftw_destroy_plan(p);
    fftw_destroy_plan(p1);
    return 1;
}
### 安装步骤 #### 1. 下载FFTW库 首先,从FFTW官方网站下载适用于您的操作系统的FFTW库。通常,这涉及到下载源代码包,例如`fftw-3.3.8.tar.gz`,然后解压它[^3]。 ```bash wget http://www.fftw.org/fftw-3.3.8.tar.gz tar -xvzf fftw-3.3.8.tar.gz cd fftw-3.3.8 ``` #### 2. 编译安装 接下来,创建一个构建目录并使用CMake来配置编译过程。这通常包括创建一个`build`目录,并在其中运行CMake以生成Makefile或其他构建系统所需的文件。之后,使用make命令编译源代码,并安装到系统指定的位置。 ```bash mkdir build cd build cmake .. make sudo make install ``` #### 3. 配置环境 安装完成后,可能需要配置环境变量以便于使用FFTW库。这通常涉及设置`LD_LIBRARY_PATH`环境变量指向FFTW库的位置,以及确保头文件路径正确设置[^4]。 ### 使用方法 #### 1. 包含头文件 在使用FFTW之前,需要包含相应的头文件。 ```c #include <fftw3.h> ``` #### 2. 创建计划 创建一个FFTW计划,该计划描述了执行变换的数据大小其他参数。 ```c int N = 10000; // 数据大小 fftw_complex *in, *out; fftw_plan p; in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE); ``` #### 3. 执行变换 一旦创建了计划,就可以使用它来执行变换。 ```c fftw_execute(p); // 执行变换 ``` #### 4. 清理资源 完成变换后,释放分配的内存并销毁计划。 ```c fftw_destroy_plan(p); fftw_free(in); fftw_free(out); ``` ### 示例代码 以下是一个简单的示例,展示了如何使用FFTW执行一维复数到复数的离散傅里叶变换(DFT)。 ```c #include <fftw3.h> #include <stdio.h> int main() { int N = 10000; fftw_complex *in, *out; fftw_plan p; in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); // 初始化输入数据 for (int i = 0; i < N; ++i) { in[i][0] = i; // 实部 in[i][1] = 0; // 虚部 } p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE); fftw_execute(p); // 执行变换 // 输出结果 for (int i = 0; i < N; ++i) { printf("out[%d] = %f + %fi\n", i, out[i][0], out[i][1]); } fftw_destroy_plan(p); fftw_free(in); fftw_free(out); return 0; } ``` ### 注意事项 - 在不同的平台上,安装过程可能会有所不同,特别是对于特定的硬件环境,如飞腾平台,需要遵循特定的指南[^2]。 - 使用CMake时,可以通过`find_package`函数查找FFTW3库,并在`CMakeLists.txt`文件中添加相应的头文件路径库链接[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值