原理稍后上传,以下是个人对这算法进行简单的C语言实现:
#include <math.h>
#include <stdio.h>
#define M_PI 3.1415
/**
* @brief 求正弦信号的幅值
*
* @param data 采集正弦信号点的值
* @param length 采样个数
* @param wave N次谐波
* @return unsigned int
*/
unsigned int DFT_Compute_Amplitude(unsigned short *data,uint16_t length,uint8_t wave)
{
double real = 0;
double imag = 0;
for (int n = 0; n < length; n++)
{
double angle = 2 * M_PI * wave * n / length;
real += data[n] * cos(angle);
imag += -data[n] * sin(angle);
}
return (unsigned int)sqrt(real * real + imag * imag);
}