FIR实现,已验证
class CFilter_FIR
{
public:
CFilter_FIR();
virtual ~CFilter_FIR();
private:
double *m_pB;
double *m_pdata_buf;
int m_nB_order;
int m_nFirIndex;
public:
void reset();
/*/*brief:注意:B数组为nB_order个
*/
*/
void setPara(double B[], int nB_order);
double filter(double data);
void filter(double data[], int len);
void filter(double data_in[], double data_out[], int len);
};
cpp文件
CFilter_FIR::CFilter_FIR()
{
m_pB = NULL;
m_pdata_buf = NULL;
m_nB_order = 0;
m_nFirIndex = 0;
}
CFilter_FIR::~CFilter_FIR()
{
if (m_pdata_buf)
{
delete m_pdata_buf;
m_pdata_buf = NULL;
}
if (m_pB)
{
delete m_pB;
m_pB = NULL;
}
}
/** \brief 将滤波器的内部状态清零
* \return
*/
void CFilter_FIR::reset()
{
for(int i = 0; i < m_nB_order; i++)
{
m_pdata_buf[i] = 0.0;
}
m_nFirIndex = 0;
}
/** \brief
*/
void CFilter_FIR::setPara(double B[], int nB_order)
{
m_nB_order = nB_order;
if (m_pB)
{
delete m_pB;
}
if (m_pdata_buf)
{
delete m_pdata_buf;
}
m_pB = new double[nB_order ];
m_pdata_buf = new double[nB_order ];
for(int i = 0; i < nB_order; i++)
{
m_pB[i] = B[i];
m_pdata_buf[i] = 0.0;
}
}
/** \brief 滤波函数
*
* \param data 传入输入数据
* \return 滤波后的结果
*/
double CFilter_FIR::filter(double data)
{
int k;
double fOut = 0.0;
int i = 0;
m_pdata_buf[m_nFirIndex%m_nB_order] = data;
fOut = 0.0f;
for (i = 0; i < m_nB_order; ++i)
{
fOut += m_pdata_buf[(m_nFirIndex +i+1)%m_nB_order]*m_pB[i];
}
m_nFirIndex++;
return fOut;
}
/** \brief 滤波函数
*
* \param data[] 传入输入数据,返回时给出滤波后的结果
* \param len data[] 数组的长度
* \return
*/
void CFilter_FIR::filter(double data[], int len)
{
int k;
double fOut = 0.0;
for(k = 0; k < len; k++)
{
int i = 0;
m_pdata_buf[m_nFirIndex%m_nB_order] = data[k];
fOut = 0.0f;
for (i = 0; i < m_nB_order; ++i)
{
fOut += m_pdata_buf[(m_nFirIndex +i+1)%m_nB_order]*m_pB[i];
}
m_nFirIndex++;
data[k] = fOut;
}
}
/** \brief 滤波函数
*
* \param data_in[] 输入数据
* \param data_out[] 保存滤波后的数据
* \param len 数组的长度
* \return
*/
void CFilter_FIR::filter(double data_in[], double data_out[], int len)
{
int k;
for(k = 0; k < len; k++)
{
int i = 0;
m_pdata_buf[m_nFirIndex%m_nB_order] = data_in[k];
data_out[k] = 0.0f;
for (i = 0; i < m_nB_order; ++i)
{
data_out[k] += m_pdata_buf[(m_nFirIndex +i+1)%m_nB_order]*m_pB[i];
}
m_nFirIndex++;
}
}
IIR实现,验证过的
class CFilter_IIR_I
{
public:
CFilter_IIR_I();
virtual ~CFilter_IIR_I();
private:
double *m_pNum;
double *m_pDen;
double *m_px;
double *m_py;
int m_num_order;
int m_den_order;
public:
void reset();
/*brief:注意:num数组为num_order个,den数组为den_order个
*/
void setPara(double num[], int num_order, double den[], int den_order);
void resp(double data_in[], int m, double data_out[], int n);
double filter(double data);
void filter(double data[], int len);
void filter(double data_in[], double data_out[], int len);
};
cpp文件
CFilter_IIR_I::CFilter_IIR_I()
{
m_pNum = NULL;
m_pDen = NULL;
m_px = NULL;
m_py = NULL;
m_num_order = -1;
m_den_order = -1;
}
CFilter_IIR_I::~CFilter_IIR_I()
{
}
/** \brief 将滤波器的内部状态清零,滤波器的系数保留
* \return
*/
void CFilter_IIR_I::reset()
{
for(int i = 0; i < m_num_order; i++)
{
m_pNum[i] = 0.0;
}
for(i = 0; i < m_den_order; i++)
{
m_pDen[i] = 0.0;
}
}
/** \brief
*
* \param num 分子多项式的系数,升序排列,num[0] 为常数项
* \param m 分子多项式的阶数
* \param den 分母多项式的系数,升序排列,den[0] 为常数项
* \param m 分母多项式的阶数
* \return
*/
void CFilter_IIR_I::setPara(double num[], int num_order, double den[], int den_order)
{
delete[] m_pNum;
delete[] m_pDen;
delete[] m_px;
delete[] m_py;
m_pNum = new double[num_order ];
m_pDen = new double[den_order ];
m_num_order = num_order;
m_den_order = den_order;
m_px = new double[num_order ];
m_py = new double[den_order ];
for(int i = 0; i < m_num_order; i++)
{
m_pNum[i] = num[i];
m_px[i] = 0.0;
}
for(i = 0; i < m_den_order; i++)
{
m_pDen[i] = den[i];
m_py[i] = 0.0;
}
}
/** \brief 滤波函数,采用直接I型结构
*
* \param data 传入输入数据
* \return 滤波后的结果
*/
double CFilter_IIR_I::filter(double data)
{
m_py[0] = 0.0; // ????????
m_px[0] = data;
for(int i = 0; i < m_num_order; i++)
{
m_py[0] = m_py[0] + m_pNum[i] * m_px[i];
}
for(i = 1; i < m_den_order; i++)
{
m_py[0] = m_py[0] - m_pDen[i] * m_py[i];
}
for(i = m_num_order-1; i >= 1; i--)
{
m_px[i] = m_px[i-1];
}
for(i = m_den_order-1; i >= 1; i--)
{
m_py[i] = m_py[i-1];
}
return m_py[0];
}
/** \brief 滤波函数,采用直接I型结构
*
* \param data[] 传入输入数据,返回时给出滤波后的结果
* \param len data[] 数组的长度
* \return
*/
void CFilter_IIR_I::filter(double data[], int len)
{
int i, k;
for(k = 0; k < len; k++)
{
m_px[0] = data[k];
data[k] = 0.0;
for(i = 0; i < m_num_order; i++)
{
data[k] = data[k] + m_pNum[i] * m_px[i];
}
for(i = 1; i < m_den_order; i++)
{
data[k] = data[k] - m_pDen[i] * m_py[i];
}
// we get the y value now
m_py[0] = data[k];
for(i = m_num_order-1; i >= 1; i--)
{
m_px[i] = m_px[i-1];
}
for(i = m_den_order-1; i >= 1; i--)
{
m_py[i] = m_py[i-1];
}
}
}
/** \brief 滤波函数,采用直接I型结构
*
* \param data_in[] 输入数据
* \param data_out[] 保存滤波后的数据
* \param len 数组的长度
* \return
*/
void CFilter_IIR_I::filter(double data_in[], double data_out[], int len)
{
int i, k;
for(k = 0; k < len; k++)
{
m_px[0] = data_in[k];
m_py[0] = 0.0;
for(i = 0; i < m_num_order; i++)
{
m_py[0] += m_pNum[i] * m_px[i];
}
for(i = 1; i < m_den_order; i++)
{
m_py[0] -= m_pDen[i] * m_py[i];
}
for(i = m_num_order-1; i >= 1; i--)
{
m_px[i] = m_px[i-1];
}
for(i = m_den_order-1; i >= 1; i--)
{
m_py[i] = m_py[i-1];
}
data_out[k] = m_py[0];
}
}
/** \brief 计算 IIR 滤波器的时域响应,不影响滤波器的内部状态
* \param data_in 为滤波器的输入,0 时刻之前的输入默认为 0,data_in[M] 及之后的输入默认为data_in[M-1]
* \param data_out 滤波器的输出
* \param M 输入数据的长度
* \param N 输出数据的长度
* \return
*/
void CFilter_IIR_I::resp(double data_in[], int M, double data_out[], int N)
{
int i, k, il;
for(k = 0; k < N; k++)
{
data_out[k] = 0.0;
for(i = 0; i < m_num_order; i++)
{
if( k - i >= 0)
{
il = ((k - i) < M) ? (k - i) : (M - 1);
data_out[k] = data_out[k] + m_pNum[i] * data_in[il];
}
}
for(i = 1; i < m_den_order; i++)
{
if( k - i >= 0)
{
data_out[k] = data_out[k] - m_pDen[i] * data_out[k - i];
}
}
}
}
IIR参考/liyuanbhu/article/details/38849897#comments