Error.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: Error.cpp
  4. * Description: Error functions
  5. *
  6. * $Date: 20. June 2019
  7. * $Revision: V1.0.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "arm_math.h"
  29. #include "Error.h"
  30. #include <stdlib.h>
  31. namespace Client {
  32. template <typename T>
  33. void assert_not_empty_generic(unsigned long nb, AnyPattern<T> &p)
  34. {
  35. if (p.nbSamples() == 0)
  36. {
  37. throw (Error(EMPTY_PATTERN_ERROR,nb));
  38. }
  39. if (p.ptr() == NULL)
  40. {
  41. throw (Error(EMPTY_PATTERN_ERROR,nb));
  42. }
  43. };
  44. template <>
  45. void assert_near_equal(unsigned long nb,double pa, double pb, double threshold)
  46. {
  47. if (fabs(pa - pb) > threshold)
  48. {
  49. throw (Error(EQUAL_ERROR,nb));
  50. }
  51. };
  52. template <>
  53. void assert_near_equal(unsigned long nb,float32_t pa, float32_t pb, float32_t threshold)
  54. {
  55. if (fabs(pa - pb) > threshold)
  56. {
  57. throw (Error(EQUAL_ERROR,nb));
  58. }
  59. };
  60. template <>
  61. void assert_near_equal(unsigned long nb,q63_t pa, q63_t pb, q63_t threshold)
  62. {
  63. if (abs(pa - pb) > threshold)
  64. {
  65. throw (Error(EQUAL_ERROR,nb));
  66. }
  67. };
  68. template <>
  69. void assert_near_equal(unsigned long nb,q31_t pa, q31_t pb, q31_t threshold)
  70. {
  71. if (abs(pa - pb) > threshold)
  72. {
  73. throw (Error(EQUAL_ERROR,nb));
  74. }
  75. };
  76. template <>
  77. void assert_near_equal(unsigned long nb,q15_t pa, q15_t pb, q15_t threshold)
  78. {
  79. if (abs(pa - pb) > threshold)
  80. {
  81. throw (Error(EQUAL_ERROR,nb));
  82. }
  83. };
  84. template <>
  85. void assert_near_equal(unsigned long nb,q7_t pa, q7_t pb, q7_t threshold)
  86. {
  87. if (abs(pa - pb) > threshold)
  88. {
  89. throw (Error(EQUAL_ERROR,nb));
  90. }
  91. };
  92. void assert_not_empty(unsigned long nb, AnyPattern<float32_t> &p)
  93. {
  94. assert_not_empty_generic(nb,p);
  95. }
  96. void assert_not_empty(unsigned long nb, AnyPattern<q63_t> &p)
  97. {
  98. assert_not_empty_generic(nb,p);
  99. }
  100. void assert_not_empty(unsigned long nb, AnyPattern<q31_t> &p)
  101. {
  102. assert_not_empty_generic(nb,p);
  103. }
  104. void assert_not_empty(unsigned long nb, AnyPattern<q15_t> &p)
  105. {
  106. assert_not_empty_generic(nb,p);
  107. }
  108. void assert_not_empty(unsigned long nb, AnyPattern<q7_t> &p)
  109. {
  110. assert_not_empty_generic(nb,p);
  111. }
  112. void assert_relative_error(unsigned long nb,float32_t &a, float32_t &b, double threshold)
  113. {
  114. double rel,delta,average;
  115. delta=abs(a-b);
  116. average = (abs(a) + abs(b)) / 2.0f;
  117. if (average !=0)
  118. {
  119. rel = delta / average;
  120. //printf("%6.9f %6.9f %6.9f %g %g\n",a,b,rel,delta,average);
  121. if (rel > threshold)
  122. {
  123. throw (Error(RELATIVE_ERROR,nb));
  124. }
  125. }
  126. };
  127. void assert_relative_error(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, double threshold)
  128. {
  129. ASSERT_NOT_EMPTY(pa);
  130. ASSERT_NOT_EMPTY(pb);
  131. if (pa.nbSamples() != pb.nbSamples())
  132. {
  133. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  134. }
  135. unsigned long i=0;
  136. float32_t *ptrA = pa.ptr();
  137. float32_t *ptrB = pb.ptr();
  138. for(i=0; i < pa.nbSamples(); i++)
  139. {
  140. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  141. }
  142. };
  143. void assert_close_error(unsigned long nb,float32_t &ref, float32_t &val, double absthreshold,double relthreshold)
  144. {
  145. if (abs(val - ref) > (absthreshold + relthreshold * abs(ref)))
  146. {
  147. throw (Error(CLOSE_ERROR,nb));
  148. }
  149. };
  150. void assert_close_error(unsigned long nb,AnyPattern<float32_t> &pref, AnyPattern<float32_t> &pval, double absthreshold,double relthreshold)
  151. {
  152. ASSERT_NOT_EMPTY(pref);
  153. ASSERT_NOT_EMPTY(pval);
  154. if (pref.nbSamples() != pval.nbSamples())
  155. {
  156. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  157. }
  158. unsigned long i=0;
  159. float32_t *ptrA = pref.ptr();
  160. float32_t *ptrB = pval.ptr();
  161. for(i=0; i < pref.nbSamples(); i++)
  162. {
  163. assert_close_error(nb,ptrA[i],ptrB[i],absthreshold,relthreshold);
  164. }
  165. };
  166. /**
  167. * @brief Calculation of SNR
  168. * @param float* Pointer to the reference buffer
  169. * @param float* Pointer to the test buffer
  170. * @param uint32_t total number of samples
  171. * @return float SNR
  172. * The function calculates signal to noise ratio for the reference output
  173. * and test output
  174. */
  175. /* If NaN, force SNR to 0.0 to ensure test will fail */
  176. #define IFNANRETURNZERO(val)\
  177. if (isnan((val))) \
  178. { \
  179. return(0.0); \
  180. }
  181. #define IFINFINITERETURN(val,def)\
  182. if (isinf((val))) \
  183. { \
  184. if ((val) > 0) \
  185. { \
  186. return(def); \
  187. } \
  188. else \
  189. { \
  190. return(-def); \
  191. } \
  192. }
  193. float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize)
  194. {
  195. float EnergySignal = 0.0, EnergyError = 0.0;
  196. uint32_t i;
  197. float SNR;
  198. for (i = 0; i < buffSize; i++)
  199. {
  200. /* Checking for a NAN value in pRef array */
  201. IFNANRETURNZERO(pRef[i]);
  202. /* Checking for a NAN value in pTest array */
  203. IFNANRETURNZERO(pTest[i]);
  204. EnergySignal += pRef[i] * pRef[i];
  205. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  206. }
  207. /* Checking for a NAN value in EnergyError */
  208. IFNANRETURNZERO(EnergyError);
  209. SNR = 10 * log10f (EnergySignal / EnergyError);
  210. /* Checking for a NAN value in SNR */
  211. IFNANRETURNZERO(SNR);
  212. IFINFINITERETURN(SNR,100000.0);
  213. return (SNR);
  214. }
  215. float arm_snr_q63(q63_t *pRef, q63_t *pTest, uint32_t buffSize)
  216. {
  217. double EnergySignal = 0.0, EnergyError = 0.0;
  218. uint32_t i;
  219. float SNR;
  220. double testVal,refVal;
  221. for (i = 0; i < buffSize; i++)
  222. {
  223. testVal = ((double)pTest[i]) / 9223372036854775808.0;
  224. refVal = ((double)pRef[i]) / 9223372036854775808.0;
  225. EnergySignal += refVal * refVal;
  226. EnergyError += (refVal - testVal) * (refVal - testVal);
  227. }
  228. SNR = 10 * log10 (EnergySignal / EnergyError);
  229. /* Checking for a NAN value in SNR */
  230. IFNANRETURNZERO(SNR);
  231. IFINFINITERETURN(SNR,100000.0);
  232. //printf("SNR = %f\n",SNR);
  233. return (SNR);
  234. }
  235. float arm_snr_q31(q31_t *pRef, q31_t *pTest, uint32_t buffSize)
  236. {
  237. float EnergySignal = 0.0, EnergyError = 0.0;
  238. uint32_t i;
  239. float SNR;
  240. float32_t testVal,refVal;
  241. for (i = 0; i < buffSize; i++)
  242. {
  243. testVal = ((float32_t)pTest[i]) / 2147483648.0f;
  244. refVal = ((float32_t)pRef[i]) / 2147483648.0f;
  245. EnergySignal += refVal * refVal;
  246. EnergyError += (refVal - testVal) * (refVal - testVal);
  247. }
  248. SNR = 10 * log10f (EnergySignal / EnergyError);
  249. /* Checking for a NAN value in SNR */
  250. IFNANRETURNZERO(SNR);
  251. IFINFINITERETURN(SNR,100000.0);
  252. //printf("SNR = %f\n",SNR);
  253. return (SNR);
  254. }
  255. float arm_snr_q15(q15_t *pRef, q15_t *pTest, uint32_t buffSize)
  256. {
  257. float EnergySignal = 0.0, EnergyError = 0.0;
  258. uint32_t i;
  259. float SNR;
  260. float32_t testVal,refVal;
  261. for (i = 0; i < buffSize; i++)
  262. {
  263. testVal = ((float32_t)pTest[i]) / 32768.0f;
  264. refVal = ((float32_t)pRef[i]) / 32768.0f;
  265. EnergySignal += refVal * refVal;
  266. EnergyError += (refVal - testVal) * (refVal - testVal);
  267. }
  268. SNR = 10 * log10f (EnergySignal / EnergyError);
  269. /* Checking for a NAN value in SNR */
  270. IFNANRETURNZERO(SNR);
  271. IFINFINITERETURN(SNR,100000.0);
  272. //printf("SNR = %f\n",SNR);
  273. return (SNR);
  274. }
  275. float arm_snr_q7(q7_t *pRef, q7_t *pTest, uint32_t buffSize)
  276. {
  277. float EnergySignal = 0.0, EnergyError = 0.0;
  278. uint32_t i;
  279. float SNR;
  280. float32_t testVal,refVal;
  281. for (i = 0; i < buffSize; i++)
  282. {
  283. testVal = ((float32_t)pTest[i]) / 128.0f;
  284. refVal = ((float32_t)pRef[i]) / 128.0f;
  285. EnergySignal += refVal * refVal;
  286. EnergyError += (refVal - testVal) * (refVal - testVal);
  287. }
  288. SNR = 10 * log10f (EnergySignal / EnergyError);
  289. IFNANRETURNZERO(SNR);
  290. IFINFINITERETURN(SNR,100000.0);
  291. return (SNR);
  292. }
  293. double arm_snr_f64(double *pRef, double *pTest, uint32_t buffSize)
  294. {
  295. double EnergySignal = 0.0, EnergyError = 0.0;
  296. uint32_t i;
  297. double SNR;
  298. for (i = 0; i < buffSize; i++)
  299. {
  300. /* Checking for a NAN value in pRef array */
  301. IFNANRETURNZERO(pRef[i]);
  302. /* Checking for a NAN value in pTest array */
  303. IFNANRETURNZERO(pTest[i]);
  304. EnergySignal += pRef[i] * pRef[i];
  305. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  306. }
  307. /* Checking for a NAN value in EnergyError */
  308. IFNANRETURNZERO(EnergyError);
  309. SNR = 10 * log10 (EnergySignal / EnergyError);
  310. /* Checking for a NAN value in SNR */
  311. IFNANRETURNZERO(SNR);
  312. IFINFINITERETURN(SNR,100000.0);
  313. return (SNR);
  314. }
  315. void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold)
  316. {
  317. float32_t snr;
  318. ASSERT_NOT_EMPTY(pa);
  319. ASSERT_NOT_EMPTY(pb);
  320. if (pa.nbSamples() != pb.nbSamples())
  321. {
  322. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  323. }
  324. float32_t *ptrA = pa.ptr();
  325. float32_t *ptrB = pb.ptr();
  326. snr = arm_snr_f32(ptrA, ptrB, pa.nbSamples());
  327. //printf("SNR = %f\n",snr);
  328. if (snr < threshold)
  329. {
  330. throw (Error(SNR_ERROR,nb));
  331. }
  332. }
  333. void assert_snr_error(unsigned long nb,float32_t a,float32_t b, float32_t threshold)
  334. {
  335. float32_t snr;
  336. snr = arm_snr_f32(&a, &b, 1);
  337. //printf("SNR = %f, %f %f\n",snr,a,b);
  338. if (snr < threshold)
  339. {
  340. throw (Error(SNR_ERROR,nb));
  341. }
  342. }
  343. void assert_snr_error(unsigned long nb,AnyPattern<q63_t> &pa,AnyPattern<q63_t> &pb, float32_t threshold)
  344. {
  345. float32_t snr;
  346. ASSERT_NOT_EMPTY(pa);
  347. ASSERT_NOT_EMPTY(pb);
  348. if (pa.nbSamples() != pb.nbSamples())
  349. {
  350. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  351. }
  352. q63_t *ptrA = pa.ptr();
  353. q63_t *ptrB = pb.ptr();
  354. snr = arm_snr_q63(ptrA, ptrB, pa.nbSamples());
  355. //printf("SNR = %f\n",snr);
  356. if (snr < threshold)
  357. {
  358. throw (Error(SNR_ERROR,nb));
  359. }
  360. }
  361. void assert_snr_error(unsigned long nb,q63_t a,q63_t b, float32_t threshold)
  362. {
  363. float32_t snr;
  364. snr = arm_snr_q63(&a, &b, 1);
  365. //printf("SNR = %f\n",snr);
  366. if (snr < threshold)
  367. {
  368. throw (Error(SNR_ERROR,nb));
  369. }
  370. }
  371. void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold)
  372. {
  373. float32_t snr;
  374. ASSERT_NOT_EMPTY(pa);
  375. ASSERT_NOT_EMPTY(pb);
  376. if (pa.nbSamples() != pb.nbSamples())
  377. {
  378. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  379. }
  380. q31_t *ptrA = pa.ptr();
  381. q31_t *ptrB = pb.ptr();
  382. snr = arm_snr_q31(ptrA, ptrB, pa.nbSamples());
  383. if (snr < threshold)
  384. {
  385. throw (Error(SNR_ERROR,nb));
  386. }
  387. }
  388. void assert_snr_error(unsigned long nb,q31_t a,q31_t b, float32_t threshold)
  389. {
  390. float32_t snr;
  391. snr = arm_snr_q31(&a, &b, 1);
  392. if (snr < threshold)
  393. {
  394. throw (Error(SNR_ERROR,nb));
  395. }
  396. }
  397. void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold)
  398. {
  399. float32_t snr;
  400. ASSERT_NOT_EMPTY(pa);
  401. ASSERT_NOT_EMPTY(pb);
  402. if (pa.nbSamples() != pb.nbSamples())
  403. {
  404. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  405. }
  406. q15_t *ptrA = pa.ptr();
  407. q15_t *ptrB = pb.ptr();
  408. snr = arm_snr_q15(ptrA, ptrB, pa.nbSamples());
  409. //printf("SNR = %f\n",snr);
  410. if (snr < threshold)
  411. {
  412. throw (Error(SNR_ERROR,nb));
  413. }
  414. }
  415. void assert_snr_error(unsigned long nb,q15_t a,q15_t b, float32_t threshold)
  416. {
  417. float32_t snr;
  418. snr = arm_snr_q15(&a, &b, 1);
  419. //printf("SNR = %f\n",snr);
  420. if (snr < threshold)
  421. {
  422. throw (Error(SNR_ERROR,nb));
  423. }
  424. }
  425. void assert_snr_error(unsigned long nb,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold)
  426. {
  427. float32_t snr;
  428. ASSERT_NOT_EMPTY(pa);
  429. ASSERT_NOT_EMPTY(pb);
  430. if (pa.nbSamples() != pb.nbSamples())
  431. {
  432. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  433. }
  434. q7_t *ptrA = pa.ptr();
  435. q7_t *ptrB = pb.ptr();
  436. snr = arm_snr_q7(ptrA, ptrB, pa.nbSamples());
  437. //printf("SNR = %f\n",snr);
  438. if (snr < threshold)
  439. {
  440. throw (Error(SNR_ERROR,nb));
  441. }
  442. }
  443. void assert_snr_error(unsigned long nb,q7_t a,q7_t b, float32_t threshold)
  444. {
  445. float32_t snr;
  446. snr = arm_snr_q7(&a, &b, 1);
  447. //printf("SNR = %f\n",snr);
  448. if (snr < threshold)
  449. {
  450. throw (Error(SNR_ERROR,nb));
  451. }
  452. }
  453. void assert_true(unsigned long nb,bool cond)
  454. {
  455. if (!cond)
  456. {
  457. throw (Error(BOOL_ERROR,nb));
  458. }
  459. }
  460. void assert_false(unsigned long nb,bool cond)
  461. {
  462. if (cond)
  463. {
  464. throw (Error(BOOL_ERROR,nb));
  465. }
  466. }
  467. }