心率测试

2017-07-13  本文已影响22人  娘亲Joanna
 ##1.RGB转HSV:转换之后需要h的值来计算心率

void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v ) {
    float min, max, delta;
    min = MIN( r, MIN(g, b ));
    max = MAX( r, MAX(g, b ));
    *v = max;
    delta = max - min;
    if(delta == 0){
        *h= 0;
    }else if((max == r)&&(g>b || g==b)){
        *h = 60 *(g-b)/delta;
    }else if((max == r)&&(g<b)){
     *h = 60 *(g-b)/delta + 360;
    }else if(max == g){
         *h = 60 *(b-r)/delta + 120;
    }else if (max == b){
     *h = 60 *(r-g)/delta + 240;
    }
 
}

2.根据H值返回一个相邻的H的差值的浮点值p:



// 记录浮点变化的前一次的值
static float lastH = 0;
// 用于判断是否是第一个福点值
static int   count = 0;



float HeartRate (float h) {
    float low = 0;
    count++;
    lastH = (count==1)?h:lastH;
    low = (h-lastH);
    lastH = h;

    return low;
}


3.用map形式来保存每个是个返回的浮点值p


例如:{“time”:"2017-5-26 12:30:56","p":"0.5"}

4.根据手指覆盖摄像头闪光灯来通过p计算心率。否则 之前采集的所有点丢掉,重新采集。


  if (r < 0.784) {
//红色值小于0.784 清除所有数据
提示手指移开摄像头和闪光灯
}else{
   if (p >= 1.0f || p<= -1.0f) {
            return;
}
}

5.分析波峰波谷求心率



#pragma mark - 分析瞬时心率

- (void)analysisPointsWith:(NSDictionary *)point {
    [self.points addObject:point];
    if (self.points.count<=30) return;
    int count = (int)self.points.count;
    
    if (self.points.count%10 == 0) {
        int d_i_c = 0;          //最低峰值的位置 姑且算在中间位置 c->center
        int d_i_l = 0;          //最低峰值左面的最低峰值位置 l->left
        int d_i_r = 0;          //最低峰值右面的最低峰值位置 r->right
        
        
        float trough_c = 0;     //最低峰值的浮点值
        float trough_l = 0;     //最低峰值左面的最低峰值浮点值
        float trough_r = 0;     //最低峰值右面的最低峰值浮点值
        
        // 1.先确定数据中的最低峰值
        for (int i = 0; i < count; i++) {
            float trough = [[[self.points[i] allObjects] firstObject] floatValue];
            if (trough < trough_c) {
                trough_c = trough;
                d_i_c = i;
            }
        }
        // 2.找到最低峰值以后  以最低峰值为中心 找到前0.5-1.5周期中的最低峰值  和后0.5-1.5周期的最低峰值
        if (d_i_c >= 1.5*T) {
            
            // a.如果最低峰值处在中心位置, 即距离前后都至少有1.5个周期
            if (d_i_c <= count-1.5*T) {
                // 左面最低峰值
                for (int j = d_i_c - 0.5*T; j > d_i_c - 1.5*T; j--) {
                    float trough = [[[self.points[j] allObjects] firstObject] floatValue];
//                    if ((trough < trough_l)&&(d_i_c-j)<=T) {
                        if (trough < trough_l) {

                        trough_l = trough;
                        d_i_l = j;
                    }
                }
                // 右面最低峰值
                for (int k = d_i_c + 0.5*T; k < d_i_c + 1.5*T; k++) {
                    float trough = [[[self.points[k] allObjects] firstObject] floatValue];
                 //   if ((trough < trough_r)&&(k-d_i_c<=T)) {
                    if (trough < trough_r) {
                        trough_r = trough;
                        d_i_r = k;
                    }
                }
                
            }
            // b.如果最低峰值右面不够1.5个周期 分两种情况 不够0.5个周期和够0.5个周期
            else {
                // b.1 够0.5个周期
                if (d_i_c <count-0.5*T) {
                    // 左面最低峰值
                    for (int j = d_i_c - 0.5*T; j > d_i_c - 1.5*T; j--) {
                        float trough = [[[self.points[j] allObjects] firstObject] floatValue];
                        if ((trough < trough_l)&&(d_i_c-j)<=T) {
                            trough_l = trough;
                            d_i_l = j;
                        }
                    }
                    // 右面最低峰值
                    for (int k = d_i_c + 0.5*T; k < count; k++) {
                        float trough = [[[self.points[k] allObjects] firstObject] floatValue];
                        if ((trough < trough_r)&&(k-d_i_c<=T)) {
                            trough_r = trough;
                            d_i_r = k;
                        }
                    }
                }
                // b.2 不够0.5个周期
                else {
                    // 左面最低峰值
                    for (int j = d_i_c - 0.5*T; j > d_i_c - 1.5*T; j--) {
                        float trough = [[[self.points[j] allObjects] firstObject] floatValue];
                        if ((trough < trough_l)&&(d_i_c-j)<=T) {
                            trough_l = trough;
                            d_i_l = j;
                        }
                    }
                }
            }
            
        }
        // c. 如果左面不够1.5个周期 一样分两种情况  够0.5个周期 不够0.5个周期
        else {
            // c.1 够0.5个周期
            if (d_i_c>0.5*T) {
                // 左面最低峰值
                for (int j = d_i_c - 0.5*T; j > 0; j--) {
                    float trough = [[[self.points[j] allObjects] firstObject] floatValue];
                    if ((trough < trough_l)&&(d_i_c-j)<=T) {
                        trough_l = trough;
                        d_i_l = j;
                    }
                }
                // 右面最低峰值
                for (int k = d_i_c + 0.5*T; k < d_i_c + 1.5*T; k++) {
                    float trough = [[[self.points[k] allObjects] firstObject] floatValue];
                    if ((trough < trough_r)&&(k-d_i_c<=T)) {
                        trough_r = trough;
                        d_i_r = k;
                    }
                }

            }
            // c.2 不够0.5个周期
            else {
                // 右面最低峰值
                for (int k = d_i_c + 0.5*T; k < d_i_c + 1.5*T; k++) {
                    float trough = [[[self.points[k] allObjects] firstObject] floatValue];
                    if ((trough < trough_r)&&(k-d_i_c<=T)) {
                        trough_r = trough;
                        d_i_r = k;
                    }
                }
            }
            
        }
        
        // 3. 确定哪一个与最低峰值更接近 用最接近的一个最低峰值测出瞬时心率 60*1000两个峰值的时间差
        if (trough_l-trough_c < trough_r-trough_c) {
        
            NSDictionary *point_c = self.points[d_i_c];
            NSDictionary *point_l = self.points[d_i_l];
            double t_c = [[[point_c allKeys] firstObject] doubleValue];
            NSLog(@"hhehehhhe:%lf",t_c);
            double t_l = [[[point_l allKeys] firstObject] doubleValue];
            NSInteger fre = (NSInteger)(60*1000)/(t_c - t_l);
            if (self.frequency)
                self.frequency(fre);
            if ([self.delegate respondsToSelector:@selector(startHeartDelegateRateFrequency:)])
                NSLog(@"手指移开  左:   %ld",fre);
                [self.delegate startHeartDelegateRateFrequency:fre];
        } else {
            NSDictionary *point_c = self.points[d_i_c];
            NSDictionary *point_r = self.points[d_i_r];
            double t_c = [[[point_c allKeys] firstObject] doubleValue];
            double t_r = [[[point_r allKeys] firstObject] doubleValue];
            NSInteger fre = (NSInteger)(60*1000)/(t_r - t_c);
            if (self.frequency)
                self.frequency(fre);
            if ([self.delegate respondsToSelector:@selector(startHeartDelegateRateFrequency:)])
                [self.delegate startHeartDelegateRateFrequency:fre];
              NSLog(@"手指移开  右:   %ld",fre);
        }
        // 4.删除过期数据
        for (int i = 0; i< 10; i++) {
            [self.points removeObjectAtIndex:0];
        }
    }
}



上一篇下一篇

猜你喜欢

热点阅读