博士
最后登录1970-1-1
在线时间 小时
注册时间2020-5-14
|
- #include <stdio.h>
- #include <stdint.h>
- #include <math.h>
- typedef struct {
- float r;
- float i;
- }complex;
- static void butter_fly(complex* a, complex* b, const complex* c) {
- complex bc;
- bc.r = b->r * c->r - b->i * c->i;
- bc.i = b->r * c->i + b->i * c->r;
- b->r = a->r - bc.r;
- b->i = a->i - bc.i;
- a->r += bc.r;
- a->i += bc.i;
- }
- static uint32_t bits_reverse(uint32_t index, uint32_t bits) {
- uint32_t left, right;
- left = index << 16;
- right = index >> 16;
- index = left | right;
- left = (index << 8) & 0xff00ff00;
- right = (index >> 8) & 0x00ff00ff;
- index = left | right;
- left = (index << 4) & 0xf0f0f0f0;
- right = (index >> 4) & 0x0f0f0f0f;
- index = left | right;
- left = (index << 2) & 0xcccccccc;
- right = (index >> 2) & 0x33333333;
- index = left | right;
- left = (index << 1) & 0xaaaaaaaa;
- right = (index >> 1) & 0x55555555;
- index = left | right;
- return index >> (32 - bits);
- }
- static void fft_k(complex* dat, const complex* w, uint32_t k, uint32_t k_all) {
- uint32_t i, j;
- complex* dat1;
- k_all = 1 << (k_all - k - 1);
- k = 1 << k;
- dat1 = dat + k;
- for (i = 0; i < k_all; i++) {
- for (j = 0; j < k; j++) {
- butter_fly(dat++, dat1++, w + j * k_all);
- }
- dat += k;
- dat1 += k;
- }
- }
- void fft_init(complex* w, uint32_t k) {
- float beta = 0.0f, dbeta = 3.1415926535f / k ;
- for ( ; k; k--) {
- w->r = cosf(beta);
- w->i = sinf(beta);
- beta += dbeta;
- w++;
- }
- }
- void fft(complex* dat, const complex* w, uint32_t k) {
- uint32_t i, j, n;
- complex temp;
- n = 1 << k;
- for (i = 0; i < n; i++) {
- j = bits_reverse(i, k);
- if (i <= j) {
- continue;
- }
- temp = dat[i];
- dat[i] = dat[j];
- dat[j] = temp;
- }
- for (i = 0; i < k; i++) {
- fft_k(dat, w, i, k);
- }
- }
复制代码
|
评分
-
查看全部评分
|