WhyCan Forum

本站无需注册,无需积分,无需回复可下载所有资料,如果真的喜欢小站,请您注册之后请至少回复一个帖子激活Id,谢谢支持! 站长QQ: 516333132 (挖坑网/填坑网) admin@whycan.cn

您尚未登录。

#1 2018-08-20 11:17:43

xxzouzhichao
会员
注册时间: 2017-10-25
累计积分: 101

代码超级精简的fft源码,无需移植,代码才80行

先上源代码!

#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) & 0xc3c3c3c3;
    right = (index >> 2) & 0x3c3c3c3c;
    index = left | right;
    left = (index << 1) & 0xa5a5a5a5;
    right = (index >> 1) & 0x5a5a5a5a;
    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);
    }
}

再上代码的使用方法:

#define K 4
#define N (1 << K)
static complex w[N / 2];
static complex dat[N];

int main() {
    uint32_t i;
    for (i = 0; i < N; i++) {
        dat[i].r = 0.0f;
        dat[i].i = 0.0f;
    }
    dat[0].r = 1.0f;
    dat[1].r = 1.0f;
    fft_init((complex*)w, N / 2);
    fft((complex*)dat, (const complex*)w, K);
    for (i = 0; i < N; i++) {
        printf((const char*)"dat[%d] = %f + %f * i\n", i, dat[i].r, dat[i].i);
    }
    return 0;
}

输出结果:
TIM20180820111332_20180819-2323.jpg

对比matlab输出结果:
none.jpg

最近编辑记录 xxzouzhichao (2018-08-20 11:23:56)

离线

#2 2018-08-20 11:26:21

演技担当黄晓明
会员
注册时间: 2017-10-17
累计积分: 30

Re: 代码超级精简的fft源码,无需移植,代码才80行

运算速度如何?  和ST的库比较一下哪个快点

最近编辑记录 演技担当黄晓明 (2018-08-20 11:30:37)

离线

#3 2018-08-20 11:30:06

BugActiveDaughter
Moderator
注册时间: 2017-10-17
累计积分: 58

Re: 代码超级精简的fft源码,无需移植,代码才80行

厉害

离线

#4 2018-08-20 11:40:24

xxzouzhichao
会员
注册时间: 2017-10-25
累计积分: 101

Re: 代码超级精简的fft源码,无需移植,代码才80行

演技担当黄晓明 说:

运算速度如何?  和ST的库比较一下哪个快点

stm32f030 16点fft 400us
stm32f030 64点fft 3900us
stm32f030 128点fft 9200us

开优化后128点fft 8800us

最近编辑记录 xxzouzhichao (2018-08-20 11:43:15)

离线

#5 2018-08-20 11:43:21

晕哥
Administrator
注册时间: 2017-09-06
累计积分: 6,471

Re: 代码超级精简的fft源码,无需移植,代码才80行

大神这是软硬通吃,我等望尘莫及.

离线

#6 2018-08-20 11:49:59

xxzouzhichao
会员
注册时间: 2017-10-25
累计积分: 101

Re: 代码超级精简的fft源码,无需移植,代码才80行

演技担当黄晓明 说:

运算速度如何?  和ST的库比较一下哪个快点

mdk的库128点fft约6300us

离线

#7 2018-08-20 11:51:57

达克罗德
会员
注册时间: 2018-04-10
累计积分: 501

Re: 代码超级精简的fft源码,无需移植,代码才80行

st的库能不需移植直接给别的CPU跑吗?

离线

#8 2018-08-20 11:54:34

xxzouzhichao
会员
注册时间: 2017-10-25
累计积分: 101

Re: 代码超级精简的fft源码,无需移植,代码才80行

晕哥 说:

大神这是软硬通吃,我等望尘莫及.

2013年毕业前夕照着数字信号处理的书写的,在amobbs上开源了,如今朝花夕拾,优化了一下代码风格,在挖坑网开源

离线

#9 2018-08-20 11:56:19

xxzouzhichao
会员
注册时间: 2017-10-25
累计积分: 101

Re: 代码超级精简的fft源码,无需移植,代码才80行

达克罗德 说:

st的库能不需移植直接给别的CPU跑吗?

我没用过stm的库,我用的mdk自带的dsp库对比的,128点比mdk的库慢了2500us

离线

#10 2018-08-20 23:05:04

sblpp
会员
注册时间: 2018-02-14
累计积分: 45

Re: 代码超级精简的fft源码,无需移植,代码才80行

感谢楼主分享!牛X的很!

离线

#11 2018-08-21 10:46:39

dgtg
会员
注册时间: 2017-11-08
累计积分: 82

Re: 代码超级精简的fft源码,无需移植,代码才80行

大神! 我等望尘莫及!

离线

#12 2019-01-11 20:58:57

xxzouzhichao
会员
注册时间: 2017-10-25
累计积分: 101

Re: 代码超级精简的fft源码,无需移植,代码才80行

更正一段代码:

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) & 0xc3c3c3c3;
    right = (index >> 2) & 0x3c3c3c3c;
    index = left | right;
    left = (index << 1) & 0xa5a5a5a5;
    right = (index >> 1) & 0x5a5a5a5a;
    index = left | right;
    return index >> (32 - bits);
}

更正后:

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) & 0xc3c3c3c3;
    right = (index >> 2) & 0x3c3c3c3c;
    index = left | right;
    left = (index << 1) & 0xaaaaaaaa;
    right = (index >> 1) & 0x55555555;
    index = left | right;
    return index >> (32 - bits);
}

离线

#13 2019-01-11 22:07:58

xxzouzhichao
会员
注册时间: 2017-10-25
累计积分: 101

Re: 代码超级精简的fft源码,无需移植,代码才80行

xxzouzhichao 说:

更正一段代码:

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) & 0xc3c3c3c3;
    right = (index >> 2) & 0x3c3c3c3c;
    index = left | right;
    left = (index << 1) & 0xa5a5a5a5;
    right = (index >> 1) & 0x5a5a5a5a;
    index = left | right;
    return index >> (32 - bits);
}

更正后:

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);
}

离线

#14 2019-03-10 10:56:19

tink
会员
注册时间: 2019-03-09
累计积分: 32

Re: 代码超级精简的fft源码,无需移植,代码才80行

mark

离线

页脚