detect_step_by_acc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #include "detect_step_by_acc.h"
  2. static int ACC_WINDOW_SIZE=20;
  3. static float acc_y_buff[20];
  4. static float acc_z_buff[20];
  5. static float acc_x_buff[20];
  6. static float acc_xyz_buff[20];
  7. //static int acc_y_wait;
  8. //static int acc_z_wait;
  9. //static int acc_x_wait;
  10. static int acc_y_status = 1;
  11. static int acc_z_status = 1;
  12. static int acc_x_status = 1;
  13. //static int last_zupt;
  14. static int last_acc_x_status;
  15. static int last_acc_y_status;
  16. static int last_acc_z_status;
  17. static int x_raised;
  18. static int y_raised;
  19. static int z_raised;
  20. static int x_hollow;
  21. static int y_hollow;
  22. static int z_hollow;
  23. static int zero_x_wait;
  24. static int zero_y_wait;
  25. static int zero_z_wait;
  26. //static int can_detect_zupt = 1;
  27. //static int stop_out_zupt = 0;
  28. //static int acc_diff_wait_count = 0;
  29. static int last_time_stamp = 0;
  30. //static int PDR_step_count = 0;
  31. static float max_z_unzero = 1.0f;
  32. static float min_z_unzero = 1.0f;
  33. void detect_raised_hollow(float *acc, int length, float max_val_thresh, float min_val_thresh, float distance, int *raised, int *hollow)
  34. {
  35. //本函数用来检测加速度曲线的凹凸标志
  36. //1、遍历加速度窗口的最大值及最小值
  37. int start_index = ACC_WINDOW_SIZE - length;
  38. if(start_index < 0)
  39. {
  40. return;
  41. }
  42. float max_val = acc[ACC_WINDOW_SIZE - length / 2];
  43. float min_val = acc[ACC_WINDOW_SIZE - length / 2];
  44. for(int i = ACC_WINDOW_SIZE - length / 2; i < ACC_WINDOW_SIZE; i++)
  45. {
  46. if(max_val < acc[i])
  47. {
  48. max_val = acc[i];
  49. }
  50. if(min_val > acc[i])
  51. {
  52. min_val = acc[i];
  53. }
  54. }
  55. //检测到平稳信号,需要重新检测凹凸性
  56. if(max_val - min_val < 0.2f)
  57. {
  58. *raised = 0;
  59. *hollow = 0;
  60. return;
  61. }
  62. for(int i = 0; i < ACC_WINDOW_SIZE - length / 2; i++)
  63. {
  64. if(max_val < acc[i])
  65. {
  66. max_val = acc[i];
  67. }
  68. if(min_val > acc[i])
  69. {
  70. min_val = acc[i];
  71. }
  72. }
  73. //检测凹凸性
  74. if(max_val - acc[0] > distance && max_val - acc[ACC_WINDOW_SIZE-1] > distance && *raised == 0 && max_val > max_val_thresh)
  75. {
  76. *raised = 1;
  77. }
  78. if(acc[0] - min_val > distance && acc[ACC_WINDOW_SIZE - 1] - min_val > distance && *hollow == 0 && min_val < min_val_thresh)
  79. {
  80. *hollow = 1;
  81. }
  82. }
  83. //求取拐点
  84. int detect_turn_line(float *acc, int length, int tag)
  85. {
  86. int start_index = ACC_WINDOW_SIZE - length;
  87. float min_val = acc[start_index];
  88. int min_index = start_index;
  89. float max_val = acc[start_index];
  90. int max_index = start_index;
  91. for(int i = 8; i < ACC_WINDOW_SIZE; i ++)
  92. {
  93. if(min_val > acc[i])
  94. {
  95. min_val = acc[i];
  96. min_index = i;
  97. }
  98. if(max_val < acc[i])
  99. {
  100. max_val = acc[i];
  101. max_index = i;
  102. }
  103. }
  104. float min_val_5 = acc[ACC_WINDOW_SIZE - 5];
  105. float max_val_5 = acc[ACC_WINDOW_SIZE - 5];
  106. for(int i = ACC_WINDOW_SIZE - 5; i < ACC_WINDOW_SIZE; i++ )
  107. {
  108. if(min_val_5 > acc[i])
  109. {
  110. min_val_5 = acc[i];
  111. }
  112. if(max_val_5 < acc[i])
  113. {
  114. max_val_5 = acc[i];
  115. }
  116. }
  117. if(max_val - min_val > 0.5f && max_val_5 - min_val_5 < 0.2f)
  118. {
  119. if(tag == 1)
  120. {
  121. if(max_index < min_index)
  122. {
  123. return 1;
  124. }
  125. else
  126. {
  127. return 0;
  128. }
  129. }
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. //求取平稳信号
  135. int station_signal(float *acc, int length)
  136. {
  137. int start_index = ACC_WINDOW_SIZE - length;
  138. float max_val = acc[start_index];
  139. float min_val = acc[start_index];
  140. for(int i = start_index; i < ACC_WINDOW_SIZE; i++)
  141. {
  142. if(max_val < acc[i])
  143. {
  144. max_val = acc[i];
  145. }
  146. if(min_val > acc[i])
  147. {
  148. min_val = acc[i];
  149. }
  150. }
  151. if(max_val - min_val < 0.15f)
  152. {
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. //剧烈信号探测拐点
  158. int severe_turn_point_z(float *acc, int length)
  159. {
  160. float max_val = acc[0];
  161. float min_val = acc[0];
  162. float min_temp = acc[0];
  163. float max_distance = max_val - min_val;
  164. for(int i = 1 ; i < ACC_WINDOW_SIZE; i++)
  165. {
  166. float distance_tmp = acc[i-1] - acc[i];
  167. if(i > 1 && distance_tmp < (acc[i-2] - acc[i]))
  168. distance_tmp = (acc[i-2] - acc[i]);
  169. if(i > 2 && distance_tmp < (acc[i-3] - acc[i]))
  170. distance_tmp = (acc[i-3] - acc[i]);
  171. if(i > 3 && distance_tmp < (acc[i-4] - acc[i]))
  172. distance_tmp = (acc[i-4] - acc[i]);
  173. if(i > 4 && distance_tmp < (acc[i-5] - acc[i]))
  174. distance_tmp = (acc[i-5] - acc[i]);
  175. if(max_distance < distance_tmp)
  176. {
  177. max_distance = distance_tmp;
  178. }
  179. if(min_temp > acc[i])
  180. {
  181. min_temp = acc[i];
  182. }
  183. }
  184. max_val = acc[ACC_WINDOW_SIZE-5];
  185. min_val = acc[ACC_WINDOW_SIZE-5];
  186. for(int i = ACC_WINDOW_SIZE - 5; i< ACC_WINDOW_SIZE; i++)
  187. {
  188. if(max_val < acc[i])
  189. {
  190. max_val = acc[i];
  191. }
  192. if(min_val > acc[i])
  193. {
  194. min_val = acc[i];
  195. }
  196. }
  197. float min_distance = max_val - min_val;
  198. if(max_distance > 1.5f && min_distance < 0.3f && min_temp > 0.5f)
  199. {
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. int severe_turn_point(float *acc, int length)
  205. {
  206. float max_val = acc[0];
  207. float min_val = acc[0];
  208. float max_distance = max_val - min_val;
  209. for(int i = 1 ; i < ACC_WINDOW_SIZE; i++)
  210. {
  211. float distance_tmp = acc[i] - acc[i-1];
  212. if(i > 1 && distance_tmp < fabsf(acc[i] - acc[i-2]))
  213. distance_tmp = fabsf(acc[i] - acc[i-2]);
  214. if(i > 2 && distance_tmp < fabsf(acc[i] - acc[i-3]))
  215. distance_tmp = fabsf(acc[i] - acc[i-3]);
  216. if(i > 3 && distance_tmp < fabsf(acc[i] - acc[i-4]))
  217. distance_tmp = fabsf(acc[i] - acc[i-4]);
  218. if(i > 4 && distance_tmp < fabsf(acc[i] - acc[i-5]))
  219. distance_tmp = fabsf(acc[i] - acc[i-5]);
  220. if(max_distance < distance_tmp)
  221. {
  222. max_distance = distance_tmp;
  223. }
  224. }
  225. max_val = acc[ACC_WINDOW_SIZE-5];
  226. min_val = acc[ACC_WINDOW_SIZE-5];
  227. for(int i = ACC_WINDOW_SIZE - 5; i< ACC_WINDOW_SIZE; i++)
  228. {
  229. if(max_val < acc[i])
  230. {
  231. max_val = acc[i];
  232. }
  233. if(min_val > acc[i])
  234. {
  235. min_val = acc[i];
  236. }
  237. }
  238. float min_distance = max_val - min_val;
  239. if(max_distance > 1.3f && min_distance < 0.3f)
  240. {
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. float var_acc(float *acc, int length)
  246. {
  247. if(length < 10)
  248. {
  249. return 0.0f;
  250. }
  251. float mean_x = 0.0f;
  252. float sum_x = 0.0f;
  253. for(int i = length - 10; i < length; i++)
  254. {
  255. sum_x += acc[i];
  256. }
  257. mean_x = sum_x * 0.1f;
  258. sum_x = 0.0f;
  259. for(int i = length - 10; i < length; i++)
  260. {
  261. sum_x += ((acc[i]- mean_x) * (acc[i]- mean_x));
  262. }
  263. return sum_x * 0.1f;
  264. }
  265. //寻找窗口的最小值
  266. float min_window(float *acc, int length)
  267. {
  268. int start_index = ACC_WINDOW_SIZE - length;
  269. float min_val = acc[start_index];
  270. for(int i = 0; i < ACC_WINDOW_SIZE; i++)
  271. {
  272. if(acc[i] < min_val)
  273. {
  274. min_val = acc[i];
  275. }
  276. }
  277. return min_val;
  278. }
  279. //寻找窗口的最大值
  280. float max_window(float *acc, int length)
  281. {
  282. int start_index = ACC_WINDOW_SIZE - length;
  283. float max_val = acc[start_index];
  284. for(int i = 0; i < ACC_WINDOW_SIZE; i++)
  285. {
  286. if(acc[i] > max_val)
  287. {
  288. max_val = acc[i];
  289. }
  290. }
  291. return max_val;
  292. }
  293. void detect_step_by_acc(uint32_t time_stamp, float *acc, int *x_zero, int *y_zero, int *z_zero, int *zupt)
  294. {
  295. //缓存加速度的滑动窗口
  296. memcpy(acc_x_buff, acc_x_buff + 1, (ACC_WINDOW_SIZE - 1) * sizeof(float));
  297. acc_x_buff[(ACC_WINDOW_SIZE - 1)] = acc[0];
  298. memcpy(acc_y_buff, acc_y_buff + 1, (ACC_WINDOW_SIZE - 1) * sizeof(float));
  299. acc_y_buff[(ACC_WINDOW_SIZE - 1)] = acc[1];
  300. memcpy(acc_z_buff, acc_z_buff + 1, (ACC_WINDOW_SIZE - 1) * sizeof(float));
  301. acc_z_buff[(ACC_WINDOW_SIZE - 1)] = acc[2];
  302. memcpy(acc_xyz_buff, acc_xyz_buff + 1, (ACC_WINDOW_SIZE - 1) * sizeof(float));
  303. acc_xyz_buff[(ACC_WINDOW_SIZE - 1)] = sqrt(acc[0] * acc[0] + acc[1] * acc[1] + acc[2] * acc[2]);
  304. //检测拐点
  305. //detect_raised_hollow(float *acc, int length, float max_val_thresh, float min_val_thresh, float distance, int *raised, int *hollow)
  306. detect_raised_hollow(acc_x_buff, ACC_WINDOW_SIZE, 0.7f, -0.7f, 0.5f, &x_raised, &x_hollow);
  307. detect_raised_hollow(acc_y_buff, ACC_WINDOW_SIZE, 0.7f, -0.7f, 0.5f, &y_raised, &y_hollow);
  308. detect_raised_hollow(acc_z_buff, ACC_WINDOW_SIZE, 1.2f, 0.8f, 0.4f, &z_raised, &z_hollow);
  309. if(x_raised == 1 && x_hollow == 1 && zero_x_wait == 0)
  310. {
  311. zero_x_wait = 30;
  312. }
  313. if(y_raised == 1 && y_hollow == 1 && zero_y_wait == 0)
  314. {
  315. zero_y_wait = 30;
  316. }
  317. if(z_raised == 1 && z_hollow == 1 && zero_z_wait == 0)
  318. {
  319. zero_z_wait = 30;
  320. }
  321. acc_x_status = 0;
  322. acc_y_status = 0;
  323. acc_z_status = 0;
  324. if((zero_x_wait > 0 || acc_x_status == 1) && detect_turn_line(acc_x_buff, 10, 0))
  325. {
  326. acc_x_status = 1;
  327. }
  328. if((zero_y_wait > 0 || acc_y_status == 1) && detect_turn_line(acc_y_buff, 10, 0))
  329. {
  330. acc_y_status = 1;
  331. }
  332. if((zero_z_wait > 0 || acc_z_status == 1) && detect_turn_line(acc_z_buff, 10, 1))
  333. {
  334. acc_z_status = 1;
  335. }
  336. //剧烈信号探测拐点
  337. if(severe_turn_point(acc_x_buff, ACC_WINDOW_SIZE))
  338. {
  339. acc_x_status = 1;
  340. }
  341. if(severe_turn_point(acc_y_buff, ACC_WINDOW_SIZE))
  342. {
  343. acc_y_status = 1;
  344. }
  345. if(severe_turn_point_z(acc_z_buff, ACC_WINDOW_SIZE))
  346. {
  347. acc_z_status = 4;
  348. }
  349. //标志垫着脚跑的变量
  350. int special_zupt = 0;
  351. if(var_acc(acc_z_buff, ACC_WINDOW_SIZE) > 0.5f && max_window(acc_z_buff, 10) > 1.0f && acc[2] > 0.7f)
  352. {
  353. acc_x_status = 1;
  354. acc_y_status = 1;
  355. acc_z_status = 3;
  356. special_zupt = 1;
  357. }
  358. //当上次信号大于0的时候, 判断下滑曲线
  359. if(last_acc_x_status && station_signal(acc_x_buff, 5))
  360. {
  361. acc_x_status = 2;
  362. }
  363. if(last_acc_y_status && station_signal(acc_y_buff, 5))
  364. {
  365. acc_y_status = 2;
  366. }
  367. if(last_acc_z_status && station_signal(acc_z_buff, 5))
  368. {
  369. acc_z_status = 2;
  370. }
  371. if((acc_x_status || acc_y_status) && fabsf(acc[2] - 1.0f) < 0.2f && station_signal(acc_z_buff, 10))
  372. {
  373. acc_z_status = 2;
  374. }
  375. if(acc_z_status == 0 && last_acc_z_status == 3 && max_window(acc_z_buff, 10) - acc[2] > 1.0f && acc[2] > 0.7f && acc[2] <1.5f)
  376. {
  377. acc_z_status = 3;
  378. }
  379. //因为会有断开的现象,所以大概估计到200ms内出现平缓信号,视为在地上
  380. if(time_stamp - last_time_stamp < 20)
  381. {
  382. if(max_window(acc_xyz_buff, 10) - min_window(acc_xyz_buff, 10) < 0.1f)
  383. {
  384. acc_x_status = 1;
  385. acc_y_status = 1;
  386. acc_z_status = 1;
  387. }
  388. }
  389. //利用奇异值来去掉误判信号
  390. if(special_zupt == 0 && (acc[2] < 0.8f || fabsf(acc[0]) > 0.5f ||fabsf(acc[1]) > 0.5f))
  391. {
  392. acc_x_status = 0;
  393. acc_y_status = 0;
  394. acc_z_status = 0;
  395. }
  396. //凹凸信号都检测信号,需要延时等待
  397. if(zero_x_wait > 0)
  398. {
  399. zero_x_wait --;
  400. if(zero_x_wait == 0)
  401. {
  402. x_raised = 0;
  403. x_hollow = 0;
  404. }
  405. }
  406. if(zero_y_wait > 0)
  407. {
  408. zero_y_wait --;
  409. if(zero_y_wait == 0)
  410. {
  411. y_raised = 0;
  412. y_hollow = 0;
  413. }
  414. }
  415. if(zero_z_wait > 0)
  416. {
  417. zero_z_wait --;
  418. if(zero_z_wait == 0)
  419. {
  420. z_raised = 0;
  421. z_hollow = 0;
  422. }
  423. }
  424. last_acc_x_status = acc_x_status;
  425. last_acc_y_status = acc_y_status;
  426. last_acc_z_status = acc_z_status;
  427. if(time_stamp - last_time_stamp < 15)
  428. {
  429. if(acc[2] > max_z_unzero)
  430. {
  431. max_z_unzero = acc[2];
  432. }
  433. if(acc[2] < min_z_unzero)
  434. {
  435. min_z_unzero = acc[2];
  436. }
  437. }
  438. else
  439. {
  440. max_z_unzero = 1.0f;
  441. min_z_unzero = 1.0f;
  442. }
  443. //记录zupt
  444. if( (acc_z_status > 0 || (acc_x_status > 0 && acc_y_status > 0)) )
  445. {
  446. *zupt = 1;
  447. if(time_stamp - last_time_stamp > 15)
  448. {
  449. //PDR_step_count ++;
  450. mFlash.mStep.stepCur[0]++;
  451. }
  452. else if(min_z_unzero < 0.0f && max_z_unzero > 2.0f)
  453. {
  454. mFlash.mStep.stepCur[0]++;
  455. max_z_unzero = 1.0f;
  456. min_z_unzero = 1.0f;
  457. }
  458. last_time_stamp = time_stamp;
  459. }
  460. else
  461. {
  462. *zupt = 0;
  463. }
  464. *x_zero = 0;
  465. *y_zero = 0;
  466. *z_zero = acc_z_status;
  467. }