本帖最后由 机器人天空 于 2014-10-28 16:03 编辑
unsigned char rhythmcount(unsigned char *fifo)
{
signed char i;
signed char diff[max_data]; //求导数数据区
signed char dif_max=0; //导数最大值
signed char thr_max; //阈值最大值
unsigned char j=0;
signed char k[8]; //最大值数组
unsigned char rhythm;
float DR_R; //R-R间距
*diff=0; //数据区首两个数据导数置0
*(diff+1)=0;
for(i=2;i<max_data-2;i++) //按DIFF(i)=f(i+1)-f(i-1)+2*f(i+2)-2*f(i-2)公式计算导数
{
*(diff+i)=(signed char)(*(fifo+i+1))+2*(signed char)(*(fifo+i+2))-(signed char)(*(fifo+i-1))-2*(signed char)(*(fifo+i-2));
}
*(diff+max_data-1)=0; //数据区末两个数据导数置0
*(diff+max_data)=0;
for(i=0;i<max_data;i++) //求导数最大值
{
if((dif_max)>*(diff+i))
dif_max=*(diff+i);
}
thr_max=(dif_max>>1)-(dif_max>>3); //设定阈值,为导数最大值的0.375倍
for(i=1;i<max_data-1;i++) //将满足f(i)>thr_max和f(i)*f(i+1)<0这两个条件的值找到,即为R波最高点
{
if((*(diff+i)>thr_max)&&(((*(diff+i))^(*(diff+i+1)))>>7))
k[j++]=i;
}
DR_R=(k[j-1]-k[0])/(j-1); //计算R-R间距
rhythm=(unsigned char)(12000/DR_R); //通过公式200*60/DR-R求得心率值
return rhythm;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
/*-------------------------------------------------------------
心率计算模块头文件
--------------------------------------------------------------*/
#ifndef _RHYTHMEHEART_H
#define _RHYTHMEHEART_H
#define max_data 512 //数组最大值,调试时可往下修正
#define default_rhythm 75 //初始心率值
unsigned char rhythmcount(unsigned char *fifo);
#endif
|