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-12-26怎么使用nsenter命令进入容器?-icode9专业技术文章分享
- 2024-12-26导入文件提示存在乱码,请确定使用的是UTF-8编码怎么解决?-icode9专业技术文章分享
- 2024-12-26csv文件怎么设置编码?-icode9专业技术文章分享
- 2024-12-25TypeScript基础知识详解
- 2024-12-25安卓NDK 是什么?-icode9专业技术文章分享
- 2024-12-25caddy 可以定义日志到 文件吗?-icode9专业技术文章分享
- 2024-12-25wordfence如何设置密码规则?-icode9专业技术文章分享
- 2024-12-25有哪些方法可以实现 DLL 文件路径的管理?-icode9专业技术文章分享
- 2024-12-25错误信息 "At least one element in the source array could not be cast down to the destination array-icode9专业技术文章分享
- 2024-12-25'flutter' 不是内部或外部命令,也不是可运行的程序 或批处理文件。错误信息提示什么意思?-icode9专业技术文章分享