采用巴特沃斯滤波器对脉搏数据滤波,系数是用matlab算出来的,滤波器阶次为4,为何算出来的结果和matlab调用filter函数差距这么大。
#include "StdAfx.h" #include "BWFilter.h" CBWFilter::CBWFilter() { m_pNum = NULL; m_pDen = NULL; m_px = NULL; m_py = NULL; m_num_order = 4; m_den_order = 4; } vector<float>CBWFilter::SmoothFilter(int fs, vector<float> &RawData) { vector<float> vtData(RawData.begin(),RawData.end()); vector<float> vtSmoothed(RawData.size(),0); //采样频率选择 if (fs == 1000) { //分子系数 b[0]=0.0552; b[1]=0.2209; b[2]=0.3313; b[3]=0.2209; b[4]=0.0552; //分母系数 a[0]=1.0f; a[1]=-3.7385; a[2]=5.2492; a[3]=-3.2803; a[4]=0.7698; } delete[] m_pNum; delete[] m_pDen; delete[] m_px; delete[] m_py; m_pNum = new double[m_num_order + 1]; m_pDen = new double[m_den_order + 1]; m_px = new double[m_num_order + 1]; m_py = new double[m_den_order + 1]; for(int i = 0; i <= m_num_order; i++) { m_px[i] = vtData[0]; } for(int i = 0; i <= m_den_order; i++) { m_py[i] =vtData[0]; } int j, k; for(vector<float>::size_type k=4; k!=vtData.size();k++) { m_px[0] = vtData[k]; m_py[0] = 0.0f; for(j = 0; j <= m_num_order; j++) { m_py[0] = m_py[0] + b[j] * m_px[j]; } for(j = 1; j <= m_den_order; j++) { m_py[0] = m_py[0] - a[j] * m_py[j]; } for(j = m_num_order; j >= 1; j--) { m_px[j] = m_px[j-1]; } for(j = m_den_order; j >= 1; j--) { m_py[j] = m_py[j-1]; } vtSmoothed[k]=m_py[0]; } return vtSmoothed; }
解决方案
20
去仔细看 irr 的公式,再和你的程序对比一下。
20
把float类型改为double类型试试,再不行的话,就得检查算法了。