AppFuncLib.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * @Author: chenjie
  3. * @Date: 2022-06-06
  4. * @LastEditTime: 2022-11-10
  5. * @LastEditors: chenjie
  6. * @Description:
  7. * @FilePath: \S32K146_4G\code\app\lib\AppFuncLib.c
  8. * Copyright (c) 2022 by chenjie, All Rights Reserved.
  9. */
  10. #include "AppFuncLib.h"
  11. #include "AppGlobalVar.h"
  12. #include "Hal_Fls.h"
  13. #include "AppTaskUart1.h"
  14. /**
  15. * @brief : 获取故障码函数,从故障数组中获取故障码,并将之前的故障码向前移动
  16. * @param {UINT16} *ErrorArray
  17. * @param {UINT8} Errorlen
  18. * @return {*}
  19. */
  20. static uint8 bcc_chk_fota(uint8 *data, uint8 length);
  21. static uint8 Fota_crc_chk(uint8 *data, uint8 length);
  22. static int fft_cmp(const void*p1,const void*p2)
  23. {
  24. return (*(struct _fft_Freq*)p2).amp>(*(struct _fft_Freq*)p1).amp ? 1 : -1;
  25. }
  26. uint16 GetErrorNum(uint16 *ErrorArray, uint8 Errorlen)
  27. {
  28. uint16 OutNum;
  29. OutNum = *(ErrorArray);
  30. for (uint8 i = 0; i < Errorlen - 1; i++)
  31. {
  32. *(ErrorArray + i) = *(ErrorArray + i + 1);
  33. if (*(ErrorArray + i + 1) == 0)
  34. break;
  35. }
  36. return OutNum;
  37. }
  38. uint8 PutErrorNum(uint16 *ErrorArray, uint8 Errorlen, uint16 ErrorNum)
  39. {
  40. for (uint8 i = 0; i < Errorlen; i++)
  41. {
  42. if (*(ErrorArray + i) == 0)
  43. {
  44. *(ErrorArray + i) = ErrorNum;
  45. return 0;
  46. }
  47. else
  48. {
  49. if (*(ErrorArray + i) == ErrorNum)
  50. {
  51. return 1;
  52. }
  53. else
  54. {
  55. continue;
  56. }
  57. }
  58. }
  59. return 2;
  60. }
  61. uint16 ATstrdel(char *str)
  62. {
  63. char *p = str;
  64. bool flag = false;
  65. while (*str)
  66. {
  67. if (*str > 0x20)
  68. {
  69. *(p) = *str;
  70. p = p + 1;
  71. flag = false;
  72. }
  73. else
  74. {
  75. if (!flag)
  76. {
  77. *(p) = ',';
  78. p = p + 1;
  79. flag = true;
  80. }
  81. }
  82. str++;
  83. }
  84. *p = '\0';
  85. return 0;
  86. }
  87. uint16 mstrlen(const char *s)
  88. {
  89. uint16 out = 0;
  90. const char *ss = s;
  91. while (*ss)
  92. ss++;
  93. out = (ss - s);
  94. return out;
  95. }
  96. int mstrncmp(const char *s1, const char *s2, int n)
  97. {
  98. const unsigned char *c1 = (const unsigned char *)s1;
  99. const unsigned char *c2 = (const unsigned char *)s2;
  100. unsigned char ch;
  101. int d = 0;
  102. while (n--)
  103. {
  104. d = (int)(ch = *c1++) - (int)*c2++;
  105. if (d || !ch)
  106. break;
  107. }
  108. return d;
  109. }
  110. unsigned char HexToChar(unsigned char bHex)
  111. {
  112. if ((bHex >= 0) && (bHex <= 9))
  113. bHex += 0x30;
  114. else if ((bHex >= 10) && (bHex <= 15)) // 大写字母
  115. bHex += 0x37;
  116. else
  117. bHex = 0xff;
  118. return bHex;
  119. }
  120. unsigned char CharToHex(unsigned char bChar)
  121. {
  122. if ((bChar >= 0x30) && (bChar <= 0x39))
  123. bChar -= 0x30;
  124. else if ((bChar >= 0x41) && (bChar <= 0x46)) // 大写字母
  125. bChar -= 0x37;
  126. else if ((bChar >= 0x61) && (bChar <= 0x66)) // 小写字母
  127. bChar -= 0x57;
  128. else
  129. bChar = 0xff;
  130. return bChar;
  131. }
  132. uint8 AtStrCompare(const char *a, const char *b)
  133. {
  134. uint8 out = 1;
  135. while (1)
  136. {
  137. if (*a == '\0' || *b == '\0') // 判断其中是否有字符串结束
  138. {
  139. if (strlen(a) == strlen(b))
  140. {
  141. out = 1;
  142. break;
  143. }
  144. else
  145. {
  146. out = 0;
  147. break;
  148. }
  149. }
  150. else
  151. {
  152. if (*a != *b)
  153. {
  154. out = 0;
  155. break;
  156. }
  157. else if (*a == '=' && *b == '=')
  158. {
  159. out = 1;
  160. break;
  161. }
  162. }
  163. a++;
  164. b++;
  165. }
  166. return out;
  167. }
  168. unsigned short CRC16_Modbus(unsigned char *pdata, int len)
  169. {
  170. unsigned short crc = 0xFFFF;
  171. int i, j;
  172. for (j = 0; j < len; j++)
  173. {
  174. crc = crc ^ pdata[j];
  175. for (i = 0; i < 8; i++)
  176. {
  177. if ((crc & 0x0001) > 0)
  178. {
  179. crc = crc >> 1;
  180. crc = crc ^ 0xa001;
  181. }
  182. else
  183. crc = crc >> 1;
  184. }
  185. }
  186. return crc;
  187. }
  188. char *Myitoa(int value, char *result, int base)
  189. {
  190. // check that the base if valid
  191. if (base < 2 || base > 36)
  192. {
  193. *result = '\0';
  194. return result;
  195. }
  196. char *ptr = result, *ptr1 = result, tmp_char;
  197. int tmp_value;
  198. do
  199. {
  200. tmp_value = value;
  201. value /= base;
  202. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
  203. } while (value);
  204. // Apply negative sign
  205. if (tmp_value < 0)
  206. *ptr++ = '-';
  207. *ptr-- = '\0';
  208. while (ptr1 < ptr)
  209. {
  210. tmp_char = *ptr;
  211. *ptr-- = *ptr1;
  212. *ptr1++ = tmp_char;
  213. }
  214. return result;
  215. }
  216. /************************************************************************
  217. * @brief 整数转字符串
  218. * @param[in] num 整数
  219. * @param[out] buf 字符串
  220. * @return 返回字符串长度
  221. ************************************************************************/
  222. inline int _itoa(int num, char buf[32])
  223. {
  224. return _i2a(num, buf, 10);
  225. }
  226. /************************************************************************
  227. * @brief 整数转字符串
  228. * @param[in] num 整数
  229. * @param[out] buf 字符串
  230. * @param[in] radix 进位制整数
  231. * @return 返回字符串长度
  232. ************************************************************************/
  233. int _i2a(int num, char buf[32], int radix)
  234. {
  235. static const char s[] = "0123456789abcdef";
  236. int n = num, R = radix;
  237. char *dst = buf;
  238. if (n < 0)
  239. {
  240. *dst++ = '-';
  241. n = -n;
  242. }
  243. if (n < 10)
  244. {
  245. *dst++ = s[n];
  246. *dst = 0;
  247. }
  248. else
  249. {
  250. char tmp[32], *p = tmp;
  251. while (n)
  252. {
  253. *p++ = s[n % R];
  254. n /= R;
  255. }
  256. while (--p != tmp)
  257. *dst++ = *p;
  258. *dst++ = *tmp;
  259. *dst = 0;
  260. }
  261. return dst - buf;
  262. }
  263. /************************************************************************
  264. * @brief 浮点数转字符串
  265. * @param[in] val 浮点数
  266. * @param[out] buf 字符串
  267. * @param[in] eps 精度(小数位)
  268. * @return 返回字符串长度
  269. ************************************************************************/
  270. int _ftoa(double val, char buf[32], int eps)
  271. {
  272. double f = val;
  273. char *p = buf;
  274. if (val < 0)
  275. {
  276. *p++ = '-';
  277. f = -f;
  278. }
  279. int n = f;
  280. int len = _itoa(n, p);
  281. return len + __ftoa(f - n, p + len, eps);
  282. }
  283. /************************************************************************
  284. * @brief 浮点数转字符串:范围(-1, 1)
  285. * @param[in] val 浮点数
  286. * @param[out] buf 字符串
  287. * @param[in] eps 精度(小数位)
  288. * @return 返回字符串长度
  289. ************************************************************************/
  290. int __ftoa(double val, char buf[32], int eps)
  291. {
  292. double f = val;
  293. char *p = buf;
  294. static const char s[] = "0123456789";
  295. if (f < 0)
  296. {
  297. *p++ = '-';
  298. f = -f;
  299. }
  300. *p++ = '.';
  301. for (int i = eps + 1, n; --i; ++p, f -= n)
  302. *p = s[n = f *= 10.0];
  303. *p = 0;
  304. return p - buf;
  305. }
  306. /************************************************************************
  307. * @brief 替换sprintf
  308. * @ref 可变长参数列表误区与陷阱——va_arg不可接受的类型
  309. * http://www.cppblog.com/ownwaterloo/archive/2009/04/21/80655.aspx
  310. ************************************************************************/
  311. int _sprintf(char *dst, const char *format, ...)
  312. {
  313. char *s = dst;
  314. const char *f = format;
  315. va_list ap, another;
  316. va_start(ap, format);
  317. va_copy(another, ap);
  318. while (*f)
  319. {
  320. int n = 1;
  321. if ('%' != *f)
  322. {
  323. *s = *f;
  324. }
  325. else
  326. {
  327. ++f;
  328. switch (*f)
  329. {
  330. case 's': // 字符串
  331. {
  332. const char *p = va_arg(ap, char *);
  333. n = strlen(p);
  334. memcpy(s, p, n);
  335. }
  336. break;
  337. case 'd':
  338. case 'u': // 整数
  339. {
  340. char buf[32];
  341. n = _itoa(va_arg(ap, int), buf);
  342. memcpy(s, buf, n);
  343. }
  344. break;
  345. case 'f': // 浮点数
  346. {
  347. char buf[32];
  348. n = _ftoa(va_arg(ap, double), buf, 6);
  349. memcpy(s, buf, n);
  350. }
  351. break;
  352. case 'x': // 16进制数
  353. {
  354. char buf[32];
  355. n = _i2a(va_arg(ap, int), buf, 16);
  356. memcpy(s, buf, n);
  357. }
  358. break;
  359. case 'c': // 字符
  360. {
  361. *s = va_arg(ap, int);
  362. }
  363. break;
  364. case '%': // 百分号
  365. {
  366. *s = '%';
  367. }
  368. break;
  369. default:
  370. {
  371. va_end(ap);
  372. int x = vsprintf(dst, format, another);
  373. va_end(another);
  374. return x;
  375. }
  376. break;
  377. }
  378. }
  379. ++f;
  380. s += n;
  381. }
  382. *s = 0;
  383. va_end(ap);
  384. return s - dst;
  385. }
  386. uint8 bcc_chk(uint8 *data, uint16 length)
  387. {
  388. uint8 bcc_chk_return = 0x00;
  389. uint16 count = 0;
  390. while (count < length)
  391. {
  392. bcc_chk_return ^= data[count];
  393. count++;
  394. }
  395. return bcc_chk_return;
  396. }
  397. uint16 crc_chk(uint8 *data, uint8 length)
  398. {
  399. uint8 j;
  400. uint16 reg_crc = 0xFFFF;
  401. while (length--)
  402. {
  403. reg_crc ^= *data++;
  404. for (j = 0; j < 8; j++)
  405. {
  406. if (reg_crc & 0x01)
  407. {
  408. reg_crc = (reg_crc >> 1) ^ 0xA001;
  409. }
  410. else
  411. {
  412. reg_crc = reg_crc >> 1;
  413. }
  414. }
  415. }
  416. return reg_crc;
  417. }
  418. /***************************************************************************************************/
  419. #define PI 3.14159265358979
  420. //-------------------uint16数组取最大-------------------------------------
  421. uint16_T ArrMax(uint16_T *Data, uint16_T n)
  422. {
  423. uint16_T i;
  424. uint16_T DataMax;
  425. DataMax = Data[0];
  426. for (i = 0; i < n; i++)
  427. {
  428. if (DataMax < Data[i])
  429. {
  430. DataMax = Data[i];
  431. }
  432. }
  433. return DataMax;
  434. }
  435. //-----------------uint16数组取最小-------------------------------------
  436. uint16_T ArrMin(uint16_T *Data, uint16_T n)
  437. {
  438. uint16_T i;
  439. uint16_T DataMin;
  440. DataMin = Data[0];
  441. for (i = 0; i < n; i++)
  442. {
  443. if (DataMin > Data[i])
  444. {
  445. DataMin = Data[i];
  446. }
  447. }
  448. return DataMin;
  449. }
  450. //
  451. /////////////////////////// int16数组求均值////////////////////////////////////
  452. int16_T ArrMean(int16_T *Data, uint16_T n)
  453. {
  454. uint16_T i;
  455. int32_T Sum = 0;
  456. int16_T DataMean;
  457. for (i = 0; i < n; i++)
  458. {
  459. Sum = Sum + Data[i];
  460. }
  461. DataMean = (int16_T)Sum / n;
  462. return DataMean;
  463. }
  464. //-----------------real_T 限幅函数
  465. real_T Saturation_r(real_T in, real_T LowLim, real_T UpLim)
  466. {
  467. real_T out;
  468. out = in > LowLim ? in : LowLim;
  469. out = out > UpLim ? UpLim : out;
  470. return out;
  471. }
  472. //-----------------uint16 限幅函数
  473. uint16_T Saturation_u(uint16_T in, uint16_T LowLim, uint16_T UpLim)
  474. {
  475. uint16_T out;
  476. out = in > LowLim ? in : LowLim;
  477. out = out > UpLim ? UpLim : out;
  478. return out;
  479. }
  480. // ---------------------滤波控制变动速率--------------------------------------
  481. uint16_T DataFilt(uint16_T in, uint16_T *out, uint16_T Lim)
  482. {
  483. int16_T delt;
  484. delt = (int16_T)(in - *out);
  485. if (delt > Lim)
  486. {
  487. *out = *out + (delt > Lim ? Lim : delt);
  488. }
  489. if (delt < -Lim)
  490. {
  491. *out = *out + (delt < -Lim ? -Lim : delt);
  492. }
  493. if (delt <= Lim && delt >= -Lim)
  494. {
  495. *out = in;
  496. }
  497. return *out;
  498. }
  499. // ---------------------滤波控制变动速率--------------------------------------
  500. uint8_T DataFilt8(uint8_T in, uint8_T *out, uint8_T Lim)
  501. {
  502. int8_T delt;
  503. delt = (int8_T)(in - *out);
  504. if (delt > Lim)
  505. {
  506. *out = *out + (delt > Lim ? Lim : delt);
  507. }
  508. if (delt < -Lim)
  509. {
  510. *out = *out + (delt < -Lim ? -Lim : delt);
  511. }
  512. if (delt <= Lim && delt >= -Lim)
  513. {
  514. *out = in;
  515. }
  516. return *out;
  517. }
  518. //-------------------uint16 to uint16的一维查表--------------------------------------
  519. uint16_T look1_u16tu16(uint16_T u0, const uint16_T *bp0, const uint16_T *table, uint16_T MaxLen)
  520. {
  521. uint32_T bpIdx = 0;
  522. uint32_T iLeft = 0;
  523. uint32_T iRght = 0;
  524. uint16_T y = 0;
  525. uint32_T yL_0d0 = 0;
  526. uint32_T yR_0d0 = 0;
  527. uint32_T maxIndex = MaxLen - 1;
  528. if (u0 <= bp0[0U])
  529. {
  530. iLeft = 0U;
  531. iRght = 0U;
  532. }
  533. else if (u0 < bp0[maxIndex])
  534. {
  535. //????????u0??λ??
  536. bpIdx = maxIndex >> 1U;
  537. iLeft = 0U;
  538. iRght = maxIndex;
  539. while ((iRght - iLeft) > 1)
  540. {
  541. if (u0 < bp0[bpIdx])
  542. {
  543. iRght = bpIdx;
  544. }
  545. else
  546. {
  547. iLeft = bpIdx;
  548. }
  549. bpIdx = (iRght + iLeft) >> 1U;
  550. }
  551. }
  552. else
  553. {
  554. iLeft = maxIndex;
  555. iRght = maxIndex;
  556. }
  557. //???λ??????????
  558. if (iLeft != iRght)
  559. {
  560. //??????
  561. yR_0d0 = table[iLeft + 1U];
  562. yL_0d0 = table[iLeft];
  563. if (yR_0d0 >= yL_0d0)
  564. {
  565. y = (uint16_T)(((uint32_T)(u0 - bp0[iLeft]) * (yR_0d0 - yL_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]) + yL_0d0);
  566. }
  567. else
  568. {
  569. y = (uint16_T)((yL_0d0 - ((uint32_T)(u0 - bp0[iLeft]) * (yL_0d0 - yR_0d0)) / (bp0[iLeft + 1] - bp0[iLeft])));
  570. }
  571. }
  572. else
  573. {
  574. y = (uint16_T)table[iLeft];
  575. }
  576. return y;
  577. }
  578. //-------------------int16 to uint16的一维查表--------------------------------------
  579. uint16_T look1_i16tu16(int16_T u0, const int16_T *bp0, const uint16_T *table, uint16_T MaxLen)
  580. {
  581. uint32_T bpIdx = 0;
  582. uint32_T iLeft = 0;
  583. uint32_T iRght = 0;
  584. uint16_T y = 0;
  585. uint32_T yL_0d0 = 0;
  586. uint32_T yR_0d0 = 0;
  587. uint32_T maxIndex = MaxLen - 1;
  588. if (u0 <= bp0[0U])
  589. {
  590. iLeft = 0U;
  591. iRght = 0U;
  592. }
  593. else if (u0 < bp0[maxIndex])
  594. {
  595. //????????u0??λ??
  596. bpIdx = maxIndex >> 1U;
  597. iLeft = 0U;
  598. iRght = maxIndex;
  599. while ((iRght - iLeft) > 1)
  600. {
  601. if (u0 < bp0[bpIdx])
  602. {
  603. iRght = bpIdx;
  604. }
  605. else
  606. {
  607. iLeft = bpIdx;
  608. }
  609. bpIdx = (iRght + iLeft) >> 1U;
  610. }
  611. }
  612. else
  613. {
  614. iLeft = maxIndex;
  615. iRght = maxIndex;
  616. }
  617. //???λ??????????
  618. if (iLeft != iRght)
  619. {
  620. //??????
  621. yR_0d0 = table[iLeft + 1U];
  622. yL_0d0 = table[iLeft];
  623. if (yR_0d0 >= yL_0d0)
  624. {
  625. y = (uint16_T)(((uint32_T)(u0 - bp0[iLeft]) * (yR_0d0 - yL_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]) + yL_0d0);
  626. }
  627. else
  628. {
  629. y = (uint16_T)((yL_0d0 - ((uint32_T)(u0 - bp0[iLeft]) * (yL_0d0 - yR_0d0)) / (bp0[iLeft + 1] - bp0[iLeft])));
  630. }
  631. }
  632. else
  633. {
  634. y = (uint16_T)table[iLeft];
  635. }
  636. return y;
  637. }
  638. // uint16 二维查表
  639. uint16_T look2_u16u16tu16(uint16_T x, uint16_T y, const uint16_T xTable[], const uint16_T yTable[], const uint16_T zTable[], uint16_T xLen, uint16_T yLen)
  640. {
  641. uint16_T xMaxIndex;
  642. uint16_T yMaxIndex;
  643. uint16_T xIndexLeft;
  644. uint16_T xIndexRight;
  645. uint16_T yIndexLeft;
  646. uint16_T yIndexRight;
  647. uint16_T xValueLeft;
  648. uint16_T xValueRight;
  649. uint16_T yValueLeft;
  650. uint16_T yValueRight;
  651. uint16_T bpIdx;
  652. uint32_T zIndex1;
  653. uint32_T zIndex2;
  654. uint32_T zValueMid_xLeft;
  655. uint32_T zValueMid_xRight;
  656. uint16_T z;
  657. xMaxIndex = xLen - 1;
  658. yMaxIndex = yLen - 1;
  659. if (x <= xTable[0U])
  660. {
  661. xIndexLeft = 0U;
  662. xIndexRight = 0U;
  663. }
  664. else if (x >= xTable[xMaxIndex])
  665. {
  666. xIndexLeft = xMaxIndex;
  667. xIndexRight = xMaxIndex;
  668. }
  669. else
  670. {
  671. bpIdx = xMaxIndex >> 1U;
  672. xIndexLeft = 0;
  673. xIndexRight = xMaxIndex;
  674. while (xIndexRight - xIndexLeft > 1U)
  675. {
  676. if (x < xTable[bpIdx])
  677. {
  678. xIndexRight = bpIdx;
  679. }
  680. else
  681. {
  682. xIndexLeft = bpIdx;
  683. }
  684. bpIdx = (xIndexRight + xIndexLeft) >> 1U;
  685. }
  686. }
  687. xValueLeft = xTable[xIndexLeft];
  688. xValueRight = xTable[xIndexRight];
  689. if (y <= yTable[0U])
  690. {
  691. yIndexLeft = 0U;
  692. yIndexRight = 0U;
  693. }
  694. else if (y >= yTable[yMaxIndex])
  695. {
  696. yIndexLeft = yMaxIndex;
  697. yIndexRight = yMaxIndex;
  698. }
  699. else
  700. {
  701. bpIdx = yMaxIndex >> 1U;
  702. yIndexLeft = 0;
  703. yIndexRight = yMaxIndex;
  704. while (yIndexRight - yIndexLeft > 1U)
  705. {
  706. if (y < yTable[bpIdx])
  707. {
  708. yIndexRight = bpIdx;
  709. }
  710. else
  711. {
  712. yIndexLeft = bpIdx;
  713. }
  714. bpIdx = (yIndexRight + yIndexLeft) >> 1U;
  715. }
  716. }
  717. yValueLeft = yTable[yIndexLeft];
  718. yValueRight = yTable[yIndexRight];
  719. zIndex1 = yIndexLeft * xLen + xIndexLeft;
  720. zIndex2 = yIndexRight * xLen + xIndexLeft;
  721. if (yIndexLeft != yIndexRight)
  722. {
  723. if (zTable[zIndex2] > zTable[zIndex1])
  724. {
  725. zValueMid_xLeft = (uint16_T)(((uint32_T)(y - yValueLeft) * (zTable[zIndex2] - zTable[zIndex1])) / (yValueRight - yValueLeft) + zTable[zIndex1]);
  726. }
  727. else
  728. {
  729. zValueMid_xLeft = (uint16_T)(zTable[zIndex1] - ((uint32_T)(y - yValueLeft) * (zTable[zIndex1] - zTable[zIndex2])) / (yValueRight - yValueLeft));
  730. }
  731. if (zTable[zIndex2 + 1] > zTable[zIndex1 + 1])
  732. {
  733. zValueMid_xRight = (uint16_T)(((uint32_T)(y - yValueLeft) * (zTable[zIndex2 + 1] - zTable[zIndex1 + 1])) / (yValueRight - yValueLeft) + zTable[zIndex1 + 1]);
  734. }
  735. else
  736. {
  737. zValueMid_xRight = (uint16_T)(zTable[zIndex1 + 1] - ((uint32_T)(y - yValueLeft) * (zTable[zIndex1 + 1] - zTable[zIndex2 + 1])) / (yValueRight - yValueLeft));
  738. }
  739. }
  740. else
  741. {
  742. zValueMid_xLeft = (uint16_T)zTable[zIndex1];
  743. zValueMid_xRight = (uint16_T)zTable[zIndex1 + 1];
  744. }
  745. if (xIndexLeft != xIndexRight)
  746. {
  747. if (zValueMid_xLeft < zValueMid_xRight)
  748. {
  749. z = (uint16_T)(((uint32_T)(x - xValueLeft) * (zValueMid_xRight - zValueMid_xLeft)) / ((uint16_T)(xValueRight - xValueLeft)) + zValueMid_xLeft);
  750. }
  751. else
  752. {
  753. z = (uint16_T)(zValueMid_xLeft - ((uint32_T)(x - xValueLeft) * (zValueMid_xLeft - zValueMid_xRight)) / ((uint16_T)(xValueRight - xValueLeft)));
  754. }
  755. }
  756. else
  757. {
  758. z = (uint16_T)(zValueMid_xLeft);
  759. }
  760. return z;
  761. }
  762. //-----------------------------------------------诊断大于阈值
  763. boolean_T DiagThrSystem1(boolean_T Enable, boolean_T precondition, uint16_T Input, uint16_T fltThr, uint16_T recThr, uint8_T fltNumThr, uint8_T recNumThr, uint8_T *fltNum, uint8_T *recNum, boolean_T *fitFlg)
  764. {
  765. if (Enable && precondition && Input > fltThr)
  766. {
  767. *fltNum = (*fltNum + 1) > 200 ? 200 : (*fltNum + 1);
  768. }
  769. else
  770. {
  771. *fltNum = 0;
  772. }
  773. if (Enable && precondition && Input < recThr)
  774. {
  775. *recNum = (*recNum + 1) > 200 ? 200 : (*recNum + 1);
  776. }
  777. else
  778. {
  779. *recNum = 0;
  780. }
  781. if ((*fltNum > fltNumThr || (*fitFlg && *recNum < recNumThr)) && precondition)
  782. {
  783. *fitFlg = true;
  784. }
  785. else
  786. {
  787. *fitFlg = false;
  788. }
  789. return *fitFlg;
  790. }
  791. //================================诊断小于阈值===================================
  792. boolean_T DiagThrSystem2(boolean_T Enable, boolean_T precondition, uint16_T Input, uint16_T fltThr, uint16_T recThr, uint8_T fltNumThr, uint8_T recNumThr, uint8_T *fltNum, uint8_T *recNum, boolean_T *fitFlg)
  793. {
  794. if (Enable && precondition && Input < fltThr)
  795. {
  796. *fltNum = (*fltNum + 1) > 200 ? 200 : (*fltNum + 1);
  797. }
  798. else
  799. {
  800. *fltNum = 0;
  801. }
  802. if (Enable && precondition && Input > recThr)
  803. {
  804. *recNum = (*recNum + 1) > 200 ? 200 : (*recNum + 1);
  805. }
  806. else
  807. {
  808. *recNum = 0;
  809. }
  810. if ((*fltNum > fltNumThr || (*fitFlg && *recNum < recNumThr)) && precondition)
  811. {
  812. *fitFlg = true;
  813. }
  814. else
  815. {
  816. *fitFlg = false;
  817. }
  818. return *fitFlg;
  819. }
  820. //---------------------------判断条件成立次数
  821. boolean_T JudgeTimeSystem(boolean_T Enable, boolean_T Input, uint16_T *N, uint16_T Thr)
  822. {
  823. boolean_T Flg = false;
  824. if (Input && Enable)
  825. {
  826. *N = (*N + 1) > 20000 ? 20000 : (*N + 1);
  827. }
  828. else
  829. {
  830. *N = 0;
  831. }
  832. if (*N > Thr && Enable)
  833. {
  834. Flg = true;
  835. }
  836. else
  837. {
  838. Flg = false;
  839. }
  840. return Flg;
  841. }
  842. void fft(real_T *S, uint16_T N, real_T freq, real_T *returnFreq, real_T *returnP) // N为偶数
  843. {
  844. creal_T X[SIZE_FFT];
  845. creal_T W[SIZE_FFT];
  846. real_T Y1[SIZE_FFT];
  847. // real_T Y2[SIZE_FFT / 2 + 1];
  848. // real_T F[SIZE_FFT / 2 + 1];
  849. fft_type fft_data[SIZE_FFT / 2 + 1];
  850. uint16_T i = 0;
  851. uint16_T j = 0;
  852. uint16_T k = 0;
  853. uint16_T l = 0;
  854. creal_T up;
  855. creal_T down;
  856. creal_T product;
  857. // 变换
  858. for (uint16_t i = 0; i < N; i++)
  859. {
  860. X[i].re = S[i];
  861. X[i].im = 0;
  862. }
  863. RaderReverse(X, N);
  864. for (i = 0; i < N; i++)
  865. {
  866. W[i].re = cos(2 * PI / N * i);
  867. W[i].im = -sin(2 * PI / N * i);
  868. }
  869. for (i = 0; i < log(N) / log(2); i++)
  870. {
  871. l = 1 << i;
  872. for (j = 0; j < N; j += 2 * l)
  873. {
  874. for (k = 0; k < l; k++)
  875. {
  876. cmul(X[j + k + l], W[N * k / 2 / l], &product);
  877. cadd(X[j + k], product, &up);
  878. csub(X[j + k], product, &down);
  879. X[j + k] = up;
  880. X[j + k + l] = down;
  881. }
  882. }
  883. }
  884. // 计算幅值
  885. for (i = 0; i < N; i++)
  886. {
  887. Y1[i] = sqrt((X[i].im) * (X[i].im) + (X[i].re) * (X[i].re)) / N;
  888. }
  889. fft_data[0].amp = Y1[0];
  890. fft_data[N / 2].amp = Y1[N / 2];
  891. for (i = 1; i < N / 2; i++)
  892. {
  893. fft_data[i].amp = 2 * Y1[i];
  894. }
  895. // 计算频率
  896. for (i = 0; i < N / 2 + 1; i++)
  897. {
  898. fft_data[i].freq = freq * i / N;
  899. }
  900. // 从大到小 排序
  901. // real_T temp;
  902. // uint16_T temp_idx;
  903. // uint16_T idx[N / 2 + 1];
  904. // for (i = 0; i < N / 2 + 1; i++)
  905. // {
  906. // idx[i] = i;
  907. // }
  908. //
  909. // for (j = 0; j < N / 2 + 1; j++) // 比较n-1轮
  910. // {
  911. // for (k = 0; k < N / 2 - j; k++) // 每轮比较n-1-i次,
  912. // {
  913. //
  914. // if (Y2[k] < Y2[k + 1]) // 从大到小
  915. // {
  916. // temp = Y2[k];
  917. // Y2[k] = Y2[k + 1];
  918. // Y2[k + 1] = temp;
  919. //
  920. // temp_idx = idx[k];
  921. // idx[k] = idx[k + 1];
  922. // idx[k + 1] = temp_idx;
  923. // }
  924. // }
  925. // }
  926. // // 输出前5个
  927. qsort(fft_data,sizeof(fft_data) / sizeof(fft_data[0]),sizeof(fft_data[0]),fft_cmp);
  928. for (i = 0; i < 5; i++)
  929. {
  930. returnFreq[i] = fft_data[i].freq;
  931. returnP[i] = fft_data[i].amp;
  932. }
  933. }
  934. //
  935. void RaderReverse(creal_T *X, uint16_T N)
  936. {
  937. uint16_T i;
  938. uint16_T j;
  939. uint16_T k;
  940. creal_T temp;
  941. j = N / 2;
  942. for (i = 1; i < N - 1; i++)
  943. {
  944. if (i < j)
  945. {
  946. temp = X[j];
  947. X[j] = X[i];
  948. X[i] = temp;
  949. }
  950. k = N / 2;
  951. while (k <= j)
  952. {
  953. j = j - k;
  954. k = k / 2;
  955. }
  956. j = j + k;
  957. }
  958. }
  959. void cmul(creal_T a, creal_T b, creal_T *c)
  960. {
  961. c->re = a.re * b.re - a.im * b.im;
  962. c->im = a.re * b.im + a.im * b.re;
  963. }
  964. void cadd(creal_T a, creal_T b, creal_T *c)
  965. {
  966. c->re = a.re + b.re;
  967. c->im = a.im + b.im;
  968. }
  969. void csub(creal_T a, creal_T b, creal_T *c)
  970. {
  971. c->re = a.re - b.re;
  972. c->im = a.im - b.im;
  973. }