Error.cpp 23 KB

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