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.

208 lines
5.7 KiB

1 year ago
  1. #pragma once
  2. #include <fstream>
  3. #include <functional>
  4. #include <iostream>
  5. #include <list>
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <sstream>
  10. #include <string>
  11. #include <vector>
  12. namespace opt_algo {
  13. using namespace std;
  14. typedef enum {
  15. k250,
  16. k1000,
  17. } ProcessPointNumType_t;
  18. typedef enum {
  19. k_ecode_ok = 0,
  20. k_ecode_can_not_find_peak = 1,
  21. k_ecode_can_not_find_peak_start = 2,
  22. k_ecode_can_not_find_peak_end = 3,
  23. } Error_t;
  24. class LineContext {
  25. public:
  26. vector<float> raw; //
  27. vector<float> avg; //
  28. vector<float> diff; //
  29. float agvline; //
  30. float baseline_slope; //
  31. float baseline_intercept; //
  32. vector<float> raw250; //
  33. vector<float> avg250; //
  34. vector<float> diff250; //
  35. };
  36. class PeakInfo {
  37. public:
  38. bool find_peak;
  39. float peak_full_area;
  40. float peak_base_line_area;
  41. float area;
  42. int peak_pos;
  43. int peak_start_pos;
  44. int peak_end_pos;
  45. };
  46. class AlgoResult {
  47. public:
  48. /**
  49. * @brief
  50. * :
  51. * 1) 1200
  52. * 2) 1200线6000
  53. * 3) 6000
  54. * 4) 60001000
  55. *
  56. * 5) 1000 13
  57. * 6) 1000 20
  58. *
  59. * 7)
  60. * 8
  61. * 9)
  62. *
  63. */
  64. AlgoResult() {
  65. error_code = k_ecode_ok;
  66. lineContext = make_shared<LineContext>();
  67. for (int i = 0; i < 5; i++) {
  68. peakInfo[i] = make_shared<PeakInfo>();
  69. }
  70. }
  71. vector<float> ogigin_val; // 1200
  72. vector<float> supper_val; // 原始数据,线性填充,1200*5=6000
  73. vector<float> supper_median_val; // supper_val 窗口平滑滤波,6000
  74. vector<float> supper_smooth_sub_val; // supper_smooth_val 均值压缩,6000/6=1000
  75. shared_ptr<LineContext> lineContext; // supper_smooth_sub_val 13点滑动均值滤波,1000
  76. Error_t error_code; // 错误码
  77. // result
  78. shared_ptr<PeakInfo> peakInfo[5];
  79. int peakNum;
  80. };
  81. class OptAlgo {
  82. public:
  83. /**
  84. * @brief
  85. *
  86. * @param context
  87. * @param pconfig
  88. * @param ogigin_val expect 1200
  89. */
  90. static shared_ptr<AlgoResult> calculate(vector<float> ogigin_val, int peaknum);
  91. static int calculate_peak_num(vector<float>& ogigin_val);
  92. static int getAlgoVersion();
  93. private:
  94. static Error_t findpeak(shared_ptr<LineContext> lineContext, int32_t search_start, int32_t peakwindth, shared_ptr<PeakInfo> retpeak);
  95. static int sub_find_peak(shared_ptr<LineContext> lineContext, int start_off, int windos_size, int judge_win_size);
  96. static int find_peak_endpoint(shared_ptr<LineContext> lineContext, int peakpos, int search_direction, int search_windows);
  97. private:
  98. /*******************************************************************************
  99. * *
  100. *******************************************************************************/
  101. /**
  102. * @brief
  103. *
  104. * @param inputRaw
  105. * @param nInputLength
  106. * @param nUpSampleRate
  107. * @return vector<float>
  108. */
  109. static vector<float> super_sampling(vector<float>& inputRaw, int32_t nInputLength, int32_t nUpSampleRate);
  110. /**
  111. * @brief
  112. *
  113. * @param inputRaw
  114. * @param nSubSampleRate
  115. * @return vector<float>
  116. */
  117. static vector<float> sub_sampling(vector<float>& inputRaw, int nSubSampleRate);
  118. /**
  119. * @brief
  120. *
  121. * @param inputRaw
  122. * @param windows_size
  123. * @return vector<float>
  124. */
  125. static vector<float> smooth_windows(vector<float>& inputRaw, int windows_size);
  126. /**
  127. * @brief
  128. *
  129. * @param inputRaw
  130. * @param windows_size
  131. * @return vector<float>
  132. */
  133. static vector<float> median_filtering(vector<float>& inputRaw, int windows_size);
  134. /**
  135. * @brief 线
  136. *
  137. * @param inputRaw
  138. * @return float
  139. */
  140. static float find_avg_line(vector<float>& inputRaw);
  141. /**
  142. * @brief 线
  143. *
  144. * @param inputRaw
  145. * @return vector<float> 线
  146. */
  147. static vector<float> differentiate(vector<float>& inputRaw);
  148. /**
  149. * @brief
  150. *
  151. * @param inputRaw
  152. * @param windows_size
  153. * @return vector<float> 线
  154. */
  155. static vector<float> least_square_method_differentiate(vector<float>& inputRaw, int windows_size);
  156. /**
  157. * @brief windsize/2windowsize/2
  158. *
  159. * @param val
  160. * @param windows_size
  161. * @return true
  162. * @return false
  163. */
  164. static bool is_maxval_in_windows(float* val, int windows_size);
  165. /**
  166. * @brief
  167. *
  168. * @param a
  169. * @param b
  170. * @param epsilon
  171. * @return true
  172. * @return false
  173. */
  174. static bool feq(float a, float b, float epsilon = 0.00001);
  175. /**
  176. * @brief
  177. *
  178. * @param val
  179. * @param size
  180. * @return float
  181. */
  182. static void linear_least_squares(vector<float>& x, vector<float>& y, float& slope, float& intercept);
  183. static void linear_least_squares(float* y, int size, float& slope, float& intercept);
  184. static void linear_least_squares_muti_windos(float* y, int size, vector<int> startx, int windowssize, float& slope, float& intercept);
  185. static float get_avg_in_windows(vector<float>& src, int off, int windows);
  186. static void sort_vector(vector<float>& src);
  187. static vector<float> getwindowspoint(vector<float>& src, int off, int windows);
  188. };
  189. } // namespace opt_algo