用C语言写的FFT和IFFT算法

我自己写的1024点CFFT小工具;
此CFFT算法为第一版,在算法模型上可以继续优化运算速度,生成的程序段长度也可以继续精简
; 但是我保证了在我能轻松愉快地编程的能力范围内,这是兼顾了运算精度与运算速度的最佳版本,如果要继续优化代码的话那我感觉就很难受
了; 下面是工具的测试和使用例,以及头文件:


 1. #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include "cfft.h"  
    #define POINTS 1024
    #if POINTS == 4
        #define FFT(a,b,c,d,e) fft_4(a,b,c,d)
        #define IFFT(a,b,c,d,e) ifft_4(a,b,c,d)
    #elif POINTS == 16
        #define FFT(a,b,c,d,e) fft_16(a,b,c,d,e)
        #define IFFT(a,b,c,d,e) ifft_16(a,b,c,d,e)
    #elif POINTS == 64
        #define FFT(a,b,c,d,e) fft_64(a,b,c,d,e)
        #define IFFT(a,b,c,d,e) ifft_64(a,b,c,d,e)
    #elif POINTS == 256
        #define FFT(a,b,c,d,e) fft_256(a,b,c,d,e)
        #define IFFT(a,b,c,d,e) ifft_256(a,b,c,d,e)
    #elif POINTS == 1024
        #define FFT(a,b,c,d,e) fft_1024(a,b,c,d,e)
        #define IFFT(a,b,c,d,e) ifft_1024(a,b,c,d,e)
    #endif   int main() {
        float* re1, * im1, * re2, * im2;
        int i;
     
        re1 = (float*)malloc(sizeof(float) * POINTS);
        im1 = (float*)malloc(sizeof(float) * POINTS);
        re2 = (float*)malloc(sizeof(float) * POINTS);
        im2 = (float*)malloc(sizeof(float) * POINTS);
     
        for (i = 0; i < POINTS; i++)
        {
            re1[i] = (float)i * 10;
            im1[i] = 0;
        }
     
        for (i = 0; i < POINTS; i++)
        {
            printf("%.6f\t%.6f\n", re1[i], im1[i]);
        }
     
        FFT(re1, im1, re2, im2, 1); //if the last parameter is 1,
                                    //then the FFT function will clean up
                                    //the memory that it occupies at the end
        printf("fft:\n");
        for (i = 0; i < POINTS; i++)
        {
            printf("%.6f\t%.6f\n", re2[i], im2[i]);
        }
     
        IFFT(re2, im2, re1, im1, 1);
        printf("ifft(/%d):\n",POINTS);
        for (i = 0; i < POINTS; i++)
        {
            printf("%.6f\t%.6f\n", re1[i]/POINTS, im1[i]/POINTS);
        }
     
        free(re1);
        free(im1);
        free(re2);
        free(im2);
     
        return 0; }

头文件:


 1. #ifndef _CFFT_TOOL_
    #define _CFFT_TOOL_  
    #include <math.h>
    #include <stdlib.h>  
    #define CFFT_PI 3.1415926535897
    #define COEF_NUM_16 (3*3+1)
    #define COEF_NUM_64 (15*3+1)
    #define COEF_NUM_256 (63*3+1)
    #define COEF_NUM_1024 (255*3+1)   //declare void fft_4(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out );   void ifft_4(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out );   void fft_16(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag );   void addr_sort(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int total );   void fft_coef_gen_16(
        float* re_coef,
        float* im_coef );   void fft_layer_transform(
        float* re_out,
        float* im_out,
        float* re_coef,
        float* im_coef,
        int total );   void ifft_16(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag );   void ifft_coef_gen_16(
        float* re_coef,
        float* im_coef );   void ifft_layer_transform(
        float* re_out,
        float* im_out,
        float* re_coef,
        float* im_coef,
        int total );   void fft_64(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag );   void fft_coef_gen_64(
        float* re_coef,
        float* im_coef );   void ifft_coef_gen_64(
        float* re_coef,
        float* im_coef );   void fft_256(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag );   void ifft_256(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag );   void ifft_coef_gen_256(
        float* re_coef,
        float* im_coef );   void fft_coef_gen_256(
        float* re_coef,
        float* im_coef );   void fft_1024(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag );   void ifft_1024(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag );   void ifft_coef_gen_1024(
        float* re_coef,
        float* im_coef );   void fft_coef_gen_1024(
        float* re_coef,
        float* im_coef );   //define void fft_4(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out ) {
        int i;
        for (i = 0; i < 4; i++)
        {
            if (i == 0)
            {
                re_out[0] = re_in[0];
                im_out[0] = im_in[0];
                re_out[1] = re_in[0];
                im_out[1] = im_in[0];
                re_out[2] = re_in[0];
                im_out[2] = im_in[0];
                re_out[3] = re_in[0];
                im_out[3] = im_in[0];
            }
            else if (i == 1)
            {
                re_out[0] += re_in[1];
                im_out[0] += im_in[1];
                re_out[1] += im_in[1];
                im_out[1] -= re_in[1];
                re_out[2] -= re_in[1];
                im_out[2] -= im_in[1];
                re_out[3] -= im_in[1];
                im_out[3] += re_in[1];
            }
            else if (i == 2)
            {
                re_out[0] += re_in[2];
                im_out[0] += im_in[2];
                re_out[1] -= re_in[2];
                im_out[1] -= im_in[2];
                re_out[2] += re_in[2];
                im_out[2] += im_in[2];
                re_out[3] -= re_in[2];
                im_out[3] -= im_in[2];
            }
            else if (i == 3)
            {
                re_out[0] += re_in[3];
                im_out[0] += im_in[3];
                re_out[1] -= im_in[3];
                im_out[1] += re_in[3];
                re_out[2] -= re_in[3];
                im_out[2] -= im_in[3];
                re_out[3] += im_in[3];
                im_out[3] -= re_in[3];
            }
        } }   void ifft_4(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out ) {
        int i;
        for (i = 0; i < 4; i++)
        {
            if (i == 0)
            {
                re_out[0] = re_in[0];
                im_out[0] = im_in[0];
                re_out[1] = re_in[0];
                im_out[1] = im_in[0];
                re_out[2] = re_in[0];
                im_out[2] = im_in[0];
                re_out[3] = re_in[0];
                im_out[3] = im_in[0];
            }
            else if (i == 1)
            {
                re_out[0] += re_in[1];
                im_out[0] += im_in[1];
                re_out[1] -= im_in[1];
                im_out[1] += re_in[1];
                re_out[2] -= re_in[1];
                im_out[2] -= im_in[1];
                re_out[3] += im_in[1];
                im_out[3] -= re_in[1];
            }
            else if (i == 2)
            {
                re_out[0] += re_in[2];
                im_out[0] += im_in[2];
                re_out[1] -= re_in[2];
                im_out[1] -= im_in[2];
                re_out[2] += re_in[2];
                im_out[2] += im_in[2];
                re_out[3] -= re_in[2];
                im_out[3] -= im_in[2];
            }
            else if (i == 3)
            {
                re_out[0] += re_in[3];
                im_out[0] += im_in[3];
                re_out[1] += im_in[3];
                im_out[1] -= re_in[3];
                re_out[2] -= re_in[3];
                im_out[2] -= im_in[3];
                re_out[3] -= im_in[3];
                im_out[3] += re_in[3];
            }
        } }   void fft_16(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag ) {
        static int start_flag = 1;
        static float* re_coef, * im_coef;
        static float* re_tmp, * im_tmp;
     
        if (start_flag)
        {
            re_coef = (float*)malloc(sizeof(float) * COEF_NUM_16);
            im_coef = (float*)malloc(sizeof(float) * COEF_NUM_16);
            re_tmp = (float*)malloc(sizeof(float) * 16);
            im_tmp = (float*)malloc(sizeof(float) * 16);
     
            fft_coef_gen_16(
                re_coef,
                im_coef
            );
     
            start_flag = 0;
        }
     
        //transformation proccess
        addr_sort(
            re_in,
            im_in,
            re_tmp,
            im_tmp,
            16
        );
     
        for (int i = 0; i < 16; i += 4)
        {
            fft_4(
                re_tmp + i,
                im_tmp + i,
                re_out + i,
                im_out + i
            );
        }
     
        fft_layer_transform(
            re_out,
            im_out,
            re_coef,
            im_coef,
            4
        );
     
        //clean up
        if (end_flag)
        {
            free(re_coef);
            free(im_coef);
            free(re_tmp);
            free(im_tmp);
     
            start_flag = 1;
        } }   void addr_sort(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int total ) {
        int i, j;
        int* re_ptr = re_out, * im_ptr = im_out;
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < total; j++)
            {
                if (j % 4 == i)
                {
                    *re_out++ = re_in[j];
                    *im_out++ = im_in[j];
                }
            }
        } }   void fft_coef_gen_16(
        float* re_coef,
        float* im_coef ) {
        int i;
        for (i = 0; i < COEF_NUM_16; i++)
        {
            re_coef[i] = (float)cos(-2 * CFFT_PI * i / 16);
            im_coef[i] = (float)sin(-2 * CFFT_PI * i / 16);
        } }   void fft_layer_transform(
        float* re_out,
        float* im_out,
        float* re_coef,
        float* im_coef,
        int total ) {
        float* re_first_ptr, * im_first_ptr;
        float* re_second_ptr, * im_second_ptr;
        float* re_coef_ptr, * im_coef_ptr;
        float re_4_in[4];
        float im_4_in[4];
        float re_4_out[4];
        float im_4_out[4];
        float ac, bd, ad, bc;
        int i, j;
     
     
        re_first_ptr = re_out;
        im_first_ptr = im_out;
        for (i = 0; i < total; i++)
        {
            re_second_ptr = re_first_ptr;
            im_second_ptr = im_first_ptr;
            re_coef_ptr = re_coef;
            im_coef_ptr = im_coef;
     
            for (j = 0; j < 4; j++)
            {
                ac = (*re_second_ptr) * (*re_coef_ptr);
                bd = (*im_second_ptr) * (*im_coef_ptr);
                ad = (*re_second_ptr) * (*im_coef_ptr);
                bc = (*im_second_ptr) * (*re_coef_ptr);
     
                re_4_in[j] = ac - bd;
                im_4_in[j] = ad + bc;
     
                re_second_ptr += total;
                im_second_ptr += total;
                re_coef_ptr += i;
                im_coef_ptr += i;
            }
     
            fft_4(
                re_4_in,
                im_4_in,
                re_4_out,
                im_4_out
            );
     
            for (j = 3; j >= 0; j--)
            {
                re_second_ptr -= total;
                im_second_ptr -= total;
     
                *re_second_ptr = re_4_out[j];
                *im_second_ptr = im_4_out[j];
            }
     
            re_first_ptr++;
            im_first_ptr++;
        } }   void ifft_16(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag ) {
        static int start_flag = 1;
        static float* re_coef, * im_coef;
        static float* re_tmp, * im_tmp;
     
        if (start_flag)
        {
            re_coef = (float*)malloc(sizeof(float) * COEF_NUM_16);
            im_coef = (float*)malloc(sizeof(float) * COEF_NUM_16);
            re_tmp = (float*)malloc(sizeof(float) * 16);
            im_tmp = (float*)malloc(sizeof(float) * 16);
     
            ifft_coef_gen_16(
                re_coef,
                im_coef
            );
     
            start_flag = 0;
        }
     
        //transformation process
        addr_sort(
            re_in,
            im_in,
            re_tmp,
            im_tmp,
            16
        );
     
        for (int i = 0; i < 16; i += 4)
        {
            ifft_4(
                re_tmp + i,
                im_tmp + i,
                re_out + i,
                im_out + i
            );
        }
     
        ifft_layer_transform(
            re_out,
            im_out,
            re_coef,
            im_coef,
            4
        );
     
        //clean up
        if (end_flag)
        {
            free(re_coef);
            free(im_coef);
            free(re_tmp);
            free(im_tmp);
     
            start_flag = 1;
        } }   void ifft_coef_gen_16(
        float* re_coef,
        float* im_coef ) {
        int i;
        for (i = 0; i < COEF_NUM_16; i++)
        {
            re_coef[i] = (float)cos(2 * CFFT_PI * i / 16);
            im_coef[i] = (float)sin(2 * CFFT_PI * i / 16);
        } }   void ifft_layer_transform(
        float* re_out,
        float* im_out,
        float* re_coef,
        float* im_coef,
        int total ) {
        float* re_first_ptr, * im_first_ptr;
        float* re_second_ptr, * im_second_ptr;
        float* re_coef_ptr, * im_coef_ptr;
        float re_4_in[4];
        float im_4_in[4];
        float re_4_out[4];
        float im_4_out[4];
        float ac, bd, ad, bc;
        int i, j;
     
     
        re_first_ptr = re_out;
        im_first_ptr = im_out;
        for (i = 0; i < total; i++)
        {
            re_second_ptr = re_first_ptr;
            im_second_ptr = im_first_ptr;
            re_coef_ptr = re_coef;
            im_coef_ptr = im_coef;
     
            for (j = 0; j < 4; j++)
            {
                ac = (*re_second_ptr) * (*re_coef_ptr);
                bd = (*im_second_ptr) * (*im_coef_ptr);
                ad = (*re_second_ptr) * (*im_coef_ptr);
                bc = (*im_second_ptr) * (*re_coef_ptr);
     
                re_4_in[j] = ac - bd;
                im_4_in[j] = ad + bc;
     
                re_second_ptr += total;
                im_second_ptr += total;
                re_coef_ptr += i;
                im_coef_ptr += i;
            }
     
            ifft_4(
                re_4_in,
                im_4_in,
                re_4_out,
                im_4_out
            );
     
            for (j = 3; j >= 0; j--)
            {
                re_second_ptr -= total;
                im_second_ptr -= total;
     
                *re_second_ptr = re_4_out[j];
                *im_second_ptr = im_4_out[j];
            }
     
            re_first_ptr++;
            im_first_ptr++;
        } }   void fft_64(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag ) {
        static int start_flag = 1;
        static float* re_coef, * im_coef;
        static float* re_tmp, * im_tmp;
     
        if (start_flag)
        {
            re_coef = (float*)malloc(sizeof(float) * COEF_NUM_64);
            im_coef = (float*)malloc(sizeof(float) * COEF_NUM_64);
            re_tmp = (float*)malloc(sizeof(float) * 64);
            im_tmp = (float*)malloc(sizeof(float) * 64);
     
            fft_coef_gen_64(
                re_coef,
                im_coef
            );
     
            start_flag = 0;
        }
     
        //transformation proccess
        addr_sort(
            re_in,
            im_in,
            re_tmp,
            im_tmp,
            64
        );
     
        for (int i = 0; i < 64; i += 16)
        {
            if (i == 48 && end_flag == 1)
            {
                start_flag = 1;
            }
     
            fft_16(
                re_tmp + i,
                im_tmp + i,
                re_out + i,
                im_out + i,
                start_flag
            );
        }
     
        fft_layer_transform(
            re_out,
            im_out,
            re_coef,
            im_coef,
            16
        );
     
        //clean up
        if (end_flag)
        {
            free(re_coef);
            free(im_coef);
            free(re_tmp);
            free(im_tmp);
        } }   void fft_coef_gen_64(
        float* re_coef,
        float* im_coef ) {
        int i;
        for (i = 0; i < COEF_NUM_64; i++)
        {
            re_coef[i] = (float)cos(-2 * CFFT_PI * i / 64);
            im_coef[i] = (float)sin(-2 * CFFT_PI * i / 64);
        } }   void ifft_64(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag ) {
        static int start_flag = 1;
        static float* re_coef, * im_coef;
        static float* re_tmp, * im_tmp;
     
        if (start_flag)
        {
            re_coef = (float*)malloc(sizeof(float) * COEF_NUM_64);
            im_coef = (float*)malloc(sizeof(float) * COEF_NUM_64);
            re_tmp = (float*)malloc(sizeof(float) * 64);
            im_tmp = (float*)malloc(sizeof(float) * 64);
     
            ifft_coef_gen_64(
                re_coef,
                im_coef
            );
     
            start_flag = 0;
        }
     
        //transformation proccess
        addr_sort(
            re_in,
            im_in,
            re_tmp,
            im_tmp,
            64
        );
     
        for (int i = 0; i < 64; i += 16)
        {
            if (i == 48 && end_flag == 1)
            {
                start_flag = 1;
            }
     
            ifft_16(
                re_tmp + i,
                im_tmp + i,
                re_out + i,
                im_out + i,
                start_flag
            );
        }
     
        ifft_layer_transform(
            re_out,
            im_out,
            re_coef,
            im_coef,
            16
        );
     
        //clean up
        if (end_flag)
        {
            free(re_coef);
            free(im_coef);
            free(re_tmp);
            free(im_tmp);
        } }   void ifft_coef_gen_64(
        float* re_coef,
        float* im_coef ) {
        int i;
        for (i = 0; i < COEF_NUM_64; i++)
        {
            re_coef[i] = (float)cos(2 * CFFT_PI * i / 64);
            im_coef[i] = (float)sin(2 * CFFT_PI * i / 64);
        } }   void ifft_coef_gen_256(
        float* re_coef,
        float* im_coef ) {
        int i;
        for (i = 0; i < COEF_NUM_256; i++)
        {
            re_coef[i] = (float)cos(2 * CFFT_PI * i / 256);
            im_coef[i] = (float)sin(2 * CFFT_PI * i / 256);
        } }   void fft_coef_gen_256(
        float* re_coef,
        float* im_coef ) {
        int i;
        for (i = 0; i < COEF_NUM_256; i++)
        {
            re_coef[i] = (float)cos(-2 * CFFT_PI * i / 256);
            im_coef[i] = (float)sin(-2 * CFFT_PI * i / 256);
        } }   void ifft_256(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag ) {
        static int start_flag = 1;
        static float* re_coef, * im_coef;
        static float* re_tmp, * im_tmp;
     
        if (start_flag)
        {
            re_coef = (float*)malloc(sizeof(float) * COEF_NUM_256);
            im_coef = (float*)malloc(sizeof(float) * COEF_NUM_256);
            re_tmp = (float*)malloc(sizeof(float) * 256);
            im_tmp = (float*)malloc(sizeof(float) * 256);
     
            ifft_coef_gen_256(
                re_coef,
                im_coef
            );
     
            start_flag = 0;
        }
     
        //transformation proccess
        addr_sort(
            re_in,
            im_in,
            re_tmp,
            im_tmp,
            256
        );
     
        for (int i = 0; i < 256; i += 64)
        {
            if (i == 192 && end_flag == 1)
            {
                start_flag = 1;
            }
     
            ifft_64(
                re_tmp + i,
                im_tmp + i,
                re_out + i,
                im_out + i,
                start_flag
            );
        }
     
        ifft_layer_transform(
            re_out,
            im_out,
            re_coef,
            im_coef,
            64
        );
     
        //clean up
        if (end_flag)
        {
            free(re_coef);
            free(im_coef);
            free(re_tmp);
            free(im_tmp);
        } }   void fft_256(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag ) {
        static int start_flag = 1;
        static float* re_coef, * im_coef;
        static float* re_tmp, * im_tmp;
     
        if (start_flag)
        {
            re_coef = (float*)malloc(sizeof(float) * COEF_NUM_256);
            im_coef = (float*)malloc(sizeof(float) * COEF_NUM_256);
            re_tmp = (float*)malloc(sizeof(float) * 256);
            im_tmp = (float*)malloc(sizeof(float) * 256);
     
            fft_coef_gen_256(
                re_coef,
                im_coef
            );
     
            start_flag = 0;
        }
     
        //transformation proccess
        addr_sort(
            re_in,
            im_in,
            re_tmp,
            im_tmp,
            256
        );
     
        for (int i = 0; i < 256; i += 64)
        {
            if (i == 192 && end_flag == 1)
            {
                start_flag = 1;
            }
     
            fft_64(
                re_tmp + i,
                im_tmp + i,
                re_out + i,
                im_out + i,
                start_flag
            );
        }
     
        fft_layer_transform(
            re_out,
            im_out,
            re_coef,
            im_coef,
            64
        );
     
        //clean up
        if (end_flag)
        {
            free(re_coef);
            free(im_coef);
            free(re_tmp);
            free(im_tmp);
        } }   void ifft_coef_gen_1024(
        float* re_coef,
        float* im_coef ) {
        int i;
        for (i = 0; i < COEF_NUM_1024; i++)
        {
            re_coef[i] = (float)cos(2 * CFFT_PI * i / 1024);
            im_coef[i] = (float)sin(2 * CFFT_PI * i / 1024);
        } }   void fft_coef_gen_1024(
        float* re_coef,
        float* im_coef ) {
        int i;
        for (i = 0; i < COEF_NUM_1024; i++)
        {
            re_coef[i] = (float)cos(-2 * CFFT_PI * i / 1024);
            im_coef[i] = (float)sin(-2 * CFFT_PI * i / 1024);
        } }   void ifft_1024(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag ) {
        static int start_flag = 1;
        static float* re_coef, * im_coef;
        static float* re_tmp, * im_tmp;
     
        if (start_flag)
        {
            re_coef = (float*)malloc(sizeof(float) * COEF_NUM_1024);
            im_coef = (float*)malloc(sizeof(float) * COEF_NUM_1024);
            re_tmp = (float*)malloc(sizeof(float) * 1024);
            im_tmp = (float*)malloc(sizeof(float) * 1024);
     
            ifft_coef_gen_1024(
                re_coef,
                im_coef
            );
     
            start_flag = 0;
        }
     
        //transformation proccess
        addr_sort(
            re_in,
            im_in,
            re_tmp,
            im_tmp,
            1024
        );
     
        for (int i = 0; i < 1024; i += 256)
        {
            if (i == 768 && end_flag == 1)
            {
                start_flag = 1;
            }
     
            ifft_256(
                re_tmp + i,
                im_tmp + i,
                re_out + i,
                im_out + i,
                start_flag
            );
        }
     
        ifft_layer_transform(
            re_out,
            im_out,
            re_coef,
            im_coef,
            256
        );
     
        //clean up
        if (end_flag)
        {
            free(re_coef);
            free(im_coef);
            free(re_tmp);
            free(im_tmp);
        } }   void fft_1024(
        float* re_in,
        float* im_in,
        float* re_out,
        float* im_out,
        int end_flag ) {
        static int start_flag = 1;
        static float* re_coef, * im_coef;
        static float* re_tmp, * im_tmp;
     
        if (start_flag)
        {
            re_coef = (float*)malloc(sizeof(float) * COEF_NUM_1024);
            im_coef = (float*)malloc(sizeof(float) * COEF_NUM_1024);
            re_tmp = (float*)malloc(sizeof(float) * 1024);
            im_tmp = (float*)malloc(sizeof(float) * 1024);
     
            fft_coef_gen_1024(
                re_coef,
                im_coef
            );
     
            start_flag = 0;
        }
     
        //transformation proccess
        addr_sort(
            re_in,
            im_in,
            re_tmp,
            im_tmp,
            1024
        );
     
        for (int i = 0; i < 1024; i += 256)
        {
            if (i == 768 && end_flag == 1)
            {
                start_flag = 1;
            }
     
            fft_256(
                re_tmp + i,
                im_tmp + i,
                re_out + i,
                im_out + i,
                start_flag
            );
        }
     
        fft_layer_transform(
            re_out,
            im_out,
            re_coef,
            im_coef,
            256
        );
     
        //clean up
        if (end_flag)
        {
            free(re_coef);
            free(im_coef);
            free(re_tmp);
            free(im_tmp);
        } }  
    #endif

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在STM32F103上实现IFFT,您需要使用一些库和工具。以下是一些步骤: 1. 安装并配置STMCubeMX和System Workbench for STM32。 2. 在CubeMX中,选择您的微控制器型号,并配置您的时钟和引脚。 3. 添加CMSIS DSP库以进行FFTIFFT计算。您可以在CubeMX中选择此库并生成代码。 4. 在System Workbench中,创建一个新的工程,并将生成的代码导入其中。 5. 在您的代码中,调用相应的CMSIS DSP库函数以执行IFFT计算。例如,您可以使用arm_rfft_fast_f32()函数将实数FFT转换为复数FFT,然后使用arm_cfft_f32()函数执行FFT计算,最后使用arm_cmplx_mag_f32()函数计算幅值。 下面是一个实现128点IFFT的示例代码: ``` #include "stm32f1xx_hal.h" #include "arm_math.h" #define FFT_SIZE 128 float32_t ifft_input[FFT_SIZE * 2]; float32_t ifft_output[FFT_SIZE * 2]; int main(void) { /* 初始化HAL库 */ HAL_Init(); /* 配置系统时钟 */ SystemClock_Config(); /* 初始化FFT输入数据 */ for (int i = 0; i < FFT_SIZE * 2; i += 2) { ifft_input[i] = sinf((float)i / FFT_SIZE * 2 * PI); ifft_input[i + 1] = 0; } /* 执行IFFT计算 */ arm_rfft_fast_f32(&arm_rfft_instance_f32, ifft_input, ifft_output, 0); arm_cfft_f32(&arm_cfft_sR_f32_len128, ifft_output, 1, 1); arm_cmplx_mag_f32(ifft_output, ifft_input, FFT_SIZE); /* 此时ifft_input中存储了IFFT结果 */ while (1) { } } /* 系统时钟配置函数 */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞天的大鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值