Error.cpp 24 KB

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