Error.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. sprintf(details,"diff %lld > %lld (0x%016llX,0x%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 (0x%08X,0x%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 (0x%04X,0x%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 (0x%02X,0x%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 error %g > %g: (val = %g, ref = %g)",abs(val - ref) , absthreshold + relthreshold * abs(ref),val,ref);
  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 error %g > %g: (val = %g, ref = %g)",abs(val - ref) , absthreshold + relthreshold * abs(ref),val,ref);
  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 error %g > %g: (val = %g, ref = %g)",abs(val - ref) , absthreshold + relthreshold * abs(ref),val,ref);
  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,float64_t a,float64_t b, float64_t threshold)
  665. {
  666. float64_t snr;
  667. snr = arm_snr_f64(&a, &b, 1);
  668. //printf("SNR = %f\n",snr);
  669. if (snr < threshold)
  670. {
  671. char details[200];
  672. sprintf(details,"SNR %g < %g",snr,threshold);
  673. throw (Error(SNR_ERROR,nb,details));
  674. }
  675. }
  676. void assert_snr_error(unsigned long nb,AnyPattern<q63_t> &pa,AnyPattern<q63_t> &pb, float32_t threshold)
  677. {
  678. float32_t snr;
  679. ASSERT_NOT_EMPTY(pa);
  680. ASSERT_NOT_EMPTY(pb);
  681. if (pa.nbSamples() != pb.nbSamples())
  682. {
  683. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  684. }
  685. q63_t *ptrA = pa.ptr();
  686. q63_t *ptrB = pb.ptr();
  687. snr = arm_snr_q63(ptrA, ptrB, pa.nbSamples());
  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,q63_t a,q63_t b, float32_t threshold)
  697. {
  698. float32_t snr;
  699. snr = arm_snr_q63(&a, &b, 1);
  700. //printf("SNR = %f\n",snr);
  701. if (snr < threshold)
  702. {
  703. char details[200];
  704. sprintf(details,"SNR %g < %g",snr,threshold);
  705. throw (Error(SNR_ERROR,nb,details));
  706. }
  707. }
  708. void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold)
  709. {
  710. float32_t snr;
  711. ASSERT_NOT_EMPTY(pa);
  712. ASSERT_NOT_EMPTY(pb);
  713. if (pa.nbSamples() != pb.nbSamples())
  714. {
  715. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  716. }
  717. q31_t *ptrA = pa.ptr();
  718. q31_t *ptrB = pb.ptr();
  719. snr = arm_snr_q31(ptrA, ptrB, pa.nbSamples());
  720. //printf("SNR = %f\n",snr);
  721. if (snr < threshold)
  722. {
  723. char details[200];
  724. sprintf(details,"SNR %g < %g",snr,threshold);
  725. throw (Error(SNR_ERROR,nb,details));
  726. }
  727. }
  728. void assert_snr_error(unsigned long nb,q31_t a,q31_t b, float32_t threshold)
  729. {
  730. float32_t snr;
  731. snr = arm_snr_q31(&a, &b, 1);
  732. if (snr < threshold)
  733. {
  734. char details[200];
  735. sprintf(details,"SNR %g < %g",snr,threshold);
  736. throw (Error(SNR_ERROR,nb,details));
  737. }
  738. }
  739. void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold)
  740. {
  741. float32_t snr;
  742. ASSERT_NOT_EMPTY(pa);
  743. ASSERT_NOT_EMPTY(pb);
  744. if (pa.nbSamples() != pb.nbSamples())
  745. {
  746. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  747. }
  748. q15_t *ptrA = pa.ptr();
  749. q15_t *ptrB = pb.ptr();
  750. snr = arm_snr_q15(ptrA, ptrB, pa.nbSamples());
  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,q15_t a,q15_t b, float32_t threshold)
  760. {
  761. float32_t snr;
  762. snr = arm_snr_q15(&a, &b, 1);
  763. //printf("SNR = %f\n",snr);
  764. if (snr < threshold)
  765. {
  766. char details[200];
  767. sprintf(details,"SNR %g < %g",snr,threshold);
  768. throw (Error(SNR_ERROR,nb,details));
  769. }
  770. }
  771. void assert_snr_error(unsigned long nb,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold)
  772. {
  773. float32_t snr;
  774. ASSERT_NOT_EMPTY(pa);
  775. ASSERT_NOT_EMPTY(pb);
  776. if (pa.nbSamples() != pb.nbSamples())
  777. {
  778. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  779. }
  780. q7_t *ptrA = pa.ptr();
  781. q7_t *ptrB = pb.ptr();
  782. snr = arm_snr_q7(ptrA, ptrB, pa.nbSamples());
  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_snr_error(unsigned long nb,q7_t a,q7_t b, float32_t threshold)
  792. {
  793. float32_t snr;
  794. snr = arm_snr_q7(&a, &b, 1);
  795. //printf("SNR = %f\n",snr);
  796. if (snr < threshold)
  797. {
  798. char details[200];
  799. sprintf(details,"SNR %g < %g",snr,threshold);
  800. throw (Error(SNR_ERROR,nb,details));
  801. }
  802. }
  803. void assert_true(unsigned long nb,bool cond)
  804. {
  805. if (!cond)
  806. {
  807. throw (Error(BOOL_ERROR,nb));
  808. }
  809. }
  810. void assert_false(unsigned long nb,bool cond)
  811. {
  812. if (cond)
  813. {
  814. throw (Error(BOOL_ERROR,nb));
  815. }
  816. }
  817. }