C FFT 快速傅里叶库- fftw-3.3.8

在编写C++ 数据采集软件时,会使用到快速傅里叶变换算法,例如振动分析。网络上介绍的比较多的是fftw3。

FFTW—Fastest Fourier Transform in the West,是由 MIT 的 Matteo Frigo 博士和 Steven G. Johnson 博士开发的一个完全免费的软件包。FFTW 最初的 release 版本于 1997 年发布,最新的 release 版本 fftw-3.3.8。正如它的名字声称的,号称是最快的FFT软件包。

      fftw支持任意大小的、任意维数的数据的离散傅里叶变换(DFT),并且还支持离散余弦变换(DCT)、离散正弦变换(DST)和离散哈特莱变换(DHT)

安装

从官网(http://fftw.org/index.html)下载源码压缩包  fftw-3.3.8.tar.gz,然后解压到linuxwork 目录中。按照http://www.fftw.org/fftw3_doc/Installation-on-Unix.html的指导安装:

cd fftw-3.3.8
/configure CC=clang
make
sudo make install

./configure的格式为 ./configure CC="<the name of your C compiler>",我使用的是clang编译器。

测试程序

#include<fftw3.h> 
int main(void) 
{ 
int N; 
N=1024;
fftw_complex *in, *out;
fftw_plan my_plan;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N);
my_plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(my_plan);
fftw_destroy_plan(my_plan);
fftw_free(in); 
fftw_free(out);
return 0;
}

 

编译

clang test.cpp -o test -lfftw3 –lm

-lm 是指使用math 库。

例2

下面的程序引用了 https://blog.csdn.net/cyh706510441/article/details/46676123 中的代码。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 #include "fftw3.h"
 
#define PI 3.1415926
 
int main()
{
	int len = 128;
	double *in = NULL;
	// 如果要使用float版本,需先引用float版本的lib库,然后在fftw后面加上f后缀即可.
	fftw_complex *out = NULL;// fftwf_complex --> 即为float版本
	fftw_plan p;
	in  = (double *)fftw_malloc(sizeof(double) * len);
	out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * len);
	double dx = 1.0 / len;
 
	// 输入纯实数
	for (int i = 0; i < len; i++)
	{
		in[i] = sin(2*PI * dx*i) + sin(4*PI * dx*i);
 
		printf("%.2f ", in[i]);
	}
	printf("\n\n");
 
	// 傅里叶变换
	p = fftw_plan_dft_r2c_1d(len, in, out, FFTW_ESTIMATE);
	fftw_execute(p);
 
	// 输出幅度谱
	for (int i = 0; i < len; i++)
	{
		float len = sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]);
		printf("%.2f ", len);
	}
	printf("\n");
 
	// 释放资源
	fftw_destroy_plan(p);
	fftw_free(in);
	fftw_free(out);
	return 0;
}

编译

clang test2.c -o test2 -lfftw3 –lm

结束语

好了,搞定!

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值