Error.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. template <>
  66. void assert_near_equal(unsigned long nb,q63_t pa, q63_t pb, q63_t threshold)
  67. {
  68. if (abs(pa - pb) > threshold)
  69. {
  70. char details[200];
  71. sprintf(details,"diff %lld > %lld (%016llX,%016llX)",abs(pa - pb) , threshold,pa,pb);
  72. throw (Error(EQUAL_ERROR,nb,details));
  73. }
  74. };
  75. template <>
  76. void assert_near_equal(unsigned long nb,q31_t pa, q31_t pb, q31_t threshold)
  77. {
  78. if (abs(pa - pb) > threshold)
  79. {
  80. char details[200];
  81. sprintf(details,"diff %d > %d (%08X,%08X)",abs(pa - pb) , threshold,pa,pb);
  82. throw (Error(EQUAL_ERROR,nb,details));
  83. }
  84. };
  85. template <>
  86. void assert_near_equal(unsigned long nb,q15_t pa, q15_t pb, q15_t threshold)
  87. {
  88. if (abs(pa - pb) > threshold)
  89. {
  90. char details[200];
  91. sprintf(details,"diff %d > %d (%04X,%04X)",abs(pa - pb) , threshold,pa,pb);
  92. throw (Error(EQUAL_ERROR,nb,details));
  93. }
  94. };
  95. template <>
  96. void assert_near_equal(unsigned long nb,q7_t pa, q7_t pb, q7_t threshold)
  97. {
  98. if (abs(pa - pb) > threshold)
  99. {
  100. char details[200];
  101. sprintf(details,"diff %d > %d (%02X,%02X)",abs(pa - pb) , threshold,pa,pb);
  102. throw (Error(EQUAL_ERROR,nb,details));
  103. }
  104. };
  105. void assert_not_empty(unsigned long nb, AnyPattern<float64_t> &p)
  106. {
  107. assert_not_empty_generic(nb,p);
  108. }
  109. void assert_not_empty(unsigned long nb, AnyPattern<float32_t> &p)
  110. {
  111. assert_not_empty_generic(nb,p);
  112. }
  113. void assert_not_empty(unsigned long nb, AnyPattern<q63_t> &p)
  114. {
  115. assert_not_empty_generic(nb,p);
  116. }
  117. void assert_not_empty(unsigned long nb, AnyPattern<q31_t> &p)
  118. {
  119. assert_not_empty_generic(nb,p);
  120. }
  121. void assert_not_empty(unsigned long nb, AnyPattern<q15_t> &p)
  122. {
  123. assert_not_empty_generic(nb,p);
  124. }
  125. void assert_not_empty(unsigned long nb, AnyPattern<q7_t> &p)
  126. {
  127. assert_not_empty_generic(nb,p);
  128. }
  129. void assert_not_empty(unsigned long nb, AnyPattern<uint32_t> &p)
  130. {
  131. assert_not_empty_generic(nb,p);
  132. }
  133. void assert_not_empty(unsigned long nb, AnyPattern<uint16_t> &p)
  134. {
  135. assert_not_empty_generic(nb,p);
  136. }
  137. void assert_not_empty(unsigned long nb, AnyPattern<uint8_t> &p)
  138. {
  139. assert_not_empty_generic(nb,p);
  140. }
  141. void assert_relative_error(unsigned long nb,float64_t &a, float64_t &b, double threshold)
  142. {
  143. float64_t rel,delta,average;
  144. delta=abs(a-b);
  145. average = (abs(a) + abs(b)) / 2.0f;
  146. if (average !=0)
  147. {
  148. rel = delta / average;
  149. //printf("%6.9f %6.9f %6.9f %g %g\n",a,b,rel,delta,average);
  150. if (rel > threshold)
  151. {
  152. //printf("rel = %g, threshold %g \n",rel,threshold);
  153. char details[200];
  154. sprintf(details,"diff (%g,%g), %g > %g",a,b,rel , threshold);
  155. throw (Error(RELATIVE_ERROR,nb,details));
  156. }
  157. }
  158. };
  159. void assert_relative_error(unsigned long nb,float32_t &a, float32_t &b, double threshold)
  160. {
  161. double 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,AnyPattern<float64_t> &pa, AnyPattern<float64_t> &pb, double threshold)
  178. {
  179. ASSERT_NOT_EMPTY(pa);
  180. ASSERT_NOT_EMPTY(pb);
  181. if (pa.nbSamples() != pb.nbSamples())
  182. {
  183. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  184. }
  185. unsigned long i=0;
  186. float64_t *ptrA = pa.ptr();
  187. float64_t *ptrB = pb.ptr();
  188. char id[40];
  189. for(i=0; i < pa.nbSamples(); i++)
  190. {
  191. try
  192. {
  193. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  194. }
  195. catch(Error &err)
  196. {
  197. sprintf(id," (nb=%lu)",i+1);
  198. strcat(err.details,id);
  199. throw(err);
  200. }
  201. }
  202. };
  203. void assert_relative_error(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, double threshold)
  204. {
  205. ASSERT_NOT_EMPTY(pa);
  206. ASSERT_NOT_EMPTY(pb);
  207. if (pa.nbSamples() != pb.nbSamples())
  208. {
  209. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  210. }
  211. unsigned long i=0;
  212. float32_t *ptrA = pa.ptr();
  213. float32_t *ptrB = pb.ptr();
  214. char id[40];
  215. for(i=0; i < pa.nbSamples(); i++)
  216. {
  217. try
  218. {
  219. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  220. }
  221. catch(Error &err)
  222. {
  223. sprintf(id," (nb=%lu)",i+1);
  224. strcat(err.details,id);
  225. throw(err);
  226. }
  227. }
  228. };
  229. void assert_close_error(unsigned long nb,float64_t &ref, float64_t &val, double absthreshold,double relthreshold)
  230. {
  231. if (abs(val - ref) > (absthreshold + relthreshold * abs(ref)))
  232. {
  233. char details[200];
  234. sprintf(details,"close %g : abs=%g, rel=%g",abs(val - ref) , absthreshold,relthreshold);
  235. throw (Error(CLOSE_ERROR,nb,details));
  236. }
  237. };
  238. void assert_close_error(unsigned long nb,AnyPattern<float64_t> &pref, AnyPattern<float64_t> &pval, double absthreshold,double relthreshold)
  239. {
  240. ASSERT_NOT_EMPTY(pref);
  241. ASSERT_NOT_EMPTY(pval);
  242. if (pref.nbSamples() != pval.nbSamples())
  243. {
  244. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  245. }
  246. unsigned long i=0;
  247. char id[40];
  248. float64_t *ptrA = pref.ptr();
  249. float64_t *ptrB = pval.ptr();
  250. for(i=0; i < pref.nbSamples(); i++)
  251. {
  252. try
  253. {
  254. assert_close_error(nb,ptrA[i],ptrB[i],absthreshold,relthreshold);
  255. }
  256. catch(Error &err)
  257. {
  258. sprintf(id," (nb=%lu)",i+1);
  259. strcat(err.details,id);
  260. throw(err);
  261. }
  262. }
  263. };
  264. void assert_close_error(unsigned long nb,float32_t &ref, float32_t &val, double absthreshold,double relthreshold)
  265. {
  266. if (abs(val - ref) > (absthreshold + relthreshold * abs(ref)))
  267. {
  268. char details[200];
  269. sprintf(details,"close %g : abs=%g, rel=%g",abs(val - ref) , absthreshold,relthreshold);
  270. throw (Error(CLOSE_ERROR,nb,details));
  271. }
  272. };
  273. void assert_close_error(unsigned long nb,AnyPattern<float32_t> &pref, AnyPattern<float32_t> &pval, double absthreshold,double relthreshold)
  274. {
  275. ASSERT_NOT_EMPTY(pref);
  276. ASSERT_NOT_EMPTY(pval);
  277. if (pref.nbSamples() != pval.nbSamples())
  278. {
  279. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  280. }
  281. unsigned long i=0;
  282. char id[40];
  283. float32_t *ptrA = pref.ptr();
  284. float32_t *ptrB = pval.ptr();
  285. for(i=0; i < pref.nbSamples(); i++)
  286. {
  287. try
  288. {
  289. assert_close_error(nb,ptrA[i],ptrB[i],absthreshold,relthreshold);
  290. }
  291. catch(Error &err)
  292. {
  293. sprintf(id," (nb=%lu)",i+1);
  294. strcat(err.details,id);
  295. throw(err);
  296. }
  297. }
  298. };
  299. /**
  300. * @brief Calculation of SNR
  301. * @param float* Pointer to the reference buffer
  302. * @param float* Pointer to the test buffer
  303. * @param uint32_t total number of samples
  304. * @return float SNR
  305. * The function calculates signal to noise ratio for the reference output
  306. * and test output
  307. */
  308. /* If NaN, force SNR to 0.0 to ensure test will fail */
  309. #define IFNANRETURNZERO(val)\
  310. if (isnan((val))) \
  311. { \
  312. return(0.0); \
  313. }
  314. #define IFINFINITERETURN(val,def)\
  315. if (isinf((val))) \
  316. { \
  317. if ((val) > 0) \
  318. { \
  319. return(def); \
  320. } \
  321. else \
  322. { \
  323. return(-def); \
  324. } \
  325. }
  326. float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize)
  327. {
  328. float EnergySignal = 0.0, EnergyError = 0.0;
  329. uint32_t i;
  330. float SNR;
  331. for (i = 0; i < buffSize; i++)
  332. {
  333. /* Checking for a NAN value in pRef array */
  334. IFNANRETURNZERO(pRef[i]);
  335. /* Checking for a NAN value in pTest array */
  336. IFNANRETURNZERO(pTest[i]);
  337. EnergySignal += pRef[i] * pRef[i];
  338. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  339. }
  340. /* Checking for a NAN value in EnergyError */
  341. IFNANRETURNZERO(EnergyError);
  342. SNR = 10 * log10f (EnergySignal / EnergyError);
  343. /* Checking for a NAN value in SNR */
  344. IFNANRETURNZERO(SNR);
  345. IFINFINITERETURN(SNR,100000.0);
  346. return (SNR);
  347. }
  348. float arm_snr_q63(q63_t *pRef, q63_t *pTest, uint32_t buffSize)
  349. {
  350. double EnergySignal = 0.0, EnergyError = 0.0;
  351. uint32_t i;
  352. float SNR;
  353. double testVal,refVal;
  354. for (i = 0; i < buffSize; i++)
  355. {
  356. testVal = ((double)pTest[i]) / 9223372036854775808.0;
  357. refVal = ((double)pRef[i]) / 9223372036854775808.0;
  358. EnergySignal += refVal * refVal;
  359. EnergyError += (refVal - testVal) * (refVal - testVal);
  360. }
  361. SNR = 10 * log10 (EnergySignal / EnergyError);
  362. /* Checking for a NAN value in SNR */
  363. IFNANRETURNZERO(SNR);
  364. IFINFINITERETURN(SNR,100000.0);
  365. //printf("SNR = %f\n",SNR);
  366. return (SNR);
  367. }
  368. float arm_snr_q31(q31_t *pRef, q31_t *pTest, uint32_t buffSize)
  369. {
  370. float EnergySignal = 0.0, EnergyError = 0.0;
  371. uint32_t i;
  372. float SNR;
  373. float32_t testVal,refVal;
  374. for (i = 0; i < buffSize; i++)
  375. {
  376. testVal = ((float32_t)pTest[i]) / 2147483648.0f;
  377. refVal = ((float32_t)pRef[i]) / 2147483648.0f;
  378. EnergySignal += refVal * refVal;
  379. EnergyError += (refVal - testVal) * (refVal - testVal);
  380. }
  381. SNR = 10 * log10f (EnergySignal / EnergyError);
  382. /* Checking for a NAN value in SNR */
  383. IFNANRETURNZERO(SNR);
  384. IFINFINITERETURN(SNR,100000.0);
  385. //printf("SNR = %f\n",SNR);
  386. return (SNR);
  387. }
  388. float arm_snr_q15(q15_t *pRef, q15_t *pTest, uint32_t buffSize)
  389. {
  390. float EnergySignal = 0.0, EnergyError = 0.0;
  391. uint32_t i;
  392. float SNR;
  393. float32_t testVal,refVal;
  394. for (i = 0; i < buffSize; i++)
  395. {
  396. testVal = ((float32_t)pTest[i]) / 32768.0f;
  397. refVal = ((float32_t)pRef[i]) / 32768.0f;
  398. EnergySignal += refVal * refVal;
  399. EnergyError += (refVal - testVal) * (refVal - testVal);
  400. }
  401. SNR = 10 * log10f (EnergySignal / EnergyError);
  402. /* Checking for a NAN value in SNR */
  403. IFNANRETURNZERO(SNR);
  404. IFINFINITERETURN(SNR,100000.0);
  405. //printf("SNR = %f\n",SNR);
  406. return (SNR);
  407. }
  408. float arm_snr_q7(q7_t *pRef, q7_t *pTest, uint32_t buffSize)
  409. {
  410. float EnergySignal = 0.0, EnergyError = 0.0;
  411. uint32_t i;
  412. float SNR;
  413. float32_t testVal,refVal;
  414. for (i = 0; i < buffSize; i++)
  415. {
  416. testVal = ((float32_t)pTest[i]) / 128.0f;
  417. refVal = ((float32_t)pRef[i]) / 128.0f;
  418. EnergySignal += refVal * refVal;
  419. EnergyError += (refVal - testVal) * (refVal - testVal);
  420. }
  421. SNR = 10 * log10f (EnergySignal / EnergyError);
  422. IFNANRETURNZERO(SNR);
  423. IFINFINITERETURN(SNR,100000.0);
  424. return (SNR);
  425. }
  426. double arm_snr_f64(double *pRef, double *pTest, uint32_t buffSize)
  427. {
  428. double EnergySignal = 0.0, EnergyError = 0.0;
  429. uint32_t i;
  430. double SNR;
  431. for (i = 0; i < buffSize; i++)
  432. {
  433. /* Checking for a NAN value in pRef array */
  434. IFNANRETURNZERO(pRef[i]);
  435. /* Checking for a NAN value in pTest array */
  436. IFNANRETURNZERO(pTest[i]);
  437. EnergySignal += pRef[i] * pRef[i];
  438. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  439. }
  440. /* Checking for a NAN value in EnergyError */
  441. IFNANRETURNZERO(EnergyError);
  442. SNR = 10 * log10 (EnergySignal / EnergyError);
  443. /* Checking for a NAN value in SNR */
  444. IFNANRETURNZERO(SNR);
  445. IFINFINITERETURN(SNR,100000.0);
  446. return (SNR);
  447. }
  448. void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold)
  449. {
  450. float32_t snr;
  451. ASSERT_NOT_EMPTY(pa);
  452. ASSERT_NOT_EMPTY(pb);
  453. if (pa.nbSamples() != pb.nbSamples())
  454. {
  455. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  456. }
  457. float32_t *ptrA = pa.ptr();
  458. float32_t *ptrB = pb.ptr();
  459. snr = arm_snr_f32(ptrA, ptrB, pa.nbSamples());
  460. //printf("SNR = %f\n",snr);
  461. if (snr < threshold)
  462. {
  463. char details[200];
  464. sprintf(details,"SNR %g < %g",snr,threshold);
  465. throw (Error(SNR_ERROR,nb,details));
  466. }
  467. }
  468. void assert_snr_error(unsigned long nb,float32_t a,float32_t b, float32_t threshold)
  469. {
  470. float32_t snr;
  471. snr = arm_snr_f32(&a, &b, 1);
  472. //printf("SNR = %f\n",snr);
  473. if (snr < threshold)
  474. {
  475. char details[200];
  476. sprintf(details,"SNR %g < %g",snr,threshold);
  477. throw (Error(SNR_ERROR,nb,details));
  478. }
  479. }
  480. void assert_snr_error(unsigned long nb,AnyPattern<float64_t> &pa,AnyPattern<float64_t> &pb, float64_t threshold)
  481. {
  482. float64_t snr;
  483. ASSERT_NOT_EMPTY(pa);
  484. ASSERT_NOT_EMPTY(pb);
  485. if (pa.nbSamples() != pb.nbSamples())
  486. {
  487. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  488. }
  489. float64_t *ptrA = pa.ptr();
  490. float64_t *ptrB = pb.ptr();
  491. snr = arm_snr_f64(ptrA, ptrB, pa.nbSamples());
  492. //printf("SNR = %f\n",snr);
  493. if (snr < threshold)
  494. {
  495. char details[200];
  496. sprintf(details,"SNR %g < %g",snr,threshold);
  497. throw (Error(SNR_ERROR,nb,details));
  498. }
  499. }
  500. void assert_snr_error(unsigned long nb,AnyPattern<q63_t> &pa,AnyPattern<q63_t> &pb, float32_t threshold)
  501. {
  502. float32_t snr;
  503. ASSERT_NOT_EMPTY(pa);
  504. ASSERT_NOT_EMPTY(pb);
  505. if (pa.nbSamples() != pb.nbSamples())
  506. {
  507. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  508. }
  509. q63_t *ptrA = pa.ptr();
  510. q63_t *ptrB = pb.ptr();
  511. snr = arm_snr_q63(ptrA, ptrB, pa.nbSamples());
  512. //printf("SNR = %f\n",snr);
  513. if (snr < threshold)
  514. {
  515. char details[200];
  516. sprintf(details,"SNR %g < %g",snr,threshold);
  517. throw (Error(SNR_ERROR,nb,details));
  518. }
  519. }
  520. void assert_snr_error(unsigned long nb,q63_t a,q63_t b, float32_t threshold)
  521. {
  522. float32_t snr;
  523. snr = arm_snr_q63(&a, &b, 1);
  524. //printf("SNR = %f\n",snr);
  525. if (snr < threshold)
  526. {
  527. char details[200];
  528. sprintf(details,"SNR %g < %g",snr,threshold);
  529. throw (Error(SNR_ERROR,nb,details));
  530. }
  531. }
  532. void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold)
  533. {
  534. float32_t snr;
  535. ASSERT_NOT_EMPTY(pa);
  536. ASSERT_NOT_EMPTY(pb);
  537. if (pa.nbSamples() != pb.nbSamples())
  538. {
  539. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  540. }
  541. q31_t *ptrA = pa.ptr();
  542. q31_t *ptrB = pb.ptr();
  543. snr = arm_snr_q31(ptrA, ptrB, pa.nbSamples());
  544. //printf("SNR = %f\n",snr);
  545. if (snr < threshold)
  546. {
  547. char details[200];
  548. sprintf(details,"SNR %g < %g",snr,threshold);
  549. throw (Error(SNR_ERROR,nb,details));
  550. }
  551. }
  552. void assert_snr_error(unsigned long nb,q31_t a,q31_t b, float32_t threshold)
  553. {
  554. float32_t snr;
  555. snr = arm_snr_q31(&a, &b, 1);
  556. if (snr < threshold)
  557. {
  558. char details[200];
  559. sprintf(details,"SNR %g < %g",snr,threshold);
  560. throw (Error(SNR_ERROR,nb,details));
  561. }
  562. }
  563. void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold)
  564. {
  565. float32_t snr;
  566. ASSERT_NOT_EMPTY(pa);
  567. ASSERT_NOT_EMPTY(pb);
  568. if (pa.nbSamples() != pb.nbSamples())
  569. {
  570. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  571. }
  572. q15_t *ptrA = pa.ptr();
  573. q15_t *ptrB = pb.ptr();
  574. snr = arm_snr_q15(ptrA, ptrB, pa.nbSamples());
  575. //printf("SNR = %f\n",snr);
  576. if (snr < threshold)
  577. {
  578. char details[200];
  579. sprintf(details,"SNR %g < %g",snr,threshold);
  580. throw (Error(SNR_ERROR,nb,details));
  581. }
  582. }
  583. void assert_snr_error(unsigned long nb,q15_t a,q15_t b, float32_t threshold)
  584. {
  585. float32_t snr;
  586. snr = arm_snr_q15(&a, &b, 1);
  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,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold)
  596. {
  597. float32_t snr;
  598. ASSERT_NOT_EMPTY(pa);
  599. ASSERT_NOT_EMPTY(pb);
  600. if (pa.nbSamples() != pb.nbSamples())
  601. {
  602. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  603. }
  604. q7_t *ptrA = pa.ptr();
  605. q7_t *ptrB = pb.ptr();
  606. snr = arm_snr_q7(ptrA, ptrB, pa.nbSamples());
  607. //printf("SNR = %f\n",snr);
  608. if (snr < threshold)
  609. {
  610. char details[200];
  611. sprintf(details,"SNR %g < %g",snr,threshold);
  612. throw (Error(SNR_ERROR,nb,details));
  613. }
  614. }
  615. void assert_snr_error(unsigned long nb,q7_t a,q7_t b, float32_t threshold)
  616. {
  617. float32_t snr;
  618. snr = arm_snr_q7(&a, &b, 1);
  619. //printf("SNR = %f\n",snr);
  620. if (snr < threshold)
  621. {
  622. char details[200];
  623. sprintf(details,"SNR %g < %g",snr,threshold);
  624. throw (Error(SNR_ERROR,nb,details));
  625. }
  626. }
  627. void assert_true(unsigned long nb,bool cond)
  628. {
  629. if (!cond)
  630. {
  631. throw (Error(BOOL_ERROR,nb));
  632. }
  633. }
  634. void assert_false(unsigned long nb,bool cond)
  635. {
  636. if (cond)
  637. {
  638. throw (Error(BOOL_ERROR,nb));
  639. }
  640. }
  641. }