Error.cpp 18 KB

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