dspmana 发表于 2023-3-27 10:00

C语言FFT

/************FFT***********/

#include < stdio.h >
    #include < math.h >
    #include < stdlib.h >
    #define N 1000
typedef struct

{

    double real;

    double img;

}
complex;

void fft(); /*快速傅里叶变换*/

void ifft(); /*快速傅里叶逆变换*/

void initW();

void change();

void add(complex, complex, complex * ); /*复数加法*/

void mul(complex, complex, complex * ); /*复数乘法*/

void sub(complex, complex, complex * ); /*复数减法*/

void divi(complex, complex, complex * ); /*复数除法*/

void output(); /*输出结果*/

complex x, * W; /*输出序列的值*/

int size_x = 0; /*输入序列的长度,只限2的N次方*/

double PI;

int main()

{

    int i, method;

    system("cls");

    PI = atan(1) * 4;

    printf("Please input the size of x:\n");

    /*输入序列的长度*/

    scanf("%d", & size_x);

    printf("Please input the data in x:(such as:5 6)\n");

    /*输入序列对应的值*/

    for (i = 0; i < size_x; i++)

      scanf("%lf %lf", & x.real, & x.img);

    initW();

    /*选择FFT或逆FFT运算*/

    printf("Use FFT(0) or IFFT(1)?\n");

    scanf("%d", & method);

    if (method == 0)

      fft();

    else

      ifft();

    output();

    return 0;

}

/*进行基-2 FFT运算*/

void fft()

{

    int i = 0, j = 0, k = 0, l = 0;

    complex up, down, product;

    change();

    for (i = 0; i < log(size_x) / log(2); i++)

    {

      l = 1 << i;

      for (j = 0; j < size_x; j += 2 * l)

      {

            for (k = 0; k < l; k++)

            {

                mul(x, W, & product);

                add(x, product, & up);

                sub(x, product, & down);

                x = up;

                x = down;

            }

      }

    }

}

void ifft()

{

    int i = 0, j = 0, k = 0, l = size_x;

    complex up, down;

    for (i = 0; i < (int)(log(size_x) / log(2)); i++) /*蝶形运算*/

    {

      l /= 2;

      for (j = 0; j < size_x; j += 2 * l)

      {

            for (k = 0; k < l; k++)

            {

                add(x, x, & up);

                up.real /= 2;
                up.img /= 2;

                sub(x, x, & down);

                down.real /= 2;
                down.img /= 2;

                divi(down, W, & down);

                x = up;

                x = down;

            }

      }

    }

    change();

}

void initW()

{

    int i;

    W = (complex * ) malloc(sizeof(complex) * size_x);

    for (i = 0; i < size_x; i++)

    {

      W.real = cos(2 * PI / size_x * i);

      W.img = -1 * sin(2 * PI / size_x * i);

    }

}

void change()

{

    complex temp;

    unsigned short i = 0, j = 0, k = 0;

    double t;

    for (i = 0; i < size_x; i++)

    {

      k = i;
      j = 0;

      t = (log(size_x) / log(2));

      while ((t--) > 0)

      {

            j = j << 1;

            j |= (k & 1);

            k = k >> 1;

      }

      if (j > i)

      {

            temp = x;

            x = x;

            x = temp;

      }

    }

}

void output() /*输出结果*/

{

    int i;

    printf("The result are as follows\n");

    for (i = 0; i < size_x; i++)

    {

      printf("%.4f", x.real);

      if (x.img >= 0.0001)

            printf("+%.4fj\n", x.img);

      else if (fabs(x.img) < 0.0001)

            printf("\n");

      else

            printf("%.4fj\n", x.img);

    }

}

void add(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 sub(complex a, complex b, complex * c)

{

    c - > real = a.real - b.real;

    c - > img = a.img - b.img;

}

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

}

wowu 发表于 2023-4-13 17:31

FFT(Fast Fourier Transformation),即为快速傅氏变换,是离散傅氏变换的快速算法,它是根据离散傅氏变换的奇、偶、虚、实等特性,对离散傅立叶变换的算法进行改进获得的

tpgf 发表于 2023-4-14 09:20

想要看懂这个代码 就得懂傅里叶变换 就得明白复数是什么

xiaoqizi 发表于 2023-4-14 11:10

在这个过程中我们如何解决复数运算的问题呢

木木guainv 发表于 2023-4-14 11:30

FFT算法是把长序列的DFT逐次分解为较短序列的DFT

磨砂 发表于 2023-4-14 12:04

FFT即为快速傅里叶变换,是离散傅里叶变换的快速算法,它是根据离散傅里叶变换的奇、偶、虚、实等特性,对离散傅里叶变换的算法进行改进获得的

晓伍 发表于 2023-4-14 12:19

FFT算法按照抽取方式的不同可分为DIT-FFT(按时间抽取)和DIF-FFT(按频率抽取)算法

abotomson 发表于 2023-5-11 15:25

二维FFT相当于对行和列分别进行一维FFT运算?

mickit 发表于 2023-5-11 16:21

MATLAB和Python等科学计算工具中都包括了FFT相关的库函数

jimmhu 发表于 2023-5-11 16:52

如何用C语言或汇编语言实现FFT(快速傅里叶)变换,

lihuami 发表于 2023-5-11 17:39

实现FFT需要了解FFT算法原理,以及掌握C语言的相关编程技巧。

sdCAD 发表于 2023-5-11 18:57

C语言中有一些现成的FFT库函数,例如FFTW、PFFFT等。使用这些库函数可以更方便地实现FFT操作,而且通常具有较高的精度和效率。

averyleigh 发表于 2023-5-11 19:18

如果想要深入学习FFT算法或者需要对FFT算法进行优化,则可以手动编写FFT代码。FFT算法包括Cooley-Tukey算法、Radix-2算法等,可以根据具体情况进行选择

kkzz 发表于 2023-5-11 19:35

用C语言实现FFT算法,还要做频谱分析

fengm 发表于 2023-5-11 20:41

想用C语言实现一个1024点的FFT

jimmhu 发表于 2023-5-11 20:53

傅里叶变换用C语言程序怎么实现?

youtome 发表于 2023-5-11 21:03

FFT的最优算法是什么?            

qiufengsd 发表于 2023-5-11 22:05

使用现成的库函数,可以快速实现FFT操作。

理想阳 发表于 2023-8-10 09:37

要实现FFT需要了解FFT算法的原理,并掌握c语言的编程技巧。

LinkMe 发表于 2023-8-10 09:49

二维FFT分别相当于一维FFT的行和列计算?
页: [1]
查看完整版本: C语言FFT