代码在官方的例子里稍做修改而来,频率计算准确.相位不对.
#include "arm_math.h"
#include "arm_const_structs.h"
#define TEST_LENGTH_SAMPLES 256
#if TEST_LENGTH_SAMPLES==2048
#define ARM_CFFT_SR_F32_LEN arm_cfft_sR_f32_len1024
#elif TEST_LENGTH_SAMPLES==1024
#define ARM_CFFT_SR_F32_LEN arm_cfft_sR_f32_len512
#elif TEST_LENGTH_SAMPLES==512
#define ARM_CFFT_SR_F32_LEN arm_cfft_sR_f32_len256
#elif TEST_LENGTH_SAMPLES==256
#define ARM_CFFT_SR_F32_LEN arm_cfft_sR_f32_len128
#endif
//#define TEST_F 2300
uint16_t TEST_F = 5000;
/* -------------------------------------------------------------------
* External Input and Output buffer Declarations for FFT Bin Example
* ------------------------------------------------------------------- */
float32_t testInput_f32_10khz[TEST_LENGTH_SAMPLES*2];
static float32_t testOutput[TEST_LENGTH_SAMPLES/2];
/* ------------------------------------------------------------------
* Global variables for FFT Bin Example
* ------------------------------------------------------------------- */
uint32_t fftSize = TEST_LENGTH_SAMPLES/2;
uint32_t ifftFlag = 0;
uint32_t doBitReverse = 1;
/* Reference index at which max energy of bin ocuurs */
uint32_t refIndex = 213, testIndex = 0;
volatile float32_t ph;
/* ----------------------------------------------------------------------
* Max magnitude FFT Bin test
* ------------------------------------------------------------------- */
float32_t fre1;
float32_t n;
int32_t main(void)
{
uint16_t i,j=0;
arm_status status;
float32_t maxValue;
status = ARM_MATH_SUCCESS;
while(1)
{
//for(i=0; i<fftSize; i++)TEST_LENGTH_SAMPLES
n++;
for(i=0; i<TEST_LENGTH_SAMPLES; i++)
{
testInput_f32_10khz[i*2+1] = 0;
testInput_f32_10khz[i*2] = arm_sin_f32(2*3.1415926f*TEST_F/10000*i+PI/180*n);//模拟一路5K正弦信号,10K采样
}
for(i=1;i<TEST_LENGTH_SAMPLES;i++)//
{
//testInput_f32_10khz[i*2]=testInput_f32_10khz[i*2]*conv[i-1];
}
for(i=0;i<TEST_LENGTH_SAMPLES/2;i++)//
{
//testInput_f32_10khz[i*2]+=testInput_f32_10khz[(i+TEST_LENGTH_SAMPLES/2)*2];
}
/* Process the data through the CFFT/CIFFT module */
arm_cfft_f32(&ARM_CFFT_SR_F32_LEN, testInput_f32_10khz, ifftFlag, doBitReverse);
/* Process the data through the Complex Magnitude Module for
calculating the magnitude at each bin */
arm_cmplx_mag_f32(testInput_f32_10khz, testOutput, fftSize);
/* Calculates maxValue and returns corresponding BIN value */
arm_max_f32(testOutput, fftSize, &maxValue, &testIndex);
fre1=((float)(testIndex)/fftSize)*10000; //计算频率
maxValue=atan2(testInput_f32_10khz[testIndex+1],testInput_f32_10khz[testIndex]);
ph=maxValue;
ph*=180;
ph/=PI; //角度
if(testIndex != refIndex)
{
status = ARM_MATH_TEST_FAILURE;
}
} /* main function does not return */
}
|