TI中文支持网
TI专业的中文技术问题搜集分享网站

5502利用FFT进行频谱分析

aic23的采样率为48K,在程序中写下FFT后,听到的声音莎莎的,想问下是什么问题

#include <csl.h>
#include <csl_chip.h>
#include <csl_i2c.h>
#include <csl_pll.h>
#include <csl_mcbsp.h>
#include <csl_emif.h>
#include <csl_emifBhal.h>
#include <stdio.h>
#include <math.h>
//#include "5502_FLASH.h"
#include "E2PROM_Function.h"
#include "CODEC.h"
#include "i_cmplx.h"     /* definition of the complex type */  
#include "twiddle1024.h" /* quantised and scaled Twiddle factors */
#undef  CODEC_ADDR
#define CODEC_ADDR 0x1A
short SourData1[65536]={0};
short SourData2[65536]={0};
short SourData3[65536]={0};
short OutData1[65536]={0};
short OutData2[65536]={0};
short OutData3[65536]={0};
Uint32 mod[256];
#pragma DATA_SECTION (SourData1,".Audio_in_data1");
#pragma DATA_SECTION (SourData2,".Audio_in_data2");
#pragma DATA_SECTION (SourData3,".Audio_in_data3");
#pragma DATA_SECTION (OutData1,".Audio_out_data1");
#pragma DATA_SECTION (OutData2,".Audio_out_data2");
#pragma DATA_SECTION (OutData3,".Audio_out_data3");
#define  AUDIOTRY      0xAA0A//音频试听
#define  AUDIOCOPY     0xAA07//音频存贮并回放
#define TESTCOMMAND    1     //操作命令选择 
// 定义McBSP的句柄
MCBSP_Handle hMcbsp;
short OutFFT[256]={0};
COMPLEX DOutFFT[256];
void  myFFT(COMPLEX *Y, int N);
/*————————————————————————————*/
//
// FUNCTION: MAIN
//
/*————————————————————————————*/
                  
void main(void)
{
    short  DataTempLeft = 0; // 暂存采样数据
    short  DataTempRight = 0;
    Uint16  i;
    short  TempData;
    Uint16  TestCommand =0;
// long n;
// Uint16 m=0;
 //  short p,q;
    Uint16 j=0;
    
// Initialize CSL library – This is REQUIRED !!! 
    CSL_init();
#if TESTCOMMAND==1
TestCommand =AUDIOTRY;//试听
#endif
#if TESTCOMMAND==2
TestCommand =AUDIOCOPY;//录音并回放
#endif
// The main frequency of system is 240MHz
// 该频率是为了设置IIC模块的需要设置的,为了使用I2C_setup函数
    PLL_setFreq(1, 0xC, 0, 1, 3, 3, 0);
    //EMIF初始化
     Emif_Config(); 
  
// Open McBSP port 1 and get a McBSP type handle
hMcbsp = MCBSP_open(MCBSP_PORT1,MCBSP_OPEN_RESET);
// Config McBSP port 1 by use previously defined structure
Mcbsp_Config(hMcbsp);
//I2C初始化
I2C_cofig(); 
    
//CODEC寄存器初始化
inti_AIC(); 
      
/*————————————————————————————*/               
// Receive the ADC output data of CODEC  
// Then output the received data to DAC of CODEC 
/*————————————————————————————*/
while(1)
{
switch(TestCommand)
{
/*音频试听*/
case AUDIOTRY:
    /* 左通路数据 */
while(!MCBSP_rrdy(hMcbsp)){};
DataTempLeft = MCBSP_read16(hMcbsp);
for (j=0;j<255;j++)
                { 
                 OutFFT[j]=OutFFT[j+1];
                 OutFFT[255]=DataTempLeft;
                // OutFFT[j]=DataTempLeft;
                 }
 
for (j=0;j<128;j++)
{
DOutFFT[j].real=OutFFT[2*j];
DOutFFT[j].imag=OutFFT[2*j+1];
}
/* for(j=0;j<128;j++)
    {  
            p=OutFFT[j].real;
            q=OutFFT[j].imag;     
              n=(long)p*(long)p+(long)q*(long)q;
            mod[m]=sqrt(n); 
            m++;                           
        }*/
    /* 右通路数据 */
    while(!MCBSP_rrdy(hMcbsp)){};
    DataTempRight = MCBSP_read16(hMcbsp);
/* 左声道耳机输出 */
while(!MCBSP_xrdy(hMcbsp)) {};
MCBSP_write16(hMcbsp,DataTempLeft);
/* 右声道耳机输出 */
while(!MCBSP_xrdy(hMcbsp)) {};
MCBSP_write16(hMcbsp,DataTempRight);
myFFT(DOutFFT,256);
break;
/*音频存贮并回放*/
case AUDIOCOPY:
for(i=0;i<65535;i++)
{
while(!MCBSP_rrdy(hMcbsp)){};
//SourData[i] = MCBSP_read16(hMcbsp);
TempData=MCBSP_read16(hMcbsp);
SourData1[i]=TempData;
    }
    for(i=0;i<65535;i++)
{
while(!MCBSP_rrdy(hMcbsp)){};
//SourData[i] = MCBSP_read16(hMcbsp);
TempData=MCBSP_read16(hMcbsp);
SourData2[i]=TempData;
    }
for(i=0;i<65535;i++)
{
while(!MCBSP_rrdy(hMcbsp)){};
//SourData[i] = MCBSP_read16(hMcbsp);
TempData=MCBSP_read16(hMcbsp);
SourData3[i]=TempData;
    }
    for(i=0;i<65535;i++)
{
OutData1[i] = SourData1[i];
}
for(i=0;i<65535;i++)
{
OutData2[i] = SourData2[i];
}
for(i=0;i<65535;i++)
{
OutData3[i] = SourData3[i];
}
for(i=0;i<65535;i++)
{
while(!MCBSP_xrdy(hMcbsp)) {};
MCBSP_write16(hMcbsp,OutData1[i]);
}
for(i=0;i<65535;i++)
{
while(!MCBSP_xrdy(hMcbsp)) {};
MCBSP_write16(hMcbsp,OutData2[i]);
}
for(i=0;i<65535;i++)
{
while(!MCBSP_xrdy(hMcbsp)) {};
MCBSP_write16(hMcbsp,OutData3[i]);
}
break;
default:
break;
}
}
}
void  myFFT(COMPLEX *Y, int N)
{
    Int32 temp1R, temp1I, temp2R,temp2I;  /* 32 bits temporary storage for */
      /* intermediate results */
    short tempR, tempI, c, s;      /* 16 bits temporary storages    */
    /* variables */
    Int32 TwFStep,  /* Step between twiddle factors *///旋转因子阶数
     TwFIndex, /* Index of twiddle factors *///旋转因子指数
     BLStep,    /* Step for incrementing butterfly index */
     BLdiff,   /* Difference between upper and lower butterfly legs */ //碟形运算的长度(N点)
     upperIdx,
     lowerIdx, /* upper and lower indexes of buterfly leg */ 
     i, j, k;  /* loop control variables *///循环变量
   
    BLdiff=N;
    TwFStep=1;
    for(k=N;k>1;k=(k>>1)) /* Do Log(base 2)(N) Stages */
    {
      BLStep=BLdiff;
      BLdiff=BLdiff>>1; 
      TwFIndex=0;
      for(j=0;j<BLdiff;j++)/* Nbr of twiddle factors to use=BLDiff  */
      {
      c=w256[TwFIndex].real;
      s=w256[TwFIndex].imag;
      TwFIndex=TwFIndex+TwFStep;                 
      /* Now do N/BLStep butterflies */  
      for(upperIdx=j;upperIdx<N;upperIdx+=BLStep)
          {                              
/* Calculations inside this loop avoid overflow by shifting left once
   the result of every adittion/substration and by shifting left 15 
   places the result of every multiplication. Double precision temporary
   results (32-bit) are used in order to avoid losing information because
   of overflow. Final DFT result is scaled by N (number of points), i.e.,
   2^(Nbr of stages) =2^(log(base 2) N) = N */
             
            lowerIdx=upperIdx+BLdiff;
            temp1R     = (Y[upperIdx].real – Y[lowerIdx].real)>>1;
            temp2R     = (Y[upperIdx].real + Y[lowerIdx].real)>>1;
            Y[upperIdx].real  =  (short) temp2R;
            temp1I     = (Y[upperIdx].imag – Y[lowerIdx].imag)>>1;
            temp2I     = (Y[upperIdx].imag + Y[lowerIdx].imag)>>1;
            Y[upperIdx].imag  =  (short) temp2I;
            temp2R     = (c*temp1R – s*temp1I)>>15;
            Y[lowerIdx].real  = (short) temp2R;
            temp2I     = (c*temp1I + s*temp1R)>>15;
            Y[lowerIdx].imag  =  (short) temp2I;
            }
          }
          TwFStep = TwFStep<<1; /* update separation of twiddle factors)*/
        }
/* bit reversal for resequencing data *///变址运算
j=0;
   for (i=1;i<(N-1);i++)
    {
      k=N/2;
      while (k<=j)
      {
         j = j-k;
         k=k/2;
         }
      j=j+k;
      if (i<j)
      {
         tempR=Y[j].real;
         tempI=Y[j].imag;
         Y[j].real=Y[i].real;
         Y[j].imag=Y[i].imag;
         Y[i].real=tempR;
         Y[i].imag=tempI;
         }
      }
  return;
}
Shine:

是EVM板还是自己的板子?

如果不用FFT的话,有杂音吗?

kangni pang:

回复 Shine:

是教学的实验板,不用FFT就没有杂音,用上FFT就有,,是不是16bit跟32bit不合,,还是他们的采样率不一样

kangni pang:

回复 Shine:

FFT为什么在while(1)外听歌录音效果什么都很好 ,为什么FFT一进到while(1)里面就会有莎莎的声音。。。。。。。

Tony Tang:

回复 kangni pang:

我怎么没看到什么地方调用了myFFT函数?

赞(0)
未经允许不得转载:TI中文支持网 » 5502利用FFT进行频谱分析
分享到: 更多 (0)