找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5723|回复: 0
打印 上一主题 下一主题
收起左侧

AVR单片机的音乐频谱,tft显示,32点FFT变换

[复制链接]
跳转到指定楼层
楼主
实物图:



  1. /*
  2. * FFT.c
  3. *
  4. * Created: 2013/5/1 0:16:05
  5. *  Author: zhanddkk
  6. */
  7. #include <math.h>
  8. #include "../Include/FFT.h"
  9. float sin_tab[FFT_N],cos_tab[FFT_N];
  10. //-------------------------------------------//
  11. //函数名:FFT初始化函数
  12. //入口:void
  13. //出口:void
  14. //功能:建立sin()、cos()表
  15. //-------------------------------------------//
  16. void FFT_Init()
  17. {
  18.         uchar i;
  19.         for (i=0;i<FFT_N;i++)
  20.         {
  21.                 sin_tab[i]=sin(PI*2*i/FFT_N);
  22.                 cos_tab[i]=cos(PI*2*i/FFT_N);
  23.         }
  24. }
  25. //-------------------------------------------//
  26. //函数名:FFT运算函数
  27. //入口:compx *xin:需要计算的FFT数据表结构体指针
  28. //出口:void
  29. //功能:FFT运算,最后得到峰值功率频谱
  30. //-------------------------------------------//
  31. void FFT(compx *xin)
  32. {
  33.         uchar i,j=0,k;
  34.         uchar L,b,p;
  35.         uchar Nv2,Nm1;
  36.         float TR,TI;
  37.         float tempData_rc,tempData_is,tempData_rs,tempData_ic;
  38.         //float t;
  39.         Nv2=FFT_N/2;                  //变址运算,即把自然顺序变成倒位序,采用雷德算法
  40.         Nm1=FFT_N-1;
  41.         //--倒序运算--//
  42.         for(i=0;i<Nm1;i++)
  43.         {
  44.                 if(i<j)
  45.                 {
  46.                         TI=xin[j].real;
  47.                         xin[j].real=xin[i].real;
  48.                         xin[i].real=TI;
  49.                 }
  50.                 k=Nv2;
  51.                 while(k<=j)
  52.                 {
  53.                         j=j-k;
  54.                         k=k/2;
  55.                 }
  56.                 j=j+k;
  57.         }
  58.         //--基2的FFT蝶形运算--//
  59.         for (L=1;L<=FFT_m;L++)                /* Loop_1 L是L级蝶形运算 (2^FFT_m=FFT_N)*/
  60.         {                       
  61.                 b=1<<(L-1);                                /* b=2^(L-1) b是进行蝶形运算的两个数据的距离 */
  62.                 for (j=0;j<b;j++)                /* Loop_2 根据b进行三级的蝶形运算 */
  63.                 {
  64.                         p=1<<(FFT_m-L);                /* p=2^(FFT_m-L) 旋转因子计算 */
  65.                         p*=j;
  66.                         for (k=j;k<FFT_N;k+=(2*b))
  67.                         {
  68.                                 TR=xin[k].real;
  69.                                 TI=xin[k].imag;
  70.                                
  71.                                 tempData_rc=xin[k+b].real*cos_tab[p];
  72.                                 tempData_is=xin[k+b].imag*sin_tab[p];
  73.                                 tempData_rs=xin[k+b].real*sin_tab[p];
  74.                                 tempData_ic=xin[k+b].imag*cos_tab[p];
  75.                                
  76.                                 xin[k].real=xin[k].real+tempData_rc+tempData_is;
  77.                                 xin[k].imag=xin[k].imag-tempData_rs+tempData_ic;
  78.                                 xin[k+b].real=TR-tempData_rc-tempData_is;
  79.                                 xin[k+b].imag=TI+tempData_rs-tempData_ic;
  80.                         }                       
  81.                 }
  82.         }
  83.         //--功率峰值计算--//
  84.         for (i=1;i<=Nv2;i++)
  85.         {
  86.                 xin[i].real=(uint)sqrt(xin[i].real*xin[i].real+xin[i].imag*xin[i].imag)/(FFT_N/2);
  87.         }       
  88. }       

复制代码


  1. /*
  2. * MusicSpectrum.c
  3. *
  4. * Created: 2013/4/30 19:34:05
  5. *  Author: 詹磊
  6. */
  7. #include <avr/io.h>
  8. #include <math.h>
  9. #include "../Include/main.h"
  10. #include "../Include/ADC.h"
  11. #include "../Include/TFT.h"
  12. #include "../Include/FFT.h"
  13. #define MaxData                256
  14. compx Data[ADC_ConversionsTime];
  15. int lastData[16][2],temp[2];
  16. uchar flag[16],speed1[16],speed2=10,Gain=10;
  17. void DrawingColumnar(uchar x,uchar y,uchar Wide,uchar High,int Data,int FullData,uint ColumnarColor)
  18. {
  19.         uint i,tempData1,tempData2;
  20.         uchar tempData;
  21.         if (Data>FullData)
  22.         {
  23.                 Data=FullData;
  24.         }
  25.         tempData=High-(uint)High*Data/FullData;
  26.         tempData1=tempData*Wide;
  27.         tempData2=((uint)High)*Wide;
  28.         y=159-y;
  29.         TFT_RamAddSet(x,y-High,x+Wide-1,y);
  30.         for (i=0;i<tempData1;i++)
  31.         {
  32.                 TFT_WriteByteDAT(BackgroundColor>>8);        //高八位
  33.                 TFT_WriteByteDAT(BackgroundColor);                //低八位
  34.         }
  35.         for (i=tempData1;i<tempData2;i++)
  36.         {
  37.                 TFT_WriteByteDAT(ColumnarColor>>8);                //高八位
  38.                 TFT_WriteByteDAT(ColumnarColor);                //低八位
  39.         }
  40. }
  41. void FallColumnar(uchar x,uchar y,uint High,int pData1,int pData2,int FullData,uint Color1,uint Color2)
  42. {
  43.         uchar i,tempData1,tempData2;
  44.         /*if (pData1>FullData)
  45.         {
  46.                 pData1=FullData;
  47.         }
  48.         if (pData2>FullData)
  49.         {
  50.                 pData2=FullData;
  51.         }*/
  52.         if (pData2>pData1)
  53.         {
  54.                 pData2=pData1;
  55.         }
  56.         tempData1=High-High*pData1/FullData+2;
  57.         tempData2=High-High*pData2/FullData;
  58.         y=159-y;
  59.         TFT_RamAddSet(x,y-High,x,y);
  60.         for (i=2;i<tempData1;i++)
  61.         {
  62.                 TFT_WriteByteDAT(BackgroundColor>>8);        //高八位
  63.                 TFT_WriteByteDAT(BackgroundColor);                //低八位
  64.         }
  65.         TFT_WriteByteDAT(Color1>>8);        //高八位
  66.         TFT_WriteByteDAT(Color1);                //低八位
  67.         for (i=tempData1;i<tempData2;i++)
  68.         {
  69.                 TFT_WriteByteDAT(BackgroundColor>>8);        //高八位
  70.                 TFT_WriteByteDAT(BackgroundColor);                //低八位
  71.         }
  72.         for (i=tempData2;i<High;i++)
  73.         {
  74.                 TFT_WriteByteDAT(Color2>>8);        //高八位
  75.                 TFT_WriteByteDAT(Color2);                //低八位
  76.         }
  77. }
  78. int main(void)
  79. {
  80.         uchar i;
  81.         ADCInit();
  82.         TFT_Init();
  83.         FFT_Init();
  84.         for (i=0;i<16;i++)
  85.         {
  86.                 speed1[i]=0;
  87.         }
  88.         TFT_putstr(0,0,"------ZHAN LEI------\n----MusicSpectrum----\n-----2013/04/30-----",DataRed);               
  89.     while(1)
  90.     {
  91.                 if (Time==0)
  92.                 {
  93.                         for (i=0;i<32;i++)
  94.                         {
  95.                                
  96.                                 /*
  97.                                 Data[i].real=255+sin(PI*2*i/FFT_N)*20
  98.                                                                 +sin(PI*2*i/FFT_N*2)*40
  99.                                                                 +sin(PI*2*i/FFT_N*3)*80
  100.                                                                 +sin(PI*2*i/FFT_N*4)*120
  101.                                                                 +sin(PI*2*i/FFT_N*5)*110
  102.                                                                 +sin(PI*2*i/FFT_N*6)*100
  103.                                                                 +sin(PI*2*i/FFT_N*7)*90
  104.                                                                 +sin(PI*2*i/FFT_N*7.9)*20;
  105.                                 */
  106.                                 Data[i].imag=0;                       
  107.                                 Data[i].real=ADC_Buffer[i]*Gain;                                               
  108.                         }
  109.                         ADCSRA        |=(1<<ADSC);                                                       
  110.                 }
  111.                 else
  112.                 {
  113.                         FFT(Data);
  114.                         for (i=0;i<16;i++)
  115.                         {
  116.                                 temp[0]=Data[i+1].real;
  117.                                 if (temp[0]>MaxData)
  118.                                         temp[0]=MaxData;
  119.                                 temp[1]=temp[0];//防止超量程
  120.                                 if(temp[0]<lastData[i][0]-speed1[i])//上次的顶部位置-3>本次应该的位置
  121.                                 {
  122.                                         if (flag[i]>20)
  123.                                         {
  124.                                                 speed1[i]=4;
  125.                                         }
  126.                                         else
  127.                                         {
  128.                                                 flag[i]++;
  129.                                                 speed1[i]=0;
  130.                                         }
  131.                                         temp[0]=lastData[i][0]-speed1[i];                                                                                                                                               
  132.                                 }
  133.                                 else
  134.                                         flag[i]=0;
  135.                                 if (temp[1]<lastData[i][1]-speed2)
  136.                                         temp[1]=lastData[i][1]-speed2;                                                                                                                       
  137.                                 FallColumnar(i*8,0,60,temp[0],temp[1],256,DataGreen,DataYellow);
  138.                                 lastData[i][0]=temp[0];
  139.                                 lastData[i][1]=temp[1];
  140.                         }
  141.                 }                               
  142.     }
  143. }
复制代码





下载:
MusicSpectrum.rar (87.51 KB, 下载次数: 784)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表