fft的c++实现
2021/8/6 9:35:58
本文主要是介绍fft的c++实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
代码实现
#include"fft.h" | |
extern complex x[N * 2], *W; | |
void add(complex a, complex b, complex *c) // 复数加运算 | |
{ | |
c->real = a.real + b.real; | |
c->img = a.img + b.img; | |
} | |
void sub(complex a, complex b, complex *c) // 复数减运算 | |
{ | |
c->real = a.real - b.real; | |
c->img = a.img - b.img; | |
} | |
void mul(complex a, complex b, complex *c) // 复数乘运算 | |
{ | |
c->real = a.real*b.real - a.img*b.img; | |
c->img = a.real*b.img + a.img*b.real; | |
} | |
void divi(complex a, complex b, complex *c) // 复数除运算 | |
{ | |
c->real = (a.real*b.real + a.img*b.img) / (b.real*b.real + b.img*b.img); | |
c->img = (a.img*b.real - a.real*b.img) / (b.real*b.real + b.img*b.img); | |
} | |
/********************** | |
@ 欧拉公式运算 | |
***********************/ | |
void initW(int size) | |
{ | |
int i; | |
W = (complex*)malloc(sizeof(complex)* size); //分配内存空间 | |
for (i = 0; i<size; i++) | |
{ | |
W[i].real = cos(2 * PI / size*i); | |
W[i].img = -1 * sin(2 * PI / size*i); | |
} | |
} | |
/********************** | |
@ 变址运算 | |
***********************/ | |
void changex(int size) | |
{ | |
complex temp; | |
unsigned int i = 0, j = 0, k = 0; | |
double t; | |
for (i = 0; i<size; i++) | |
{ | |
k = i; j = 0; | |
t = (log(size) / log(2)); | |
while ((t--)>0) | |
{ | |
j = j << 1; | |
j |= (k & 1); | |
k = k >> 1; | |
} | |
if (j>i) | |
{ | |
temp = x[i]; | |
x[i] = x[j]; | |
x[j] = temp; | |
} | |
} | |
} | |
/********************** | |
@ 快速傅里叶函数 | |
***********************/ | |
void fftx() | |
{ | |
long int i = 0, j = 0, k = 0, l = 0; | |
complex up, down, product; | |
changex(N); | |
for (i = 0; i<log(N) / log(2); i++) /*一级蝶形运算*/ | |
{ | |
l = 1 << i; | |
for (j = 0; j<N; j += 2 * l) /*一组蝶形运算*/ | |
{ | |
for (k = 0; k<l; k++) /*一个蝶形运算*/ | |
{ | |
mul(x[j + k + l], W[N*k / 2 / l], &product); | |
add(x[j + k], product, &up); | |
sub(x[j + k], product, &down); | |
x[j + k] = up; | |
x[j + k + l] = down; | |
} | |
} | |
} | |
} | |
/********************** | |
@ 输出x结果 | |
***********************/ | |
void output() | |
{ | |
int i; | |
printf("\nx傅里叶变换结果\n"); | |
for (i = 0; i<N; i++) | |
{ | |
if (i % 4 == 0 && i != 0) printf("\n"); | |
printf(" %.2f", x[i].real); | |
if (x[i].img >= 0.0001) | |
printf("+%.2fj ", x[i].img); | |
else if (fabs(x[i].img)<0.0001) | |
printf("+0.0000j "); | |
else | |
printf("%.2fj ", x[i].img); | |
} | |
printf("\n"); | |
} |
参考网址:https://github.com/DUTFangXiang/FFT
这篇关于fft的c++实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-01UniApp 中组件的生命周期是多少-icode9专业技术文章分享
- 2024-11-01如何使用Svg Sprite Icon简化网页图标管理
- 2024-10-31Excel数据导出课程:新手从入门到精通的实用教程
- 2024-10-31Excel数据导入课程:新手入门指南
- 2024-10-31RBAC的权限课程:新手入门教程
- 2024-10-31Svg Sprite Icon课程:新手入门必备指南
- 2024-10-31怎么配置 L2TP 允许多用户连接-icode9专业技术文章分享
- 2024-10-31怎么在FreeBSD上 安装 OpenResty-icode9专业技术文章分享
- 2024-10-31运行 modprobe l2tp_ppp 时收到“module not found”消息提醒是什么-icode9专业技术文章分享
- 2024-10-31FreeBSD的下载命令有哪些-icode9专业技术文章分享