You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.9 KiB

  1. #include "HC_Chen_detect.h"
  2. bool HC_Chen_detect(float signal)
  3. {
  4. ecg_buff[ecg_buff_WR_idx++] = signal;
  5. sample = ecg_buff_WR_idx;
  6. ecg_buff_WR_idx %= (M+1);
  7. /* High pass filtering */
  8. if(number_iter < M){
  9. // first fill buffer with enough points for HP filter
  10. hp_sum += ecg_buff[ecg_buff_RD_idx];
  11. hp_buff[hp_buff_WR_idx] = 0;
  12. }
  13. else{
  14. hp_sum += ecg_buff[ecg_buff_RD_idx];
  15. int tmp = ecg_buff_RD_idx - M;
  16. if(tmp < 0){
  17. tmp += M + 1;
  18. }
  19. hp_sum -= ecg_buff[tmp];
  20. float y1 = 0;
  21. float y2 = 0;
  22. tmp = (ecg_buff_RD_idx - ((M+1)/2));
  23. if(tmp < 0){
  24. tmp += M + 1;
  25. }
  26. y2 = ecg_buff[tmp];
  27. y1 = HP_CONSTANT * hp_sum;
  28. hp_buff[hp_buff_WR_idx] = y2 - y1;
  29. }
  30. // done reading ECG buffer, increment position
  31. ecg_buff_RD_idx++;
  32. ecg_buff_RD_idx %= (M+1);
  33. // done writing to HP buffer, increment position
  34. hp_buff_WR_idx++;
  35. hp_buff_WR_idx %= (N+1);
  36. /* Low pass filtering */
  37. // shift in new sample from high pass filter
  38. lp_sum += hp_buff[hp_buff_RD_idx] * hp_buff[hp_buff_RD_idx];
  39. if(number_iter < N){
  40. // first fill buffer with enough points for LP filter
  41. next_eval_pt = 0;
  42. }
  43. else{
  44. // shift out oldest data point
  45. int tmp = hp_buff_RD_idx - N;
  46. if(tmp < 0){
  47. tmp += N+1;
  48. }
  49. lp_sum -= hp_buff[tmp] * hp_buff[tmp];
  50. next_eval_pt = lp_sum;
  51. }
  52. // done reading HP buffer, increment position
  53. hp_buff_RD_idx++;
  54. hp_buff_RD_idx %= (N+1);
  55. /* Adapative thresholding beat detection */
  56. // set initial threshold
  57. if(number_iter < window_size) {
  58. if(next_eval_pt > treshold) {
  59. treshold = next_eval_pt;
  60. }
  61. ++number_iter;
  62. }
  63. // check if detection hold off period has passed
  64. if(triggered){
  65. trig_time++;
  66. if(trig_time >= DELAY_TIME){
  67. triggered = false;
  68. trig_time = 0;
  69. }
  70. }
  71. // find if we have a new max
  72. if(next_eval_pt > win_max) win_max = next_eval_pt;
  73. // find if we are above adaptive threshold
  74. if(next_eval_pt > treshold && !triggered) {
  75. //result.push_back(true);
  76. last_qrs_point = sample;
  77. triggered = true;
  78. return true;
  79. }
  80. else {
  81. //result.push_back(false);
  82. }
  83. // adjust adaptive threshold using max of signal found
  84. // in previous window
  85. if(win_idx++ >= window_size){
  86. // weighting factor for determining the contribution of
  87. // the current peak value to the threshold adjustment
  88. float gamma = (0.2f+0.15f)/2.0f; // 0.15~0.2
  89. // forgetting factor -
  90. // rate at which we forget old observations
  91. float alpha = 0.01f + ( ((float) rand() / (float) RAND_MAX) * ((0.1f - 0.01f))); // 0~1
  92. //float alpha = 1.0f*exp(-0.00005f*(sample - last_qrs_point));
  93. treshold = alpha * gamma * win_max + (1.0f - alpha) * treshold;
  94. // reset current window ind
  95. win_idx = 0;
  96. win_max = -10000000;
  97. }
  98. return false;
  99. }