Table of Contents

1.源代码

2.编译运行

CMakeLists.txt

编译

运行

致谢


1.源代码

#include <complex.h>
#include <fftw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <math.h>#ifndef M_PI
#define M_PI 3.141592653589793
#endifint 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<n; i++) {double t=i*dt;double y = a*sin(omega*t);in[i] = y;
/*    printf("%f %f %f\n", t, creal(in[i]), cimag(in[i]));*/}out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);p = fftw_plan_dft_1d(n, in, out, FFTW_FORWARD, FFTW_ESTIMATE);for (int j=0; j<m; j++) {fftw_execute(p); /* repeat as needed *//* Print output */printf("Output Run %i:\n", j);for (i=0; i<n; i++) {int f=i*r/n;double a=creal(out[i])/n;double b=cimag(out[i])/n;double r=sqrt(a*a+b*b);printf("%d %f\n", f, r);}}fftw_destroy_plan(p);fftw_free(in); fftw_free(out);return 0;
}

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

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

  1. mvc登录实例 mysql_spring mvc + mybatis + mysql 调整的一个简单的登录例子

    spring mvc + mybatis + mysql 整合的一个简单的登录例子 今天用spring跟mybatis整合写了一个简单的登录例子,第一次整合,给自己做个笔记,可能注释写的有点少,做的不 ...

  2. boost::log模块实现一个简单日志的例子

    boost::log模块实现一个简单日志的例子 实现功能 C++实现代码 实现功能 boost::log模块实现一个简单日志的例子 C++实现代码 #include <boost/log/tri ...

  3. 轻松创建nodejs服务器(1):一个简单nodejs服务器例子

    这篇文章主要介绍了一个简单nodejs服务器例子,本文实现了一个简单的hello world例子,并展示如何运行这个服务器,需要的朋友可以参考下 我们先来实现一个简单的例子,hello world. ...

  4. 一个简单的CORBA例子

    因为对CORBA分析的需要,这里写一个简单的CORBA例子.从JDK1.2开始,JDK中集成了ORB的实现,本例子使用了JDK1.7,对于JDK1.2+应该都没有问题.这个例子实现一个简单的加减乘除的 ...

  5. 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...

  6. java servlet例子_Servlet学习教程(三)---- 一个简单的Servlet例子

    我们用个最简单的Servlet例子来解说一下Servlet简单配置以及Servlet类实现类的写法. 第一,我们新建一个Dynamic Web Project,起名Servlet 点击NEXT,设置D ...

  7. Python网络爬虫 - 一个简单的爬虫例子

    下面我们创建一个真正的爬虫例子 爬取我的博客园个人主页首页的推荐文章列表和地址 scrape_home_articles.py from urllib.request import urlopen f ...

  8. python网页爬虫例子_Python网络爬虫 - 一个简单的爬虫例子

    下面我们创建一个真正的爬虫例子 爬取我的博客园个人主页首页的推荐文章列表和地址 scrape_home_articles.py from urllib.request importurlopenfro ...

  9. 决策树分析例题经典案例_决策树原理及一个简单的小例子

    首先通过两个图来引入什么是决策树. 是否学习的决策过程 决策树是仿树结构来进行决策的,例如上图来说,我们要对'是否学习'这个问题进行决策时,通常伴随一系列的子决策.先看是否有'对象',有的话是否需要' ...

最新文章

  1. 利用graphviz模块展示斐波那契数列的递归函数调用图(Python)
  2. python 代码-20个Python代码段,你需要立刻学会,好用到哭!
  3. 苹果App Icon的问题
  4. 【转】C 从函数返回数组
  5. 未来教育计算机二级01为法律一班,2019年3月计算机二级MSOffice提分试题及答案012...
  6. 大数据数据量估算_如何估算数据科学项目的数据收集成本
  7. 高度为5的3阶b树含有的关键字个数_数据结构要考得好,你心里要有B树
  8. 之前画得太丑了,再来张好看的.我试着改小点.但是就看不清了
  9. RSA加密算法【手把手解释】
  10. bzoj1854 [Scoi2010]游戏
  11. 【老孙随笔】求职,不要无的放矢
  12. php单例模式的实现方式,PHP之单例模式的实现
  13. 【源码分享】python开发的高通平台fastboot刷机工具
  14. vue-socket.io 对IE浏览器(IE10+)的兼容性修复
  15. SPSS统计分析行业应用实战--SPSS 24.0新增
  16. 计算机开机时10声响,电脑开机报警的声音有什么含义?详细教您认识报警声音
  17. 记一次跳过系统版本检测强刷opengapps卡刷包的过程,20210110修
  18. 23Exchange Server 2010跨站点部署-公网邮箱系统配置及公网记录
  19. xz压缩文件的解压缩过程
  20. java SpringBoot 对接支付宝 APP支付 证书模式及非证书模式

热门文章

  1. Spring框架----基于接口的动态代理
  2. javascript打印1-100内的质数
  3. 纯css实现DIV以及图片水平垂直居中兼容多种浏览器(实现过程)
  4. ListView setOnItemClickListener无法响应点击事件解决
  5. double 乘法_老大说:谁要再用double定义商品金额,就自己收拾东西走 | 文末福利...
  6. mysql error report,ECSHOP网店系统提示MYSQL SERVER ERROR REPORT的解决方法
  7. c语言 socket 断开自动连接,如何优雅地断开TCP连接?
  8. linux 使用ssr客户端_webpack5.0+SSR尝鲜【排坑记录】
  9. ros创建工作空间_ROS 初级 - 安装和配置 ROS 环境
  10. 有个内含单词的超大文本文件_如果你家有个大阳台,你会选择封阳台吗?