FFT是DFT的快速计算方法,在信号处理中具有“万金油”一般的作用。在STM32中依然能够对信号进行快速傅里叶变换,从而把信号的特征从频域很好地展现出来。本程序利用固定数组进行256点FFT运算,对STM32的硬件和外围设备初始化不再介绍。
平台:STM32F103C8T6
库:标准固件库V3.5.0 DSP库:V.1.4.5
单片机内调用的DSP函数为复数FFT运算,也就是说,256点的FFT需要将256个复数输入算法,得到256点复数后再求模值即可。
需要注意的是,最后需将倒序的运算结果重新排序为正序。
准备工作:
正余弦表:
1.0f,0.0f,
0.999699f,0.0245412f,
0.998795f,0.0490677f,
0.99729f,0.0735646f,
0.995185f,0.0980171f,
0.99248f,0.122411f,
0.989177f,0.14673f,
0.985278f,0.170962f,
0.980785f,0.19509f,
0.975702f,0.219101f,
0.970031f,0.24298f,
0.963776f,0.266713f,
0.95694f,0.290285f,
0.949528f,0.313682f,
0.941544f,0.33689f,
0.932993f,0.359895f,
0.92388f,0.382683f,
0.91421f,0.405241f,
0.903989f,0.427555f,
0.893224f,0.449611f,
0.881921f,0.471397f,
0.870087f,0.492898f,
0.857729f,0.514103f,
0.844854f,0.534998f,
0.83147f,0.55557f,
0.817585f,0.575808f,
0.803208f,0.595699f,
0.788346f,0.615232f,
0.77301f,0.634393f,
0.757209f,0.653173f,
0.740951f,0.671559f,
0.724247f,0.689541f,
0.707107f,0.707107f,
0.689541f,0.724247f,
0.671559f,0.740951f,
0.653173f,0.757209f,
0.634393f,0.77301f,
0.615232f,0.788346f,
0.595699f,0.803208f,
0.575808f,0.817585f,
0.55557f,0.83147f,
0.534998f,0.844854f,
0.514103f,0.857729f,
0.492898f,0.870087f,
0.471397f,0.881921f,
0.449611f,0.893224f,
0.427555f,0.903989f,
0.405241f,0.91421f,
0.382683f,0.92388f,
0.359895f,0.932993f,
0.33689f,0.941544f,
0.313682f,0.949528f,
0.290285f,0.95694f,
0.266713f,0.963776f,
0.24298f,0.970031f,
0.219101f,0.975702f,
0.19509f,0.980785f,
0.170962f,0.985278f,
0.14673f,0.989177f,
0.122411f,0.99248f,
0.0980171f,0.995185f,
0.0735646f,0.99729f,
0.0490677f,0.998795f,
0.0245412f,0.999699f,
6.12323e-17f,1.0f,
-0.0245412f,0.999699f,
-0.0490677f,0.998795f,
-0.0735646f,0.99729f,
-0.0980171f,0.995185f,
-0.122411f,0.99248f,
-0.14673f,0.989177f,
-0.170962f,0.985278f,
-0.19509f,0.980785f,
-0.219101f,0.975702f,
-0.24298f,0.970031f,
-0.266713f,0.963776f,
-0.290285f,0.95694f,
-0.313682f,0.949528f,
-0.33689f,0.941544f,
-0.359895f,0.932993f,
-0.382683f,0.92388f,
-0.405241f,0.91421f,
-0.427555f,0.903989f,
-0.449611f,0.893224f,
-0.471397f,0.881921f,
-0.492898f,0.870087f,
-0.514103f,0.857729f,
-0.534998f,0.844854f,
-0.55557f,0.83147f,
-0.575808f,0.817585f,
-0.595699f,0.803208f,
-0.615232f,0.788346f,
-0.634393f,0.77301f,
-0.653173f,0.757209f,
-0.671559f,0.740951f,
-0.689541f,0.724247f,
-0.707107f,0.707107f,
-0.724247f,0.689541f,
-0.740951f,0.671559f,
-0.757209f,0.653173f,
-0.77301f,0.634393f,
-0.788346f,0.615232f,
-0.803208f,0.595699f,
-0.817585f,0.575808f,
-0.83147f,0.55557f,
-0.844854f,0.534998f,
-0.857729f,0.514103f,
-0.870087f,0.492898f,
-0.881921f,0.471397f,
-0.893224f,0.449611f,
-0.903989f,0.427555f,
-0.91421f,0.405241f,
-0.92388f,0.382683f,
-0.932993f,0.359895f,
-0.941544f,0.33689f,
-0.949528f,0.313682f,
-0.95694f,0.290285f,
-0.963776f,0.266713f,
-0.970031f,0.24298f,
-0.975702f,0.219101f,
-0.980785f,0.19509f,
-0.985278f,0.170962f,
-0.989177f,0.14673f,
-0.99248f,0.122411f,
-0.995185f,0.0980171f,
-0.99729f,0.0735646f,
-0.998795f,0.0490677f,
-0.999699f,0.0245412f,
-1.0f,1.22465e-16f,
-0.999699f,-0.0245412,
-0.998795f,-0.0490677,
-0.99729f,-0.0735646f,
-0.995185f,-0.0980171,
-0.99248f,-0.122411f,
-0.989177f,-0.14673f,
-0.985278f,-0.170962f,
-0.980785f,-0.19509f,
-0.975702f,-0.219101f,
-0.970031f,-0.24298f,
-0.963776f,-0.266713f,
-0.95694f,-0.290285f,
-0.949528f,-0.313682f,
-0.941544f,-0.33689f,
-0.932993f,-0.359895f,
-0.92388f,-0.382683f,
-0.91421f,-0.405241f,
-0.903989f,-0.427555f,
-0.893224f,-0.449611f,
-0.881921f,-0.471397f,
-0.870087f,-0.492898f,
-0.857729f,-0.514103f,
-0.844854f,-0.534998f,
-0.83147f,-0.55557f,
-0.817585f,-0.575808f,
-0.803208f,-0.595699f,
-0.788346f,-0.615232f,
-0.77301f,-0.634393f,
-0.757209f,-0.653173f,
-0.740951f,-0.671559f,
-0.724247f,-0.689541f,
-0.707107f,-0.707107f,
-0.689541f,-0.724247f,
-0.671559f,-0.740951f,
-0.653173f,-0.757209f,
-0.634393f,-0.77301f,
-0.615232f,-0.788346f,
-0.595699f,-0.803208f,
-0.575808f,-0.817585f,
-0.55557f,-0.83147f,
-0.534998f,-0.844854f,
-0.514103f,-0.857729f,
-0.492898f,-0.870087f,
-0.471397f,-0.881921f,
-0.449611f,-0.893224f,
-0.427555f,-0.903989f,
-0.405241f,-0.91421f,
-0.382683f,-0.92388f,
-0.359895f,-0.932993f,
-0.33689f,-0.941544f,
-0.313682f,-0.949528f,
-0.290285f,-0.95694f,
-0.266713f,-0.963776f,
-0.24298f,-0.970031f,
-0.219101f,-0.975702f,
-0.19509f,-0.980785f,
-0.170962f,-0.985278f,
-0.14673f,-0.989177f,
-0.122411f,-0.99248f,
-0.0980171f,-0.995185,
-0.0735646f,-0.99729f,
-0.0490677f,-0.998795,
-0.0245412f,-0.999699,
-1.83697e-16f,-1.0f,
0.0245412f,-0.999699f,
0.0490677f,-0.998795f,
0.0735646f,-0.99729f,
0.0980171f,-0.995185f,
0.122411f,-0.99248f,
0.14673f,-0.989177f,
0.170962f,-0.985278f,
0.19509f,-0.980785f,
0.219101f,-0.975702f,
0.24298f,-0.970031f,
0.266713f,-0.963776f,
0.290285f,-0.95694f,
0.313682f,-0.949528f,
0.33689f,-0.941544f,
0.359895f,-0.932993f,
0.382683f,-0.92388f,
0.405241f,-0.91421f,
0.427555f,-0.903989f,
0.449611f,-0.893224f,
0.471397f,-0.881921f,
0.492898f,-0.870087f,
0.514103f,-0.857729f,
0.534998f,-0.844854f,
0.55557f,-0.83147f,
0.575808f,-0.817585f,
0.595699f,-0.803208f,
0.615232f,-0.788346f,
0.634393f,-0.77301f,
0.653173f,-0.757209f,
0.671559f,-0.740951f,
0.689541f,-0.724247f,
0.707107f,-0.707107f,
0.724247f,-0.689541f,
0.740951f,-0.671559f,
0.757209f,-0.653173f,
0.77301f,-0.634393f,
0.788346f,-0.615232f,
0.803208f,-0.595699f,
0.817585f,-0.575808f,
0.83147f,-0.55557f,
0.844854f,-0.534998f,
0.857729f,-0.514103f,
0.870087f,-0.492898f,
0.881921f,-0.471397f,
0.893224f,-0.449611f,
0.903989f,-0.427555f,
0.91421f,-0.405241f,
0.92388f,-0.382683f,
0.932993f,-0.359895f,
0.941544f,-0.33689f,
0.949528f,-0.313682f,
0.95694f,-0.290285f,
0.963776f,-0.266713f,
0.970031f,-0.24298f,
0.975702f,-0.219101f,
0.980785f,-0.19509f,
0.985278f,-0.170962f,
0.989177f,-0.14673f,
0.99248f,-0.122411f,
0.995185f,-0.0980171f,
0.99729f,-0.0735646f,
0.998795f,-0.0490677f,
0.999699f,-0.0245412f
调用arm_cfft_radix4_f32.c文件内的void arm_radix4_butterfly_f32(float * pSrc,uint16_t fftLen,const float * pCoef,uint16_t twidCoefModifier);函数,在这个函数中,*pSrc指向原始数据,fftLen为FFT点数,twiddleCoef为正余弦表,最后一个形参赋值为1即可。
进行完毕FFT转换后,结果为倒序,再通过Rader arithmetic 转换,算法原型如下(忘记出处了。。。):
int array[8]={0,1,2,3,4,5,6,7};
int i,j,k;
int N = 8;
int temp;
j = 0;
for(i = 0; i < N -1; i ++)
{
if(i < j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
k = N >> 1;
while( k <= j)
{
j = j - k;
k >>= 1;
}
j = j + k;
}
for( i = 0; i < N; i ++)
printf("%d ",array[i]);
printf("\n");
return 0;
进行到这里,FFT运算已经完成了,接下来对这个结果在Matlab中进行验证,得出误差在0.00001,符合精度要求。如果进行512点,1024点,2048点FFT,则需要将表进行更换再计算。由于本次只进行了256点的FFT,所以,其他的正余弦表不再贴出。DSP库也没有上传,如果有需要的,可以联系我哈。