已回答假定已回答

基于C的单片机FFT程序

wonderwall 在 2013-1-9 詢問的問題

****************************************************************************

*                (FFT.c          FFT.h)

*

*

*             1.define a array which would contain the number you get from ADC

*               like:   //  struct  compx sample[16];

*               and after that you will use this array for FFT.

*             2.you also must define a number used for times which you will

*               operate papilionaceous(蝶形) operation.

*               like:  //  unsigned char Mum = 16;//2exp(Num)

&

*attantion: * 基于时间抽取,同址计算

*           * 要知道进行蝶形运算的数组必须是 2exp(Num)个

*           * 数组的赋值是换算成的电压值,而不是采样后的寄存器结果

*

*made by: SuLi

*Version: 1.0

*Time: 07-8-29

*****************************************************************************/

#ifndef    _FFT_h_

#define    _FFT_h_

#include   "math.h"

struct compx

{

    float real;//实部

    float imag;//虚部

};

extern void FFT(struct compx *xin, unsigned int Num);

#endif

/*

*

*             1.define a array which would contain the number you get from ADC

*               like:   //  struct  compx sample[16];

*               and after that you will use this array for FFT.

*             2.you also must define a number used for times which you will

*               operate papilionaceous(蝶形) operation.

*               like:  //  unsigned char Mum = 16;//2exp(Num)

&

*attantion: * 基于时间抽取,同址计算

*           * 要知道进行蝶形运算的数组必须是 2exp(Num)个

*           * 数组的赋值是换算成的电压值,而不是采样后的寄存器结果

*

*made by: SuLi

*Version: 1.0

*Time: 07-8-29

*****************************************************************************/

#include    "FFT.h"

/*

struct compx

{

    float real;//实部

    float imag;//虚部

}compx;*/

 

struct compx EE(struct compx b1,struct compx b2)//蝶形运算单元

{

    struct compx b3;

    b3.real=b1.real*b2.real-b1.imag*b2.imag;

    b3.imag=b1.real*b2.imag+b1.imag*b2.real;

    return(b3);

}


void FFT(struct compx *xin, unsigned int Num)

{

  int f,m,LH,nm,i,k,j,L;

  float p , ps;

  int le,B,ip;

  float pi;

  struct compx  w,t;

 

  LH="Num/2";  //首先分为两组

  f="Num";     //输入的数据个数

  for(m=1;(f=f/2)!=1;m++){;} //m 表示将数据进行2分分组,f 不过是中间变量

                             //直到不能载分 也就是说 Num = 2exp(m)

  nm="Num-2"; //

  j="Num/2";  //

 

  /*变址运算*/

  for(i=1;i<=nm;i++)//i从 1--(Num-1) (去头去尾)//j=Num/2

  {

    if(i<j){t=xin[j];xin[j]=xin[i];xin[i]=t;}

    k="LH";

    while(j>=k){j=j-k;k=k/2;}

    j="j"+k;

  }

 

//**************蝶形运算的主程序************* 

  {

    for(L=1;L<=m;L++)

      { 

        le=(int)pow(2,L)+1;

        B="le/2";

        pi="3".14159;

        for(j=0;j<=B-1;j++)

          {

            p="pow"(2,m-L)*j;

            ps="2"*pi/Num*p;

            w.real=cos(ps);

            w.imag=-sin(ps);

            for(i=j;i<=Num-1;i=i+le)

            {

                 ip="i"+B;

                 t="EE"(xin[ip],w);

                 xin[ip].real=xin[i].real-t.real;

                 xin[ip].imag=xin[i].imag-t.imag;

                 xin[i].real=xin[i].real+t.real;

                 xin[i].imag=xin[i].imag+t.imag;

          }

         }

      }

  }  

  return ;

}

結果