void main(void)
{
	Uint16	i;

	InitSysCtrl();
	DINT;
	InitPieCtrl();
	IER = 0x0000;
	IFR = 0x0000;
	InitPieVectTable();
	EINT;   // Enable Global interrupt INTM
	ERTM;   // Enable Global realtime interrupt DBGM

	// Clear input buffers:
	for(i=0; i < (CFFT_SIZE*2); i=i+2)
		{
		CFFTin1Buff[i] = 0.0f;
		CFFTin1Buff[i+1] = 0.0f;
		CFFTin2Buff[i] = 0.0f;
		CFFTin2Buff[i+1] = 0.0f;
		CFFToutBuff[i] = 0.0f;
		CFFToutBuff[i+1] = 0.0f;
		}
	
// Generate sample waveforms:
//	   CFFTin1Buff[0] = real[0]
//	   CFFTin1Buff[1] = imag[0]
//	   CFFTin1Buff[2] = real[1]
//	   ………
//	   CFFTin1Buff[N] = real[N/2]
//	   CFFTin1Buff[N+1] = imag[N/2]
//	   ………
//	   CFFTin1Buff[2N-3] = imag[N-2]
//	   CFFTin1Buff[2N-2] = real[N-1]
//	   CFFTin1Buff[2N-1] = imag[N-1]
			
	Rad = 0.0f;
	for(i=0; i < (CFFT_SIZE*2); i=i+2)
		{
		CFFTin1Buff[i]   = sin(Rad) + cos(Rad*2.3567);       // Real Part
		CFFTin1Buff[i+1] = cos(Rad*8.345) + sin(Rad*5.789);  // Image Part
		CFFTin2Buff[i]   = CFFTin1Buff[i];					 // Not used in calculation
		CFFTin2Buff[i+1] = CFFTin1Buff[i+1];				 // Not used in calculation
		Rad = Rad + RadStep;
		}

//
// Comment Out Following Code To Run CFFT Between Ping-Pong Buffers:
//
// Note: In this version, CFFTin1Buff and CFFToutBuff are used in
//       ping-pong fashion. The input data is first stored in CFFTin1Buff.
//       The FFT is then calculated, including bit reversing, and
//       when done, the cfft.CurrentInPtr pointer points to
//       buffer which has the result. Depending on the FFT size,
//       it will either be in CFFTin1Buff or CFFToutBuff.
	
	cfft.CoefPtr = CFFTF32Coef;				//Twiddle factor table
	cfft.InPtr   = CFFTin1Buff;				//Input/output or middle stage of ping-pong buffer
	cfft.OutPtr  = CFFToutBuff;				//Output or middle stage of ping-pong buffer
	cfft.Stages  = CFFT_STAGES;				// FFT stages
	cfft.FFTSize = CFFT_SIZE;				// FFT size
	
	CFFT_f32_sincostable(&cfft);            // Calculate twiddle factor
	
//===========================================================================
// CFFT result:
//     CurrentInPtr[0] = real[0]
//     CurrentInPtr[1] = imag[0]
//     CurrentInPtr[2] = real[1]
//     ………
//     CurrentInPtr[N] = real[N/2]
//     CurrentInPtr[N+1] = imag[N/2]
//     ………
//     CurrentInPtr[2N-3] = imag[N-2]
//     CurrentInPtr[2N-2] = real[N-1]
//     CurrentInPtr[2N-1] = imag[N-1]
//
//=============================================================================
	
	CFFT_f32(&cfft);						// Calculate FFT

//
// Note: To calculate magnitude, the input data is pointed by cfft.CurrentInPtr.
//       The calculated magnitude is stored in the memory pointed by
//       cfft.CurrentOutPtr. If not changing cfft.CurrentOutPtr after called
//       magnitude calculation function, the output buffer would be overwrite
//       right after phase calculation function called.
//
//       If Stages is ODD, the currentInPtr=CFFTin1Buff, currentOutPtr=CFFToutBuff
//       If Stages is Even, the currentInPtr=CFFToutBuff, currentOutPtr=CFFTin1Buff
//

	// Calculate Magnitude:
	CFFT_f32_mag(&cfft);					// Calculate magnitude, result stored in CurrentOutPtr

// Magnitude Result:
//	   CurrentOutPtr[0] = Mag[0]
//	   CurrentOutPtr[1] = Mag[1]
//	   CurrentOutPtr[2] = Mag[2]
//	   ………
//	   CurrentOutPtr[N-1] = Mag[N-1]

	// Calculate Phase:
    cfft.CurrentOutPtr=CFFTin2Buff;			// To avoid overwrite magnitude, changed the output of phase
    										// to CFFTin2Buff
	CFFT_f32_phase(&cfft); 					// Calculate phase, result stored in CurrentOutPtr
	
// Phase Result:
//	   CurrentOutPtr[0] = Phase[0]
//	   CurrentOutPtr[1] = Phase[1]
//	   CurrentOutPtr[2] = Phase[2]
//	   ………
//	   CurrentOutPtr[N-1] = Phase[N-1]

	// Just sit and loop forever (optional):
   	for(;;);

}
Esempio n. 2
0
int main(void){

	float Es,E;
	unsigned int i, j;
	unsigned int doa_aux[3];
	int diff[3];

#ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif


	/*------------------------------------------------------*/
	/*                   Inicialización                     */
	/*------------------------------------------------------*/
    InitSysCtrl();
    InitSysPll(XTAL_OSC,IMULT_20,FMULT_0,PLLCLK_BY_2);
    EDIS;
    InitGpio();
    InitPieCtrl();

    IER = 0x0000;
    IFR = 0x0000;

    InitPieVectTable();

    //Configura operación del ADC-A
    ADC_Configure(ADCA,16000);

    //Configura los canales 2,3,4 y 5 del ADC-A
    ADC_Init(ADCA, 2);
    ADC_Init(ADCA, 3);
    ADC_Init(ADCA, 4);

    //La interrupción del ADC-A se da cuando termine el canal 4
    ADC_Int(ADCA, 4);


    hnd_cfft->OutPtr  = CFFToutBuff;  // Apuntador al Buffer de salida
    hnd_cfft->Stages  = STAGE;  // Número de etapas de la FFT
    hnd_cfft->FFTSize = N;    // Tamaño de la FFT

    hnd_cfft->CoefPtr = CFFTF32Coef;  // Auntador a los coeficientes de Fourier
    CFFT_f32_sincostable(hnd_cfft);   // Calcula los factores de Fourier


    //Configura el puerto serial
    Serial_Init();
	Serial_Configure(BR9600);
	Serial_Start();

	ADC_Start(ADCA);   //Inicia la conversión del ADC-A
	cont = 0;

    //calculo de la energía del silencio (que filosófico suena esto)
	EINT;
	Es = 0;
	while(cont<(N<<1));
	ServiceDog();
	cont = 0;
	Es = energy(x);
	while(cont<(N<<1));
	ServiceDog();
	cont = 0;
	Es += energy(x);
	Es = Es/2;
	doaG = 0;

	while(1){

#ifndef DEBUG
		init = false;
		while(!init);
#endif
		E = Es;
		for(j=0;j<3;j++){

			//recibe datos y verifica si es ruido o no
			do{
				ADC_Start(ADCA);
				while(cont<(N<<1));
				cont = 0;    	//Una vez llenos los buffers de datos procedemos a realizar el algoritmo
				ADC_Stop(ADCA); //detiene la adquisición para obtener las FFT
				ServiceDog();
			}while(!vad(x,E));


			//FFT mic 1
			hnd_cfft->InPtr = x1;
			CFFT_f32u(hnd_cfft);
			for(i=0;i<(N<<1);i++){
				xw1[i] = hnd_cfft->CurrentInPtr[i];
			}

			//FFT mic 2
			hnd_cfft->InPtr = x2;
			CFFT_f32u(hnd_cfft);
			for(i=0;i<(N<<1);i++){
				xw2[i] = hnd_cfft->CurrentInPtr[i];
			}

			//FFT mic 3
			hnd_cfft->InPtr = x3;
			CFFT_f32u(hnd_cfft);
			for(i=0;i<(N<<1);i++){
				xw3[i] = hnd_cfft->CurrentInPtr[i];
			}

			ServiceDog();

			doa_aux[j]  = doa_est(xw1,xw2,xw3,30); //50
			//doaG +=doa_aux[j];


			DELAY_US(100000); //retraso de 100ms
			ServiceDog();
			E = 0.8*E;

		}//for j

		diff[0] = doa_aux[0]-doa_aux[1]; //diferencia entre primer y segundo frame
		diff[1] = doa_aux[1]-doa_aux[2]; //diferencia entre segundo y tercer frame
		diff[2] = doa_aux[0]-doa_aux[2]; //diferencia entre primer y tercer frame


		if(diff[0]<0)
			diff[0] = -diff[0];
		if(diff[1]<0)
			diff[1] = -diff[1];
		if(diff[2]<0)
			diff[2] = -diff[2];

		if( diff[0]<=diff[1] && diff[0]<=diff[2] )
			doaG = (doa_aux[0]+doa_aux[1])>>1;
		else if ( diff[1]<=diff[0] && diff[1]<=diff[2] )
			doaG = (doa_aux[1]+doa_aux[2])>>1;
		else