void FFTMotor(){
fractional *p_real = &sigCmpx[0].real ;
fractcomplex *p_cmpx = &sigCmpx[0] ;
in = 0;
if(ADCReturnFlagADC() == 1) // agafo dades
{
// sigCmpx[inde].real = ADCBUF3; // Load the DAC buffer with data
// inde++;
// sprintf(uTxt, "\r\n sigCmpx %d \r\n", sigCmpx[inde].real);
// putsUART1((unsigned int *)uTxt);
ADCBaixaFlagADC();
}
// if(inde == FFT_BLOCK_LENGTH){ //si ja tinc totes les dades faig la FFT
if(ADCReturnFlagDada()==1){
for (in = 0; in < FFT_BLOCK_LENGTH; in++){
sprintf(uTxt, "\r\n sigCmpx %d \r\n", sigCmpx[in].real);
putsUART1((unsigned int *)uTxt);
}
#ifndef FFTTWIDCOEFFS_IN_PROGMEM // Generate TwiddleFactor Coefficients
TwidFactorInit (LOG2_BLOCK_LENGTH, &twiddleFactors[0], 0); // We need to do this only once at start-up
#endif
for ( in = 0; in < FFT_BLOCK_LENGTH; in++ )// The FFT function requires input data
{ // to be in the fractional fixed-point range [-0.5, +0.5]
*p_real = *p_real >>1 ; // So, we shift all data samples by 1 bit to the right.
*p_real++; // Should you desire to optimize this process, perform
} // data scaling when first obtaining the time samples
// Or within the BitReverseComplex function source code
p_real = &sigCmpx[(FFT_BLOCK_LENGTH/2)-1].real ; // Set up pointers to convert real array
p_cmpx = &sigCmpx[FFT_BLOCK_LENGTH-1] ; // to a complex array. The input array initially has all
// the real input samples followed by a series of zeros
for ( in = FFT_BLOCK_LENGTH; in > 0; in-- ) // Convert the Real input sample array
{ // to a Complex input sample array
(*p_cmpx).real = (*p_real--); // We will simpy zero out the imaginary
(*p_cmpx--).imag = 0x0000; // part of each data sample
}
// Perform FFT operation
#ifndef FFTTWIDCOEFFS_IN_PROGMEM
FFTComplexIP (LOG2_BLOCK_LENGTH, &sigCmpx[0], &twiddleFactors[0], COEFFS_IN_DATA);
#else
FFTComplexIP (LOG2_BLOCK_LENGTH, &sigCmpx[0], (fractcomplex *) __builtin_psvoffset(&twiddleFactors[0]), (int) __builtin_psvpage(&twiddleFactors[0]));
#endif
// Store output samples in bit-reversed order of their addresses
BitReverseComplex (LOG2_BLOCK_LENGTH, &sigCmpx[0]);
// Compute the square magnitude of the complex FFT output array so we have a Real output vetor
SquareMagnitudeCplx(FFT_BLOCK_LENGTH, &sigCmpx[0], &sigCmpx[0].real);
// Find the frequency Bin ( = index into the sigCmpx[] array) that has the largest energy
// i.e., the largest spectral component
VectorMax(FFT_BLOCK_LENGTH/2, &sigCmpx[0].real, &peakFrequencyBin);
// Compute the frequency (in Hz) of the largest spectral component
peakFrequency = peakFrequencyBin*(SAMPLING_RATE/FFT_BLOCK_LENGTH);
sprintf(uTxt, "\r\n Frequencia de Pic: %ld \r\n", peakFrequency);
putsUART1((unsigned int *)uTxt);
inde = 0; // torno a posar l'index del buffer a 0. I tornem a començar
}
}