#define ARM_MATH_CM4
//#include "core_cm3.h"
#define __STATIC_INLINE static __inline
#define __CLZ __clz
#define __SSAT __ssat
#include "stm32f302xc.h"
#include "arm_math.h"
#define NPT 1024 //1024点FFT
#define Fs 8000 //音频采样率
#define PI2 6.28318530717959
float testOutput_f32[NPT];
float testOutput[NPT];
float testInput_f[NPT];
uint8_t I2S_DMA_CMPLX;
short testInput_sl[NPT];
short testInput_sr[NPT];
void arm_rfft_fast_f32_app(float32_t *testInput_f);
extern I2S_HandleTypeDef hi2s2;
extern DMA_HandleTypeDef hdma_spi2_rx;
void FFTThread (void const *argument) {
I2S_DMA_CMPLX=0;
uint16_t data;
uint16_t lc=0,rc=0;
//hi2s2.Instance->CR2|= 0x00020;//CR2->RXDMAEN=1;
//hi2s2.Instance->CR2 |= 0x0040;//CR2->RXNEIE=1;
//hi2s2.Instance->I2SCFGR&= 0xFBFF;// I2SCFGR->I2SE=0;
hi2s2.Instance->I2SCFGR|= 0x0400;// I2SCFGR->I2SE=1;
while(1){
while(1)
{
while(!(hi2s2.Instance->SR & 0x01));
data = hi2s2.Instance->DR;
if(!(hi2s2.Instance->SR & 0x04))
{
testInput_sr[rc++] = data;
}
else
{
testInput_sl[lc++] = data;
}
if((lc>=NPT)||(rc>=NPT))
{
//// HAL_UART_Transmit( &huart1, (unsigned char*)&testInput_f32.c, 1024*4,60000);
lc=0;rc=0;
break;
}
// osDelay(1);
// }
// for(i=0;i<512;i++)
// {
// HAL_UART_Transmit( &huart1, (unsigned char*)&testInput_f32.c[i*8+4], 4,60000);
// }
// i=0;
}
// HAL_I2S_Receive_DMA(&hi2s2,(uint16_t*)&testInput_f32.s,NPT*2);
for(data=0;data<NPT;data++)
{//这里假设音频最大电压3000mV
testInput_f[data] = testInput_sr[data]*(3000.0/0xffff);
}
arm_rfft_fast_f32_app(testInput_f);
for(data=0;data<NPT;data++)
{
testInput_f[data] = testInput_sl[data]*(3000.0/0xffff);
}
arm_rfft_fast_f32_app(testInput_f);
osThreadYield (); // suspend thread
}
}
void arm_rfft_fast_f32_app(float32_t *testInput_f)
{
uint16_t i;
char buf[20];
arm_rfft_fast_instance_f32 S;
uint16_t fftSize = NPT;
uint8_t ifftFlag = 0;
arm_rfft_fast_init_f32(&S, fftSize);
// for(i=0; i<1024; i++)
// {
// /*3??? 50Hz 2500Hz 2550Hz */
// testInput_f[i] = //1000*arm_sin_f32(PI2*i*50.0/Fs) +
// //2000*arm_sin_f32(PI2*i*1000.0/Fs) +
// 3000*arm_sin_f32(PI2*i*1000.0/Fs);
// }
arm_rfft_fast_f32(&S, testInput_f, testOutput_f32, ifftFlag);
arm_cmplx_mag_f32(testOutput_f32, testOutput, fftSize);
testOutput[0]=testOutput[0]/fftSize;
// sprintf(buf,"%f\r\n", testOutput[0]);
// USART1_send_string(buf);
for(i=1; i<fftSize/2; i++)
{
testOutput[i]=testOutput[i]/(fftSize/2);
// sprintf(buf,"%f\r\n", testOutput[i]);
// USART1_send_string(buf);
}
// while(1);
}