⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tone.c

📁 TM公司CCS平台上开发的FFT频谱分析程序。
💻 C
字号:
/*
 *  Copyright 2005 by Spectrum Digital Incorporated.
 *  All rights reserved. Property of Spectrum Digital Incorporated.
 */

/*
 *  ======== tone.c ========
 *
 *  This example uses the PCM3002 codec module of the DSK5416 Board Support
 *  Library to generate a 1KHz sine wave on the audio outputs for 5 seconds.
 *  The sine wave data is calculated and stored in an array called sinetable.
 *  The codec operates at 48KHz by default.  Since the sine wave table has 48
 *  entries per period, each pass through the inner loop takes 1 millisecond.
 *  5000 passes through the inner loop takes 5 seconds.
 *
 *  Please see the DSK5416 help file for more detailed information.
 */

#include "tonecfg.h"
#include "dsk5416.h"
#include "dsk5416_pcm3002.h"
#include <math.h>


#define PI              ( ( double )3.1415927 )


DSK5416_PCM3002_Config setup = {
    0x01ff, // Set-Up Reg 0 - Left  channel DAC attenuation
    0x01ff, // Set-Up Reg 1 - Right channel DAC attenuation
    0x0000, // Set-Up Reg 2 - Various ctl e.g. power-down modes
    0x0000  // Set-Up Reg 3 - Codec data format control
};

/*
 *  InitSineTable() - Initialize the sine wave table data
 */

int cixu[256]={0,128,64,192,32,160,96,224,16,144,80,208,48,176,112,240,
				8,136,72,200,40,168,104,232,24,152,88,216,56,184,120,248,
				4,132,68,196,36,164,100,228,20,148,84,212,52,180,116,244,
				12,140,76,204,44,172,108,236,28,156,92,220,60,188,124,252,
				2,130,66,194,34,162,98,226,18,146,82,210,50,178,114,242,10,
				138,74,202,42,170,106,234,26,154,90,218,58,186,122,250,6,
				134,70,198,38,166,102,230,22,150,86,214,54,182,118,246,14,
				142,78,206,46,174,110,238,30,158,94,222,62,190,126,254,1,129,
				65,193,33,161,97,225,17,145,81,209,49,177,113,241,9,137,73,
				201,41,169,105,233,25,153,89,217,57,185,121,249,5,133,69,197,
				37,165,101,229,21,149,85,213,53,181,117,245,13,141,77,205,45,
				173,109,237,29,157,93,221,61,189,125,253,3,131,67,195,35,163,
				99,227,19,147,83,211,51,179,115,243,11,139,75,203,43,171,107,
				235,27,155,91,219,59,187,123,251,7,135,71,199,39,167,103,231,
				23,151,87,215,55,183,119,247,15,143,79,207,47,175,111,239,31,
				159,95,223,63,191,127,255};

/*
 *  UserTask() - The main user task
 */
int input[256];
double fftr[256];
double ffti[256];
double ifftempr[256];
double ifftempi[256];
int fftabs[256];

 void UserTask()
 {
 }

void fft()
{
	double u1,u2,w1,w2,t1,t2,u1tp,u2tp;
	double tmp;
	int le,lei,i,j,m,ip;
	long tempout;
	double temp,temp1,temp2;
	
	le=1;
	for(m=1;m<=8;m++)
	{
		lei=le;
		le<<=1;
		u1=1;
		u2=0;
		tmp=PI/lei;
		w1=cos(tmp);
		w2=-sin(tmp);
		for(j=0;j<lei;j++)
		{
			for(i=j;i<256;i+=le)
			{
				ip=i+lei;
				t1=fftr[ip]*u1-ffti[ip]*u2;
				t2=fftr[ip]*u2+ffti[ip]*u1;
				fftr[ip]=fftr[i]-t1;
				ffti[ip]=ffti[i]-t2;
				fftr[i]+=t1;
				ffti[i]+=t2;
			}
			u1tp=u1*w1-u2*w2;
			u2tp=u1*w2+u2*w1;
			u1=u1tp;
			u2=u2tp;
		}
	}
	for(i=0;i<256;i++)
	{
		temp1=(fftr[i]/100);
		temp2=(ffti[i]/100);
		temp=temp1*temp1+temp2*temp2;
		tempout=(long)(temp/10);
		tempout=sqrt(tempout);
		fftabs[i]=(int)tempout;
	}
	
}
void UserTask1()
{
	int i,kk;
	Int16 tempval;    
	DSK5416_PCM3002_CodecHandle hCodec;
    /* Start the codec */
    hCodec = DSK5416_PCM3002_openCodec( 0, &setup );
	DSK5416_PCM3002_setFreq(hCodec,12000);
	for(i=0;i<10000;i++)
	{
	i++;
	i--;
	}
	for(;;){
	for(i=0;i<256;i++)
	{
		while(!DSK5416_PCM3002_read16(hCodec,&tempval));

		while(!DSK5416_PCM3002_read16(hCodec,&tempval));
		input[i]=tempval;
	}

	for(i=0;i<256;i++)
	{
		fftr[i]=input[cixu[i]];
		ffti[i]=0;
	}
	fft();
	kk=0;
	for(i=0;i<256;i++)
	{
		ifftempr[i]=fftr[cixu[i]];
		ifftempi[i]=-ffti[cixu[i]];
	}
	for(i=0;i<256;i++)
	{
		fftr[i]=ifftempr[i];
		ffti[i]=ifftempi[i];
	}
	fft();		//IFFT	   
	for(i=0;i<256;i++)
	{
	   	while(!DSK5416_PCM3002_write16(hCodec,input[i]));
	   	while(!DSK5416_PCM3002_write16(hCodec,(int)(((long)fftr[i])>>8)));
	}
    kk=1;                                                       
	}
    /* Close the codec */
//    DSK5416_PCM3002_closeCodec( hCodec );
}



void main()
{
    /* Call BSL init */
    DSK5416_init();
	UserTask1();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -