STM32F303的ADC采样后做FFT算频率,ADC时钟72M,采样间隔74个ADC cycles,采样频率约972.972K,大于那奎斯特采样要求,采样数量1024个点,信号源信号频率200K,3.00Vpp,offset1.65Vdc,实际算出来频率190033.593750,为什么FFT结果差了约10K,而且采样频率越低,算出来的准确度还高些,高了反而差距大?
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_DAC1_Init();
MX_TIM2_Init();
MX_ADC1_Init();
MX_TIM3_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim2);
HAL_TIM_Base_Start(&htim3);
//// HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0);
//// HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1,(uint32_t*) Sine, 32, DAC_ALIGN_12B_R);
HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*) ReadData, Num);
//// HAL_ADC_Stop_DMA(&hadc1);
// HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t*) ReadData, Num, DAC_ALIGN_8B_R);
// HAL_UART_Transmit_DMA(&huart2, (uint8_t*) ReadData, Num);
// HAL_UART_Transmit(&huart2, 0x11, 1, 0xFFFF);
// printf("\r\nADC was Down!\r\n");
for(int i=0;i<Num;i++)
{
ADC_ConvertedValue[2*i]=(float)(ReadData&0xFF)*3.3/256; //将ADC取到的值转化为0-3.3的电压值,为实部
ADC_ConvertedValue[2*i+1]=0; //虚部为0
}
// for(int i=0;i<Num;i++)
// {
// ADC_ConvertedValue[2*i]=(float)(ReadData&0xFF); //将ADC取到的值转化为0-3.3的电压值,为实部
// ADC_ConvertedValue[2*i+1]=0; //虚部为0
// }
arm_cfft_f32(&arm_cfft_sR_f32_len1024, ADC_ConvertedValue, ifftFlag, doBitReverse);
arm_cmplx_mag_f32(ADC_ConvertedValue, testOutput, Num);
for(uint32_t i=0;i<Num;i++)
{
printf("fft_outputbuf[%d]:%f\r\n",i,testOutput);
}
testOutput[0]=0;
/* Calculates maxValue and returns corresponding BIN value */
arm_max_f32(testOutput, Num/2, &maxValue, &testIndex);
Frequence = (((float32_t)testIndex+1)/(float32_t)Num)*(72000000/74);
printf("maxValue is %f\r,corresponding BIN value is %d\r,frequence is %f\r\n",maxValue,testIndex,Frequence);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
|