Error.cpp 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 <cstdlib>
  29. #include <cstdio>
  30. #include "arm_math_types.h"
  31. #include "arm_math_types_f16.h"
  32. #include "Error.h"
  33. namespace Client {
  34. template <typename T>
  35. void assert_not_empty_generic(unsigned long nb, AnyPattern<T> &p)
  36. {
  37. if (p.nbSamples() == 0)
  38. {
  39. throw (Error(EMPTY_PATTERN_ERROR,nb));
  40. }
  41. if (p.ptr() == NULL)
  42. {
  43. throw (Error(EMPTY_PATTERN_ERROR,nb));
  44. }
  45. };
  46. template <>
  47. void assert_near_equal(unsigned long nb,double pa, double pb, double threshold)
  48. {
  49. if (fabs(pa - pb) > threshold)
  50. {
  51. char details[200];
  52. sprintf(details,"diff %g > %g (%g,%g)",fabs(pa - pb) , threshold,pa,pb);
  53. throw (Error(EQUAL_ERROR,nb,details));
  54. }
  55. };
  56. template <>
  57. void assert_near_equal(unsigned long nb,float32_t pa, float32_t pb, float32_t threshold)
  58. {
  59. if (fabs(pa - pb) > threshold)
  60. {
  61. char details[200];
  62. sprintf(details,"diff %g > %g (%g,%g)",fabs(pa - pb) , threshold, pa, pb);
  63. throw (Error(EQUAL_ERROR,nb,details));
  64. }
  65. };
  66. #if !defined (__CC_ARM) && defined(ARM_FLOAT16_SUPPORTED)
  67. template <>
  68. void assert_near_equal(unsigned long nb,float16_t pa, float16_t pb, float16_t threshold)
  69. {
  70. if (fabs(pa - pb) > threshold)
  71. {
  72. char details[200];
  73. sprintf(details,"diff %g > %g (%g,%g)",fabs(pa - pb) , threshold, pa, pb);
  74. throw (Error(EQUAL_ERROR,nb,details));
  75. }
  76. };
  77. #endif
  78. template <>
  79. void assert_near_equal(unsigned long nb,q63_t pa, q63_t pb, q63_t threshold)
  80. {
  81. if (abs(pa - pb) > threshold)
  82. {
  83. char details[200];
  84. #if __sizeof_long == 8
  85. sprintf(details,"diff %ld > %ld (0x%016lX,0x%016lX)",abs(pa - pb) , threshold,pa,pb);
  86. #else
  87. sprintf(details,"diff %lld > %lld (0x%016llX,0x%016llX)",abs(pa - pb) , threshold,pa,pb);
  88. #endif
  89. throw (Error(EQUAL_ERROR,nb,details));
  90. }
  91. };
  92. template <>
  93. void assert_near_equal(unsigned long nb,q31_t pa, q31_t pb, q31_t threshold)
  94. {
  95. if (abs(pa - pb) > threshold)
  96. {
  97. char details[200];
  98. sprintf(details,"diff %d > %d (0x%08X,0x%08X)",abs(pa - pb) , threshold,pa,pb);
  99. throw (Error(EQUAL_ERROR,nb,details));
  100. }
  101. };
  102. template <>
  103. void assert_near_equal(unsigned long nb,q15_t pa, q15_t pb, q15_t threshold)
  104. {
  105. if (abs(pa - pb) > threshold)
  106. {
  107. char details[200];
  108. sprintf(details,"diff %d > %d (0x%04X,0x%04X)",abs(pa - pb) , threshold,pa,pb);
  109. throw (Error(EQUAL_ERROR,nb,details));
  110. }
  111. };
  112. template <>
  113. void assert_near_equal(unsigned long nb,q7_t pa, q7_t pb, q7_t threshold)
  114. {
  115. if (abs(pa - pb) > threshold)
  116. {
  117. char details[200];
  118. sprintf(details,"diff %d > %d (0x%02X,0x%02X)",abs(pa - pb) , threshold,pa,pb);
  119. throw (Error(EQUAL_ERROR,nb,details));
  120. }
  121. };
  122. void assert_not_empty(unsigned long nb, AnyPattern<float64_t> &p)
  123. {
  124. assert_not_empty_generic(nb,p);
  125. }
  126. void assert_not_empty(unsigned long nb, AnyPattern<float32_t> &p)
  127. {
  128. assert_not_empty_generic(nb,p);
  129. }
  130. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  131. void assert_not_empty(unsigned long nb, AnyPattern<float16_t> &p)
  132. {
  133. assert_not_empty_generic(nb,p);
  134. }
  135. #endif
  136. void assert_not_empty(unsigned long nb, AnyPattern<q63_t> &p)
  137. {
  138. assert_not_empty_generic(nb,p);
  139. }
  140. void assert_not_empty(unsigned long nb, AnyPattern<q31_t> &p)
  141. {
  142. assert_not_empty_generic(nb,p);
  143. }
  144. void assert_not_empty(unsigned long nb, AnyPattern<q15_t> &p)
  145. {
  146. assert_not_empty_generic(nb,p);
  147. }
  148. void assert_not_empty(unsigned long nb, AnyPattern<q7_t> &p)
  149. {
  150. assert_not_empty_generic(nb,p);
  151. }
  152. void assert_not_empty(unsigned long nb, AnyPattern<uint32_t> &p)
  153. {
  154. assert_not_empty_generic(nb,p);
  155. }
  156. void assert_not_empty(unsigned long nb, AnyPattern<uint16_t> &p)
  157. {
  158. assert_not_empty_generic(nb,p);
  159. }
  160. void assert_not_empty(unsigned long nb, AnyPattern<uint8_t> &p)
  161. {
  162. assert_not_empty_generic(nb,p);
  163. }
  164. void assert_relative_error(unsigned long nb,float64_t &a, float64_t &b, double threshold)
  165. {
  166. float64_t rel,delta,average;
  167. delta=abs(a-b);
  168. average = (abs(a) + abs(b)) / 2.0f;
  169. if (average !=0)
  170. {
  171. rel = delta / average;
  172. //printf("%6.9f %6.9f %6.9f %g %g\n",a,b,rel,delta,average);
  173. if (rel > threshold)
  174. {
  175. //printf("rel = %g, threshold %g \n",rel,threshold);
  176. char details[200];
  177. sprintf(details,"diff (%g,%g), %g > %g",a,b,rel , threshold);
  178. throw (Error(RELATIVE_ERROR,nb,details));
  179. }
  180. }
  181. };
  182. void assert_relative_error(unsigned long nb,float32_t &a, float32_t &b, double threshold)
  183. {
  184. double rel,delta,average;
  185. delta=abs(a-b);
  186. average = (abs((float)a) + abs((float)b)) / 2.0f;
  187. if (average !=0)
  188. {
  189. rel = delta / average;
  190. //printf("%6.9f %6.9f %6.9f %g %g\n",a,b,rel,delta,average);
  191. if (rel > threshold)
  192. {
  193. //printf("rel = %g, threshold %g \n",rel,threshold);
  194. char details[200];
  195. sprintf(details,"diff (%g,%g), %g > %g",a,b,rel , threshold);
  196. throw (Error(RELATIVE_ERROR,nb,details));
  197. }
  198. }
  199. };
  200. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  201. void assert_relative_error(unsigned long nb,float16_t &a, float16_t &b, double threshold)
  202. {
  203. double rel,delta,average;
  204. delta=abs(a-b);
  205. average = (abs(a) + abs(b)) / 2.0f;
  206. if (average !=0)
  207. {
  208. rel = delta / average;
  209. //printf("%6.9f %6.9f %6.9f %g %g\n",a,b,rel,delta,average);
  210. if (rel > threshold)
  211. {
  212. //printf("rel = %g, threshold %g \n",rel,threshold);
  213. char details[200];
  214. sprintf(details,"diff (%g,%g), %g > %g",a,b,rel , threshold);
  215. throw (Error(RELATIVE_ERROR,nb,details));
  216. }
  217. }
  218. };
  219. #endif
  220. void assert_relative_error(unsigned long nb,AnyPattern<float64_t> &pa, AnyPattern<float64_t> &pb, double threshold)
  221. {
  222. ASSERT_NOT_EMPTY(pa);
  223. ASSERT_NOT_EMPTY(pb);
  224. if (pa.nbSamples() != pb.nbSamples())
  225. {
  226. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  227. }
  228. unsigned long i=0;
  229. float64_t *ptrA = pa.ptr();
  230. float64_t *ptrB = pb.ptr();
  231. char id[40];
  232. for(i=0; i < pa.nbSamples(); i++)
  233. {
  234. try
  235. {
  236. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  237. }
  238. catch(Error &err)
  239. {
  240. sprintf(id," (nb=%lu)",i);
  241. strcat(err.details,id);
  242. throw(err);
  243. }
  244. }
  245. };
  246. void assert_relative_error(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, double threshold)
  247. {
  248. ASSERT_NOT_EMPTY(pa);
  249. ASSERT_NOT_EMPTY(pb);
  250. if (pa.nbSamples() != pb.nbSamples())
  251. {
  252. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  253. }
  254. unsigned long i=0;
  255. float32_t *ptrA = pa.ptr();
  256. float32_t *ptrB = pb.ptr();
  257. char id[40];
  258. for(i=0; i < pa.nbSamples(); i++)
  259. {
  260. try
  261. {
  262. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  263. }
  264. catch(Error &err)
  265. {
  266. sprintf(id," (nb=%lu)",i);
  267. strcat(err.details,id);
  268. throw(err);
  269. }
  270. }
  271. };
  272. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  273. void assert_relative_error(unsigned long nb,AnyPattern<float16_t> &pa, AnyPattern<float16_t> &pb, double threshold)
  274. {
  275. ASSERT_NOT_EMPTY(pa);
  276. ASSERT_NOT_EMPTY(pb);
  277. if (pa.nbSamples() != pb.nbSamples())
  278. {
  279. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  280. }
  281. unsigned long i=0;
  282. float16_t *ptrA = pa.ptr();
  283. float16_t *ptrB = pb.ptr();
  284. char id[40];
  285. for(i=0; i < pa.nbSamples(); i++)
  286. {
  287. try
  288. {
  289. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  290. }
  291. catch(Error &err)
  292. {
  293. sprintf(id," (nb=%lu)",i);
  294. strcat(err.details,id);
  295. throw(err);
  296. }
  297. }
  298. };
  299. #endif
  300. void assert_close_error(unsigned long nb,float64_t &ref, float64_t &val, double absthreshold,double relthreshold)
  301. {
  302. if (abs(val - ref) > (absthreshold + relthreshold * abs(ref)))
  303. {
  304. char details[200];
  305. sprintf(details,"close error %g > %g: (val = %g, ref = %g)",abs(val - ref) , absthreshold + relthreshold * abs(ref),val,ref);
  306. throw (Error(CLOSE_ERROR,nb,details));
  307. }
  308. };
  309. void assert_close_error(unsigned long nb,AnyPattern<float64_t> &pref, AnyPattern<float64_t> &pval, double absthreshold,double relthreshold)
  310. {
  311. ASSERT_NOT_EMPTY(pref);
  312. ASSERT_NOT_EMPTY(pval);
  313. if (pref.nbSamples() != pval.nbSamples())
  314. {
  315. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  316. }
  317. unsigned long i=0;
  318. char id[40];
  319. float64_t *ptrA = pref.ptr();
  320. float64_t *ptrB = pval.ptr();
  321. for(i=0; i < pref.nbSamples(); i++)
  322. {
  323. try
  324. {
  325. assert_close_error(nb,ptrA[i],ptrB[i],absthreshold,relthreshold);
  326. }
  327. catch(Error &err)
  328. {
  329. sprintf(id," (nb=%lu)",i);
  330. strcat(err.details,id);
  331. throw(err);
  332. }
  333. }
  334. };
  335. void assert_close_error(unsigned long nb,float32_t &ref, float32_t &val, double absthreshold,double relthreshold)
  336. {
  337. if (abs(val - ref) > (absthreshold + relthreshold * abs(ref)))
  338. {
  339. char details[200];
  340. sprintf(details,"close error %g > %g: (val = %g, ref = %g)",abs(val - ref) , absthreshold + relthreshold * abs(ref),val,ref);
  341. throw (Error(CLOSE_ERROR,nb,details));
  342. }
  343. };
  344. void assert_close_error(unsigned long nb,AnyPattern<float32_t> &pref, AnyPattern<float32_t> &pval, double absthreshold,double relthreshold)
  345. {
  346. ASSERT_NOT_EMPTY(pref);
  347. ASSERT_NOT_EMPTY(pval);
  348. if (pref.nbSamples() != pval.nbSamples())
  349. {
  350. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  351. }
  352. unsigned long i=0;
  353. char id[40];
  354. float32_t *ptrA = pref.ptr();
  355. float32_t *ptrB = pval.ptr();
  356. for(i=0; i < pref.nbSamples(); i++)
  357. {
  358. try
  359. {
  360. assert_close_error(nb,ptrA[i],ptrB[i],absthreshold,relthreshold);
  361. }
  362. catch(Error &err)
  363. {
  364. sprintf(id," (nb=%lu)",i);
  365. strcat(err.details,id);
  366. throw(err);
  367. }
  368. }
  369. };
  370. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  371. void assert_close_error(unsigned long nb,float16_t &ref, float16_t &val, double absthreshold,double relthreshold)
  372. {
  373. if (abs((float)val - (float)ref) > (absthreshold + relthreshold * abs((float)ref)))
  374. {
  375. char details[200];
  376. sprintf(details,"close error %g > %g: (val = %g, ref = %g)",abs(val - ref) , absthreshold + relthreshold * abs(ref),val,ref);
  377. throw (Error(CLOSE_ERROR,nb,details));
  378. }
  379. };
  380. void assert_close_error(unsigned long nb,AnyPattern<float16_t> &pref, AnyPattern<float16_t> &pval, double absthreshold,double relthreshold)
  381. {
  382. ASSERT_NOT_EMPTY(pref);
  383. ASSERT_NOT_EMPTY(pval);
  384. if (pref.nbSamples() != pval.nbSamples())
  385. {
  386. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  387. }
  388. unsigned long i=0;
  389. char id[40];
  390. float16_t *ptrA = pref.ptr();
  391. float16_t *ptrB = pval.ptr();
  392. for(i=0; i < pref.nbSamples(); i++)
  393. {
  394. try
  395. {
  396. assert_close_error(nb,ptrA[i],ptrB[i],absthreshold,relthreshold);
  397. }
  398. catch(Error &err)
  399. {
  400. sprintf(id," (nb=%lu)",i);
  401. strcat(err.details,id);
  402. throw(err);
  403. }
  404. }
  405. };
  406. #endif
  407. /**
  408. * @brief Calculation of SNR
  409. * @param float* Pointer to the reference buffer
  410. * @param float* Pointer to the test buffer
  411. * @param uint32_t total number of samples
  412. * @return float SNR
  413. * The function calculates signal to noise ratio for the reference output
  414. * and test output
  415. */
  416. /* If NaN, force SNR to 0.0 to ensure test will fail */
  417. #define IFNANRETURNZERO(val)\
  418. if (isnan((val))) \
  419. { \
  420. return(0.0); \
  421. }
  422. #define IFINFINITERETURN(val,def)\
  423. if (isinf((val))) \
  424. { \
  425. if ((val) > 0) \
  426. { \
  427. return(def); \
  428. } \
  429. else \
  430. { \
  431. return(-def); \
  432. } \
  433. }
  434. float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize)
  435. {
  436. float EnergySignal = 0.0, EnergyError = 0.0;
  437. uint32_t i;
  438. float SNR;
  439. for (i = 0; i < buffSize; i++)
  440. {
  441. /* Checking for a NAN value in pRef array */
  442. IFNANRETURNZERO(pRef[i]);
  443. /* Checking for a NAN value in pTest array */
  444. IFNANRETURNZERO(pTest[i]);
  445. EnergySignal += pRef[i] * pRef[i];
  446. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  447. }
  448. /* Checking for a NAN value in EnergyError */
  449. IFNANRETURNZERO(EnergyError);
  450. SNR = 10 * log10f (EnergySignal / EnergyError);
  451. /* Checking for a NAN value in SNR */
  452. IFNANRETURNZERO(SNR);
  453. IFINFINITERETURN(SNR,100000.0);
  454. return (SNR);
  455. }
  456. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  457. float arm_snr_f16(float16_t *pRef, float16_t *pTest, uint32_t buffSize)
  458. {
  459. float EnergySignal = 0.0, EnergyError = 0.0;
  460. uint32_t i;
  461. float SNR;
  462. for (i = 0; i < buffSize; i++)
  463. {
  464. /* Checking for a NAN value in pRef array */
  465. IFNANRETURNZERO((float)pRef[i]);
  466. /* Checking for a NAN value in pTest array */
  467. IFNANRETURNZERO((float)pTest[i]);
  468. EnergySignal += pRef[i] * pRef[i];
  469. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  470. }
  471. /* Checking for a NAN value in EnergyError */
  472. IFNANRETURNZERO(EnergyError);
  473. SNR = 10 * log10f (EnergySignal / EnergyError);
  474. /* Checking for a NAN value in SNR */
  475. IFNANRETURNZERO(SNR);
  476. IFINFINITERETURN(SNR,100000.0);
  477. return (SNR);
  478. }
  479. #endif
  480. float arm_snr_q63(q63_t *pRef, q63_t *pTest, uint32_t buffSize)
  481. {
  482. double EnergySignal = 0.0, EnergyError = 0.0;
  483. uint32_t i;
  484. float SNR;
  485. double testVal,refVal;
  486. for (i = 0; i < buffSize; i++)
  487. {
  488. testVal = ((double)pTest[i]) / 9223372036854775808.0;
  489. refVal = ((double)pRef[i]) / 9223372036854775808.0;
  490. EnergySignal += refVal * refVal;
  491. EnergyError += (refVal - testVal) * (refVal - testVal);
  492. }
  493. SNR = 10 * log10 (EnergySignal / EnergyError);
  494. /* Checking for a NAN value in SNR */
  495. IFNANRETURNZERO(SNR);
  496. IFINFINITERETURN(SNR,100000.0);
  497. //printf("SNR = %f\n",SNR);
  498. return (SNR);
  499. }
  500. float arm_snr_q31(q31_t *pRef, q31_t *pTest, uint32_t buffSize)
  501. {
  502. float EnergySignal = 0.0, EnergyError = 0.0;
  503. uint32_t i;
  504. float SNR;
  505. float32_t testVal,refVal;
  506. for (i = 0; i < buffSize; i++)
  507. {
  508. testVal = ((float32_t)pTest[i]) / 2147483648.0f;
  509. refVal = ((float32_t)pRef[i]) / 2147483648.0f;
  510. EnergySignal += refVal * refVal;
  511. EnergyError += (refVal - testVal) * (refVal - testVal);
  512. }
  513. SNR = 10 * log10f (EnergySignal / EnergyError);
  514. /* Checking for a NAN value in SNR */
  515. IFNANRETURNZERO(SNR);
  516. IFINFINITERETURN(SNR,100000.0);
  517. //printf("SNR = %f\n",SNR);
  518. return (SNR);
  519. }
  520. float arm_snr_q15(q15_t *pRef, q15_t *pTest, uint32_t buffSize)
  521. {
  522. float EnergySignal = 0.0, EnergyError = 0.0;
  523. uint32_t i;
  524. float SNR;
  525. float32_t testVal,refVal;
  526. for (i = 0; i < buffSize; i++)
  527. {
  528. testVal = ((float32_t)pTest[i]) / 32768.0f;
  529. refVal = ((float32_t)pRef[i]) / 32768.0f;
  530. EnergySignal += refVal * refVal;
  531. EnergyError += (refVal - testVal) * (refVal - testVal);
  532. }
  533. SNR = 10 * log10f (EnergySignal / EnergyError);
  534. /* Checking for a NAN value in SNR */
  535. IFNANRETURNZERO(SNR);
  536. IFINFINITERETURN(SNR,100000.0);
  537. //printf("SNR = %f\n",SNR);
  538. return (SNR);
  539. }
  540. float arm_snr_q7(q7_t *pRef, q7_t *pTest, uint32_t buffSize)
  541. {
  542. float EnergySignal = 0.0, EnergyError = 0.0;
  543. uint32_t i;
  544. float SNR;
  545. float32_t testVal,refVal;
  546. for (i = 0; i < buffSize; i++)
  547. {
  548. testVal = ((float32_t)pTest[i]) / 128.0f;
  549. refVal = ((float32_t)pRef[i]) / 128.0f;
  550. EnergySignal += refVal * refVal;
  551. EnergyError += (refVal - testVal) * (refVal - testVal);
  552. }
  553. SNR = 10 * log10f (EnergySignal / EnergyError);
  554. IFNANRETURNZERO(SNR);
  555. IFINFINITERETURN(SNR,100000.0);
  556. return (SNR);
  557. }
  558. double arm_snr_f64(double *pRef, double *pTest, uint32_t buffSize)
  559. {
  560. double EnergySignal = 0.0, EnergyError = 0.0;
  561. uint32_t i;
  562. double SNR;
  563. for (i = 0; i < buffSize; i++)
  564. {
  565. /* Checking for a NAN value in pRef array */
  566. IFNANRETURNZERO(pRef[i]);
  567. /* Checking for a NAN value in pTest array */
  568. IFNANRETURNZERO(pTest[i]);
  569. EnergySignal += pRef[i] * pRef[i];
  570. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  571. }
  572. /* Checking for a NAN value in EnergyError */
  573. IFNANRETURNZERO(EnergyError);
  574. SNR = 10 * log10 (EnergySignal / EnergyError);
  575. /* Checking for a NAN value in SNR */
  576. IFNANRETURNZERO(SNR);
  577. IFINFINITERETURN(SNR,100000.0);
  578. return (SNR);
  579. }
  580. void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold)
  581. {
  582. float32_t snr;
  583. ASSERT_NOT_EMPTY(pa);
  584. ASSERT_NOT_EMPTY(pb);
  585. if (pa.nbSamples() != pb.nbSamples())
  586. {
  587. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  588. }
  589. float32_t *ptrA = pa.ptr();
  590. float32_t *ptrB = pb.ptr();
  591. snr = arm_snr_f32(ptrA, ptrB, pa.nbSamples());
  592. //printf("SNR = %f\n",snr);
  593. if (snr < threshold)
  594. {
  595. char details[200];
  596. sprintf(details,"SNR %g < %g",snr,threshold);
  597. throw (Error(SNR_ERROR,nb,details));
  598. }
  599. }
  600. void assert_snr_error(unsigned long nb,float32_t a,float32_t b, float32_t threshold)
  601. {
  602. float32_t snr;
  603. snr = arm_snr_f32(&a, &b, 1);
  604. //printf("SNR = %f\n",snr);
  605. if (snr < threshold)
  606. {
  607. char details[200];
  608. sprintf(details,"SNR %g < %g",snr,threshold);
  609. throw (Error(SNR_ERROR,nb,details));
  610. }
  611. }
  612. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  613. void assert_snr_error(unsigned long nb,AnyPattern<float16_t> &pa,AnyPattern<float16_t> &pb, float32_t threshold)
  614. {
  615. float32_t snr;
  616. ASSERT_NOT_EMPTY(pa);
  617. ASSERT_NOT_EMPTY(pb);
  618. if (pa.nbSamples() != pb.nbSamples())
  619. {
  620. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  621. }
  622. float16_t *ptrA = pa.ptr();
  623. float16_t *ptrB = pb.ptr();
  624. snr = arm_snr_f16(ptrA, ptrB, pa.nbSamples());
  625. //printf("SNR = %f\n",snr);
  626. if (snr < threshold)
  627. {
  628. char details[200];
  629. sprintf(details,"SNR %g < %g",snr,threshold);
  630. throw (Error(SNR_ERROR,nb,details));
  631. }
  632. }
  633. #endif
  634. #if !defined (__CC_ARM) && defined(ARM_FLOAT16_SUPPORTED)
  635. void assert_snr_error(unsigned long nb,float16_t a,float16_t b, float32_t threshold)
  636. {
  637. float32_t snr;
  638. snr = arm_snr_f16(&a, &b, 1);
  639. //printf("SNR = %f\n",snr);
  640. if (snr < threshold)
  641. {
  642. char details[200];
  643. sprintf(details,"SNR %g < %g",snr,threshold);
  644. throw (Error(SNR_ERROR,nb,details));
  645. }
  646. }
  647. #endif
  648. void assert_snr_error(unsigned long nb,AnyPattern<float64_t> &pa,AnyPattern<float64_t> &pb, float64_t threshold)
  649. {
  650. float64_t snr;
  651. ASSERT_NOT_EMPTY(pa);
  652. ASSERT_NOT_EMPTY(pb);
  653. if (pa.nbSamples() != pb.nbSamples())
  654. {
  655. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  656. }
  657. float64_t *ptrA = pa.ptr();
  658. float64_t *ptrB = pb.ptr();
  659. snr = arm_snr_f64(ptrA, ptrB, pa.nbSamples());
  660. //printf("SNR = %f\n",snr);
  661. if (snr < threshold)
  662. {
  663. char details[200];
  664. sprintf(details,"SNR %g < %g",snr,threshold);
  665. throw (Error(SNR_ERROR,nb,details));
  666. }
  667. }
  668. void assert_snr_error(unsigned long nb,float64_t a,float64_t b, float64_t threshold)
  669. {
  670. float64_t snr;
  671. snr = arm_snr_f64(&a, &b, 1);
  672. //printf("SNR = %f\n",snr);
  673. if (snr < threshold)
  674. {
  675. char details[200];
  676. sprintf(details,"SNR %g < %g",snr,threshold);
  677. throw (Error(SNR_ERROR,nb,details));
  678. }
  679. }
  680. void assert_snr_error(unsigned long nb,AnyPattern<q63_t> &pa,AnyPattern<q63_t> &pb, float32_t threshold)
  681. {
  682. float32_t snr;
  683. ASSERT_NOT_EMPTY(pa);
  684. ASSERT_NOT_EMPTY(pb);
  685. if (pa.nbSamples() != pb.nbSamples())
  686. {
  687. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  688. }
  689. q63_t *ptrA = pa.ptr();
  690. q63_t *ptrB = pb.ptr();
  691. snr = arm_snr_q63(ptrA, ptrB, pa.nbSamples());
  692. //printf("SNR = %f\n",snr);
  693. if (snr < threshold)
  694. {
  695. char details[200];
  696. sprintf(details,"SNR %g < %g",snr,threshold);
  697. throw (Error(SNR_ERROR,nb,details));
  698. }
  699. }
  700. void assert_snr_error(unsigned long nb,q63_t a,q63_t b, float32_t threshold)
  701. {
  702. float32_t snr;
  703. snr = arm_snr_q63(&a, &b, 1);
  704. //printf("SNR = %f\n",snr);
  705. if (snr < threshold)
  706. {
  707. char details[200];
  708. sprintf(details,"SNR %g < %g",snr,threshold);
  709. throw (Error(SNR_ERROR,nb,details));
  710. }
  711. }
  712. void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold)
  713. {
  714. float32_t snr;
  715. ASSERT_NOT_EMPTY(pa);
  716. ASSERT_NOT_EMPTY(pb);
  717. if (pa.nbSamples() != pb.nbSamples())
  718. {
  719. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  720. }
  721. q31_t *ptrA = pa.ptr();
  722. q31_t *ptrB = pb.ptr();
  723. snr = arm_snr_q31(ptrA, ptrB, pa.nbSamples());
  724. //printf("SNR = %f\n",snr);
  725. if (snr < threshold)
  726. {
  727. char details[200];
  728. sprintf(details,"SNR %g < %g",snr,threshold);
  729. throw (Error(SNR_ERROR,nb,details));
  730. }
  731. }
  732. void assert_snr_error(unsigned long nb,q31_t a,q31_t b, float32_t threshold)
  733. {
  734. float32_t snr;
  735. snr = arm_snr_q31(&a, &b, 1);
  736. if (snr < threshold)
  737. {
  738. char details[200];
  739. sprintf(details,"SNR %g < %g",snr,threshold);
  740. throw (Error(SNR_ERROR,nb,details));
  741. }
  742. }
  743. void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold)
  744. {
  745. float32_t snr;
  746. ASSERT_NOT_EMPTY(pa);
  747. ASSERT_NOT_EMPTY(pb);
  748. if (pa.nbSamples() != pb.nbSamples())
  749. {
  750. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  751. }
  752. q15_t *ptrA = pa.ptr();
  753. q15_t *ptrB = pb.ptr();
  754. snr = arm_snr_q15(ptrA, ptrB, pa.nbSamples());
  755. //printf("SNR = %f\n",snr);
  756. if (snr < threshold)
  757. {
  758. char details[200];
  759. sprintf(details,"SNR %g < %g",snr,threshold);
  760. throw (Error(SNR_ERROR,nb,details));
  761. }
  762. }
  763. void assert_snr_error(unsigned long nb,q15_t a,q15_t b, float32_t threshold)
  764. {
  765. float32_t snr;
  766. snr = arm_snr_q15(&a, &b, 1);
  767. //printf("SNR = %f\n",snr);
  768. if (snr < threshold)
  769. {
  770. char details[200];
  771. sprintf(details,"SNR %g < %g",snr,threshold);
  772. throw (Error(SNR_ERROR,nb,details));
  773. }
  774. }
  775. void assert_snr_error(unsigned long nb,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold)
  776. {
  777. float32_t snr;
  778. ASSERT_NOT_EMPTY(pa);
  779. ASSERT_NOT_EMPTY(pb);
  780. if (pa.nbSamples() != pb.nbSamples())
  781. {
  782. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  783. }
  784. q7_t *ptrA = pa.ptr();
  785. q7_t *ptrB = pb.ptr();
  786. snr = arm_snr_q7(ptrA, ptrB, pa.nbSamples());
  787. //printf("SNR = %f\n",snr);
  788. if (snr < threshold)
  789. {
  790. char details[200];
  791. sprintf(details,"SNR %g < %g",snr,threshold);
  792. throw (Error(SNR_ERROR,nb,details));
  793. }
  794. }
  795. void assert_snr_error(unsigned long nb,q7_t a,q7_t b, float32_t threshold)
  796. {
  797. float32_t snr;
  798. snr = arm_snr_q7(&a, &b, 1);
  799. //printf("SNR = %f\n",snr);
  800. if (snr < threshold)
  801. {
  802. char details[200];
  803. sprintf(details,"SNR %g < %g",snr,threshold);
  804. throw (Error(SNR_ERROR,nb,details));
  805. }
  806. }
  807. void assert_true(unsigned long nb,bool cond)
  808. {
  809. if (!cond)
  810. {
  811. throw (Error(BOOL_ERROR,nb));
  812. }
  813. }
  814. void assert_false(unsigned long nb,bool cond)
  815. {
  816. if (cond)
  817. {
  818. throw (Error(BOOL_ERROR,nb));
  819. }
  820. }
  821. }