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.

100 lines
1.9 KiB

  1. #include "adaptive_algorithm.h"
  2. float CalculateMean(float value)
  3. {
  4. value /= 1000.0f;
  5. if(mean_count < MEAN_SIZE){
  6. mean_sum += value;
  7. ++mean_count;
  8. }
  9. else{
  10. mean = mean_sum/MEAN_SIZE;
  11. mean_count = 0;
  12. mean_sum = 0;
  13. }
  14. return (mean * 1000.0f);
  15. }
  16. float CalculateRootMeanSquare(float value)
  17. {
  18. value /= 1000.0f;
  19. if(rms_count < RMS_SIZE){
  20. rms_sum += value * value;
  21. ++rms_count;
  22. }
  23. else{
  24. rms = sqrt(rms_sum/RMS_SIZE);
  25. rms_count = 0;
  26. rms_sum = 0;
  27. }
  28. return (rms * 1000.0f);
  29. }
  30. float CalculateCoefficientOfVariation(float value)
  31. {
  32. value /= 1000.0f;
  33. if(cv_count < CV_SIZE){
  34. sd += (value - mean) * (value - mean);
  35. ++cv_count;
  36. }
  37. else{
  38. sd = sqrt(sd / (CV_SIZE-1));
  39. cv = (sd / mean) * 100;
  40. cv_count = 0;
  41. sd = 0;
  42. }
  43. return cv;
  44. }
  45. void InitPeakDetect(float value,bool emi_first)
  46. {
  47. if(!init_flag){
  48. current_max = value;
  49. current_min = value;
  50. is_detecting_emi = emi_first;
  51. init_flag = true;
  52. }
  53. }
  54. SignalPoint PeakDetect(float value,int index,float gradient,bool *is_peak)
  55. {
  56. if(value > current_max){
  57. max_point = index;
  58. current_max = value;
  59. }
  60. if(value < current_min){
  61. min_point = index;
  62. current_min = value;
  63. }
  64. if(is_detecting_emi && value < (current_max - gradient) ){
  65. is_detecting_emi = false;
  66. current_min = current_max;
  67. min_point = max_point;
  68. *is_peak = true;
  69. peak.value = current_max;
  70. peak.index = max_point;
  71. return peak;
  72. }
  73. else if((!is_detecting_emi) && value > (current_min + gradient))
  74. {
  75. is_detecting_emi = true;
  76. current_max = current_min;
  77. max_point = min_point;
  78. *is_peak = false;
  79. peak.value = current_min;
  80. peak.index = min_point;
  81. return peak;
  82. }
  83. peak.index = -1;
  84. return peak;
  85. }