Error.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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\n",a,b,rel);
  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. /**
  144. * @brief Calculation of SNR
  145. * @param float* Pointer to the reference buffer
  146. * @param float* Pointer to the test buffer
  147. * @param uint32_t total number of samples
  148. * @return float SNR
  149. * The function calculates signal to noise ratio for the reference output
  150. * and test output
  151. */
  152. /* If NaN, force SNR to 0.0 to ensure test will fail */
  153. #define IFNANRETURNZERO(val)\
  154. if (isnan((val))) \
  155. { \
  156. return(0.0); \
  157. }
  158. #define IFINFINITERETURN(val,def)\
  159. if (isinf((val))) \
  160. { \
  161. if ((val) > 0) \
  162. { \
  163. return(def); \
  164. } \
  165. else \
  166. { \
  167. return(-def); \
  168. } \
  169. }
  170. float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize)
  171. {
  172. float EnergySignal = 0.0, EnergyError = 0.0;
  173. uint32_t i;
  174. float SNR;
  175. for (i = 0; i < buffSize; i++)
  176. {
  177. /* Checking for a NAN value in pRef array */
  178. IFNANRETURNZERO(pRef[i]);
  179. /* Checking for a NAN value in pTest array */
  180. IFNANRETURNZERO(pTest[i]);
  181. EnergySignal += pRef[i] * pRef[i];
  182. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  183. }
  184. /* Checking for a NAN value in EnergyError */
  185. IFNANRETURNZERO(EnergyError);
  186. SNR = 10 * log10f (EnergySignal / EnergyError);
  187. /* Checking for a NAN value in SNR */
  188. IFNANRETURNZERO(SNR);
  189. IFINFINITERETURN(SNR,100000.0);
  190. return (SNR);
  191. }
  192. float arm_snr_q63(q63_t *pRef, q63_t *pTest, uint32_t buffSize)
  193. {
  194. double EnergySignal = 0.0, EnergyError = 0.0;
  195. uint32_t i;
  196. float SNR;
  197. double testVal,refVal;
  198. for (i = 0; i < buffSize; i++)
  199. {
  200. testVal = ((double)pTest[i]) / 9223372036854775808.0;
  201. refVal = ((double)pRef[i]) / 9223372036854775808.0;
  202. EnergySignal += refVal * refVal;
  203. EnergyError += (refVal - testVal) * (refVal - testVal);
  204. }
  205. SNR = 10 * log10 (EnergySignal / EnergyError);
  206. /* Checking for a NAN value in SNR */
  207. IFNANRETURNZERO(SNR);
  208. IFINFINITERETURN(SNR,100000.0);
  209. //printf("SNR = %f\n",SNR);
  210. return (SNR);
  211. }
  212. float arm_snr_q31(q31_t *pRef, q31_t *pTest, uint32_t buffSize)
  213. {
  214. float EnergySignal = 0.0, EnergyError = 0.0;
  215. uint32_t i;
  216. float SNR;
  217. float32_t testVal,refVal;
  218. for (i = 0; i < buffSize; i++)
  219. {
  220. testVal = ((float32_t)pTest[i]) / 2147483648.0f;
  221. refVal = ((float32_t)pRef[i]) / 2147483648.0f;
  222. EnergySignal += refVal * refVal;
  223. EnergyError += (refVal - testVal) * (refVal - testVal);
  224. }
  225. SNR = 10 * log10f (EnergySignal / EnergyError);
  226. /* Checking for a NAN value in SNR */
  227. IFNANRETURNZERO(SNR);
  228. IFINFINITERETURN(SNR,100000.0);
  229. //printf("SNR = %f\n",SNR);
  230. return (SNR);
  231. }
  232. float arm_snr_q15(q15_t *pRef, q15_t *pTest, uint32_t buffSize)
  233. {
  234. float EnergySignal = 0.0, EnergyError = 0.0;
  235. uint32_t i;
  236. float SNR;
  237. float32_t testVal,refVal;
  238. for (i = 0; i < buffSize; i++)
  239. {
  240. testVal = ((float32_t)pTest[i]) / 32768.0f;
  241. refVal = ((float32_t)pRef[i]) / 32768.0f;
  242. EnergySignal += refVal * refVal;
  243. EnergyError += (refVal - testVal) * (refVal - testVal);
  244. }
  245. SNR = 10 * log10f (EnergySignal / EnergyError);
  246. /* Checking for a NAN value in SNR */
  247. IFNANRETURNZERO(SNR);
  248. IFINFINITERETURN(SNR,100000.0);
  249. //printf("SNR = %f\n",SNR);
  250. return (SNR);
  251. }
  252. float arm_snr_q7(q7_t *pRef, q7_t *pTest, uint32_t buffSize)
  253. {
  254. float EnergySignal = 0.0, EnergyError = 0.0;
  255. uint32_t i;
  256. float SNR;
  257. float32_t testVal,refVal;
  258. for (i = 0; i < buffSize; i++)
  259. {
  260. testVal = ((float32_t)pTest[i]) / 128.0f;
  261. refVal = ((float32_t)pRef[i]) / 128.0f;
  262. EnergySignal += refVal * refVal;
  263. EnergyError += (refVal - testVal) * (refVal - testVal);
  264. }
  265. SNR = 10 * log10f (EnergySignal / EnergyError);
  266. IFNANRETURNZERO(SNR);
  267. IFINFINITERETURN(SNR,100000.0);
  268. return (SNR);
  269. }
  270. double arm_snr_f64(double *pRef, double *pTest, uint32_t buffSize)
  271. {
  272. double EnergySignal = 0.0, EnergyError = 0.0;
  273. uint32_t i;
  274. double SNR;
  275. for (i = 0; i < buffSize; i++)
  276. {
  277. /* Checking for a NAN value in pRef array */
  278. IFNANRETURNZERO(pRef[i]);
  279. /* Checking for a NAN value in pTest array */
  280. IFNANRETURNZERO(pTest[i]);
  281. EnergySignal += pRef[i] * pRef[i];
  282. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  283. }
  284. /* Checking for a NAN value in EnergyError */
  285. IFNANRETURNZERO(EnergyError);
  286. SNR = 10 * log10 (EnergySignal / EnergyError);
  287. /* Checking for a NAN value in SNR */
  288. IFNANRETURNZERO(SNR);
  289. IFINFINITERETURN(SNR,100000.0);
  290. return (SNR);
  291. }
  292. void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold)
  293. {
  294. float32_t snr;
  295. ASSERT_NOT_EMPTY(pa);
  296. ASSERT_NOT_EMPTY(pb);
  297. if (pa.nbSamples() != pb.nbSamples())
  298. {
  299. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  300. }
  301. float32_t *ptrA = pa.ptr();
  302. float32_t *ptrB = pb.ptr();
  303. snr = arm_snr_f32(ptrA, ptrB, pa.nbSamples());
  304. //printf("SNR = %f\n",snr);
  305. if (snr < threshold)
  306. {
  307. throw (Error(SNR_ERROR,nb));
  308. }
  309. }
  310. void assert_snr_error(unsigned long nb,float32_t a,float32_t b, float32_t threshold)
  311. {
  312. float32_t snr;
  313. snr = arm_snr_f32(&a, &b, 1);
  314. //printf("SNR = %f, %f %f\n",snr,a,b);
  315. if (snr < threshold)
  316. {
  317. throw (Error(SNR_ERROR,nb));
  318. }
  319. }
  320. void assert_snr_error(unsigned long nb,AnyPattern<q63_t> &pa,AnyPattern<q63_t> &pb, float32_t threshold)
  321. {
  322. float32_t snr;
  323. ASSERT_NOT_EMPTY(pa);
  324. ASSERT_NOT_EMPTY(pb);
  325. if (pa.nbSamples() != pb.nbSamples())
  326. {
  327. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  328. }
  329. q63_t *ptrA = pa.ptr();
  330. q63_t *ptrB = pb.ptr();
  331. snr = arm_snr_q63(ptrA, ptrB, pa.nbSamples());
  332. //printf("SNR = %f\n",snr);
  333. if (snr < threshold)
  334. {
  335. throw (Error(SNR_ERROR,nb));
  336. }
  337. }
  338. void assert_snr_error(unsigned long nb,q63_t a,q63_t b, float32_t threshold)
  339. {
  340. float32_t snr;
  341. snr = arm_snr_q63(&a, &b, 1);
  342. //printf("SNR = %f\n",snr);
  343. if (snr < threshold)
  344. {
  345. throw (Error(SNR_ERROR,nb));
  346. }
  347. }
  348. void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold)
  349. {
  350. float32_t snr;
  351. ASSERT_NOT_EMPTY(pa);
  352. ASSERT_NOT_EMPTY(pb);
  353. if (pa.nbSamples() != pb.nbSamples())
  354. {
  355. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  356. }
  357. q31_t *ptrA = pa.ptr();
  358. q31_t *ptrB = pb.ptr();
  359. snr = arm_snr_q31(ptrA, ptrB, pa.nbSamples());
  360. if (snr < threshold)
  361. {
  362. throw (Error(SNR_ERROR,nb));
  363. }
  364. }
  365. void assert_snr_error(unsigned long nb,q31_t a,q31_t b, float32_t threshold)
  366. {
  367. float32_t snr;
  368. snr = arm_snr_q31(&a, &b, 1);
  369. if (snr < threshold)
  370. {
  371. throw (Error(SNR_ERROR,nb));
  372. }
  373. }
  374. void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold)
  375. {
  376. float32_t snr;
  377. ASSERT_NOT_EMPTY(pa);
  378. ASSERT_NOT_EMPTY(pb);
  379. if (pa.nbSamples() != pb.nbSamples())
  380. {
  381. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  382. }
  383. q15_t *ptrA = pa.ptr();
  384. q15_t *ptrB = pb.ptr();
  385. snr = arm_snr_q15(ptrA, ptrB, pa.nbSamples());
  386. //printf("SNR = %f\n",snr);
  387. if (snr < threshold)
  388. {
  389. throw (Error(SNR_ERROR,nb));
  390. }
  391. }
  392. void assert_snr_error(unsigned long nb,q15_t a,q15_t b, float32_t threshold)
  393. {
  394. float32_t snr;
  395. snr = arm_snr_q15(&a, &b, 1);
  396. //printf("SNR = %f\n",snr);
  397. if (snr < threshold)
  398. {
  399. throw (Error(SNR_ERROR,nb));
  400. }
  401. }
  402. void assert_snr_error(unsigned long nb,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold)
  403. {
  404. float32_t snr;
  405. ASSERT_NOT_EMPTY(pa);
  406. ASSERT_NOT_EMPTY(pb);
  407. if (pa.nbSamples() != pb.nbSamples())
  408. {
  409. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  410. }
  411. q7_t *ptrA = pa.ptr();
  412. q7_t *ptrB = pb.ptr();
  413. snr = arm_snr_q7(ptrA, ptrB, pa.nbSamples());
  414. //printf("SNR = %f\n",snr);
  415. if (snr < threshold)
  416. {
  417. throw (Error(SNR_ERROR,nb));
  418. }
  419. }
  420. void assert_snr_error(unsigned long nb,q7_t a,q7_t b, float32_t threshold)
  421. {
  422. float32_t snr;
  423. snr = arm_snr_q7(&a, &b, 1);
  424. //printf("SNR = %f\n",snr);
  425. if (snr < threshold)
  426. {
  427. throw (Error(SNR_ERROR,nb));
  428. }
  429. }
  430. void assert_true(unsigned long nb,bool cond)
  431. {
  432. if (!cond)
  433. {
  434. throw (Error(BOOL_ERROR,nb));
  435. }
  436. }
  437. void assert_false(unsigned long nb,bool cond)
  438. {
  439. if (cond)
  440. {
  441. throw (Error(BOOL_ERROR,nb));
  442. }
  443. }
  444. }