eegfilter.~cpp

来自「脑电信号分析软件」· ~CPP 代码 · 共 203 行

~CPP
203
字号
//---------------------------------------------------------------------------


#pragma hdrstop

#include "EEGFilter.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
 //#include "EEGFilter.h"
//#include "GlobalVar.h"

EEGFilter::EEGFilter(int fs, int leadCount)       //EEGFilter 构造函数
	: IIRFilter(fs, leadCount)
{
    /*int count = SysConfig->TimeConstArray->Count->Value;

    //这里加上关闭的选项了。从索引一开始。0没有。不会有ValueArray[0]。为避免读
    //默认值出来,这里不设0
	//for(int i = 0; i < count; i++)
    for(int i = 1; i <= count ; i++)
	{
        AnsiString value = FloatToStrF(SysConfig->TimeConstArray->TimeConst->ValueArray[i],
                                    ffGeneral, 5, 4);
        timeConstantMap[value] = SysConfig->TimeConstArray->TimeConstStop->ValueArray[i];
	}

    count = SysConfig->LowPassArray->Count->Value;
	for(int i = 1; i <= count; i++)
	{
        AnsiString value = FloatToStrF(SysConfig->LowPassArray->LowPass->ValueArray[i],
                                    ffGeneral, 5, 4);
        lowPassMap[value] = SysConfig->LowPassArray->LowPassStop->ValueArray[i];
	}*/
}

EEGFilter::~EEGFilter()
{
}

/*!----------------------------------------------------------------------------
 * \brief  
 *     设置一个脑电方式的通带滤波器
 *
 * \param timeConst 时间常数
 * \param heightFre 低通上限频率
 * ----------------------------------------------------------------------------*/
/*void EEGFilter::SetBandPass(double timeConst, double heightFre)
{
	double heightStop = 0;
	double lowStop = 0;

	this->timeConst = timeConst;
	this->heightFre = heightFre;

	//CheckParameter();

	double lowFre = TimeConToFre(timeConst);
	lowStop = GetLowStop(timeConst);
	heightStop = GetHeightStop(heightFre);
	
	//user default aStop, aPass;
	AddLowPass(Filter::CHEBY_TWO, heightFre, heightStop);
	AddHeightPass(Filter::CHEBY_TWO, lowStop, lowFre);
}*/

/*!----------------------------------------------------------------------------
 * \brief  
 *     设置时间常数
 *
 * \param timeConst 时间常数
 *
 * ----------------------------------------------------------------------------*/
int EEGFilter::SetTimeConstant(double timeConst)
{
	this->timeConst = timeConst;
	double lowFre = TimeConToFre(timeConst);
	double lowStop = GetLowStop(timeConst);

	RemoveHeightPass();
	AddHeightPass(Filter::CHEBY_TWO, lowStop, lowFre);
	
	return 1;
}

/*!----------------------------------------------------------------------------
 * \brief  
 *     设置低通上限频率
 *
 * \param heightFre 低通上限频率
 *
 * ----------------------------------------------------------------------------*/
int EEGFilter::SetHightFreq(double heightFre)
{
	this->heightFre = heightFre;

	double heightStop = GetHeightStop(heightFre);
    RemoveLowPass();
	AddLowPass(Filter::CHEBY_TWO, heightFre, heightStop);

	return 1;
}

/*!----------------------------------------------------------------------------
 * \brief  
 *     时间常数转频率
 *
 * \param timeConst 时间常数
 * \return 返回频率
 *
 * ----------------------------------------------------------------------------*/
double EEGFilter::TimeConToFre(double timeConst)
{
	double fre = 0;
	fre = 1.0/(2*PI*timeConst);
	return fre;
}

/*!----------------------------------------------------------------------------
 * \brief 
 *     根据上限频率通过对照表获取止带频率
 *
 * \param heightFre 低通上限频率
 *
 * \return 上阻带下限
 *
 * ----------------------------------------------------------------------------*/ 
double EEGFilter::GetHeightStop(double heightFre)
{
    double re = 0;
	if(heightFre == 8)
	{
		re = 12;
	}
	if(heightFre == 15)
	{
		re = 19;
	}
    if(heightFre == 28)
    {
        re = 33;
    }
    if(heightFre == 30)
    {
        re = 35;
    }
    return re;
    //AnsiString value = FloatToStrF(heightFre, ffGeneral, 5, 4);
    //return lowPassMap[value];
}

/*!----------------------------------------------------------------------------
 * \brief
 *     根据时间常数获取阻带频率
 *
 * \param timeConst 时间常数
 *
 * \return 下阻带上限
 * ----------------------------------------------------------------------------*/
double EEGFilter::GetLowStop(double timeConst)
{
	//int timeTemp = timeConst * 10;
    double re = 0;
	if(timeConst > 2.9 && timeConst < 3.1)
	{
		re = 0.008;
        }
	if(timeConst > 0.9 && timeConst < 1.1)
	{
		re = 0.008;
	}
    return re;
    //AnsiString value = FloatToStrF(timeConst, ffGeneral, 5, 4);
    //return timeConstantMap[value];
}

/*!----------------------------------------------------------------------------
 * \brief
 *     检查输入的参数
 *
 * ----------------------------------------------------------------------------*/
void EEGFilter::CheckParameter()
{
	double lowFre = TimeConToFre(timeConst);

	if (fs<0)
		throw std::logic_error("采样率fs必须是正数");
//	else if (Asa<=0||Asb<=0||Ap<=0)
		//throw std::logic_error("衰减分贝数要为正");
	else if (lowFre<=0||heightFre<=0)
		throw std::logic_error("频率指标要为正");
	else if (lowFre>=fs/2||heightFre>=fs/2)
		throw std::logic_error("频率指标要小于奈奎斯特频率fs/2,即采样率的一半");
	else if (!(lowFre<heightFre))
		throw std::logic_error("必须符合lowFre<heightFre");
	else if (heightFre-lowFre<1)
		throw std::logic_error("通频带带宽最小为1Hz,请加大通频带范围");
	//else if (Asb/(fsb-heightFre)>15)
	//	throw std::logic_error("不能设计出符合要求的滤波器,请加大过渡带heightFre~fsb的距离或减小Asb");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?