Error.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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_relative_error(unsigned long nb,float64_t &a, float64_t &b, double threshold)
  130. {
  131. float64_t rel,delta,average;
  132. delta=abs(a-b);
  133. average = (abs(a) + abs(b)) / 2.0f;
  134. if (average !=0)
  135. {
  136. rel = delta / average;
  137. //printf("%6.9f %6.9f %6.9f %g %g\n",a,b,rel,delta,average);
  138. if (rel > threshold)
  139. {
  140. //printf("rel = %g, threshold %g \n",rel,threshold);
  141. char details[200];
  142. sprintf(details,"diff (%g,%g), %g > %g",a,b,rel , threshold);
  143. throw (Error(RELATIVE_ERROR,nb,details));
  144. }
  145. }
  146. };
  147. void assert_relative_error(unsigned long nb,float32_t &a, float32_t &b, double threshold)
  148. {
  149. double rel,delta,average;
  150. delta=abs(a-b);
  151. average = (abs(a) + abs(b)) / 2.0f;
  152. if (average !=0)
  153. {
  154. rel = delta / average;
  155. //printf("%6.9f %6.9f %6.9f %g %g\n",a,b,rel,delta,average);
  156. if (rel > threshold)
  157. {
  158. //printf("rel = %g, threshold %g \n",rel,threshold);
  159. char details[200];
  160. sprintf(details,"diff (%g,%g), %g > %g",a,b,rel , threshold);
  161. throw (Error(RELATIVE_ERROR,nb,details));
  162. }
  163. }
  164. };
  165. void assert_relative_error(unsigned long nb,AnyPattern<float64_t> &pa, AnyPattern<float64_t> &pb, double threshold)
  166. {
  167. ASSERT_NOT_EMPTY(pa);
  168. ASSERT_NOT_EMPTY(pb);
  169. if (pa.nbSamples() != pb.nbSamples())
  170. {
  171. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  172. }
  173. unsigned long i=0;
  174. float64_t *ptrA = pa.ptr();
  175. float64_t *ptrB = pb.ptr();
  176. char id[40];
  177. for(i=0; i < pa.nbSamples(); i++)
  178. {
  179. try
  180. {
  181. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  182. }
  183. catch(Error &err)
  184. {
  185. sprintf(id," (nb=%lu)",i+1);
  186. strcat(err.details,id);
  187. throw(err);
  188. }
  189. }
  190. };
  191. void assert_relative_error(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, double threshold)
  192. {
  193. ASSERT_NOT_EMPTY(pa);
  194. ASSERT_NOT_EMPTY(pb);
  195. if (pa.nbSamples() != pb.nbSamples())
  196. {
  197. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  198. }
  199. unsigned long i=0;
  200. float32_t *ptrA = pa.ptr();
  201. float32_t *ptrB = pb.ptr();
  202. char id[40];
  203. for(i=0; i < pa.nbSamples(); i++)
  204. {
  205. try
  206. {
  207. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  208. }
  209. catch(Error &err)
  210. {
  211. sprintf(id," (nb=%lu)",i+1);
  212. strcat(err.details,id);
  213. throw(err);
  214. }
  215. }
  216. };
  217. void assert_close_error(unsigned long nb,float64_t &ref, float64_t &val, double absthreshold,double relthreshold)
  218. {
  219. if (abs(val - ref) > (absthreshold + relthreshold * abs(ref)))
  220. {
  221. char details[200];
  222. sprintf(details,"close %g : abs=%g, rel=%g",abs(val - ref) , absthreshold,relthreshold);
  223. throw (Error(CLOSE_ERROR,nb,details));
  224. }
  225. };
  226. void assert_close_error(unsigned long nb,AnyPattern<float64_t> &pref, AnyPattern<float64_t> &pval, double absthreshold,double relthreshold)
  227. {
  228. ASSERT_NOT_EMPTY(pref);
  229. ASSERT_NOT_EMPTY(pval);
  230. if (pref.nbSamples() != pval.nbSamples())
  231. {
  232. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  233. }
  234. unsigned long i=0;
  235. char id[40];
  236. float64_t *ptrA = pref.ptr();
  237. float64_t *ptrB = pval.ptr();
  238. for(i=0; i < pref.nbSamples(); i++)
  239. {
  240. try
  241. {
  242. assert_close_error(nb,ptrA[i],ptrB[i],absthreshold,relthreshold);
  243. }
  244. catch(Error &err)
  245. {
  246. sprintf(id," (nb=%lu)",i+1);
  247. strcat(err.details,id);
  248. throw(err);
  249. }
  250. }
  251. };
  252. void assert_close_error(unsigned long nb,float32_t &ref, float32_t &val, double absthreshold,double relthreshold)
  253. {
  254. if (abs(val - ref) > (absthreshold + relthreshold * abs(ref)))
  255. {
  256. char details[200];
  257. sprintf(details,"close %g : abs=%g, rel=%g",abs(val - ref) , absthreshold,relthreshold);
  258. throw (Error(CLOSE_ERROR,nb,details));
  259. }
  260. };
  261. void assert_close_error(unsigned long nb,AnyPattern<float32_t> &pref, AnyPattern<float32_t> &pval, double absthreshold,double relthreshold)
  262. {
  263. ASSERT_NOT_EMPTY(pref);
  264. ASSERT_NOT_EMPTY(pval);
  265. if (pref.nbSamples() != pval.nbSamples())
  266. {
  267. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  268. }
  269. unsigned long i=0;
  270. char id[40];
  271. float32_t *ptrA = pref.ptr();
  272. float32_t *ptrB = pval.ptr();
  273. for(i=0; i < pref.nbSamples(); i++)
  274. {
  275. try
  276. {
  277. assert_close_error(nb,ptrA[i],ptrB[i],absthreshold,relthreshold);
  278. }
  279. catch(Error &err)
  280. {
  281. sprintf(id," (nb=%lu)",i+1);
  282. strcat(err.details,id);
  283. throw(err);
  284. }
  285. }
  286. };
  287. /**
  288. * @brief Calculation of SNR
  289. * @param float* Pointer to the reference buffer
  290. * @param float* Pointer to the test buffer
  291. * @param uint32_t total number of samples
  292. * @return float SNR
  293. * The function calculates signal to noise ratio for the reference output
  294. * and test output
  295. */
  296. /* If NaN, force SNR to 0.0 to ensure test will fail */
  297. #define IFNANRETURNZERO(val)\
  298. if (isnan((val))) \
  299. { \
  300. return(0.0); \
  301. }
  302. #define IFINFINITERETURN(val,def)\
  303. if (isinf((val))) \
  304. { \
  305. if ((val) > 0) \
  306. { \
  307. return(def); \
  308. } \
  309. else \
  310. { \
  311. return(-def); \
  312. } \
  313. }
  314. float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize)
  315. {
  316. float EnergySignal = 0.0, EnergyError = 0.0;
  317. uint32_t i;
  318. float SNR;
  319. for (i = 0; i < buffSize; i++)
  320. {
  321. /* Checking for a NAN value in pRef array */
  322. IFNANRETURNZERO(pRef[i]);
  323. /* Checking for a NAN value in pTest array */
  324. IFNANRETURNZERO(pTest[i]);
  325. EnergySignal += pRef[i] * pRef[i];
  326. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  327. }
  328. /* Checking for a NAN value in EnergyError */
  329. IFNANRETURNZERO(EnergyError);
  330. SNR = 10 * log10f (EnergySignal / EnergyError);
  331. /* Checking for a NAN value in SNR */
  332. IFNANRETURNZERO(SNR);
  333. IFINFINITERETURN(SNR,100000.0);
  334. return (SNR);
  335. }
  336. float arm_snr_q63(q63_t *pRef, q63_t *pTest, uint32_t buffSize)
  337. {
  338. double EnergySignal = 0.0, EnergyError = 0.0;
  339. uint32_t i;
  340. float SNR;
  341. double testVal,refVal;
  342. for (i = 0; i < buffSize; i++)
  343. {
  344. testVal = ((double)pTest[i]) / 9223372036854775808.0;
  345. refVal = ((double)pRef[i]) / 9223372036854775808.0;
  346. EnergySignal += refVal * refVal;
  347. EnergyError += (refVal - testVal) * (refVal - testVal);
  348. }
  349. SNR = 10 * log10 (EnergySignal / EnergyError);
  350. /* Checking for a NAN value in SNR */
  351. IFNANRETURNZERO(SNR);
  352. IFINFINITERETURN(SNR,100000.0);
  353. //printf("SNR = %f\n",SNR);
  354. return (SNR);
  355. }
  356. float arm_snr_q31(q31_t *pRef, q31_t *pTest, uint32_t buffSize)
  357. {
  358. float EnergySignal = 0.0, EnergyError = 0.0;
  359. uint32_t i;
  360. float SNR;
  361. float32_t testVal,refVal;
  362. for (i = 0; i < buffSize; i++)
  363. {
  364. testVal = ((float32_t)pTest[i]) / 2147483648.0f;
  365. refVal = ((float32_t)pRef[i]) / 2147483648.0f;
  366. EnergySignal += refVal * refVal;
  367. EnergyError += (refVal - testVal) * (refVal - testVal);
  368. }
  369. SNR = 10 * log10f (EnergySignal / EnergyError);
  370. /* Checking for a NAN value in SNR */
  371. IFNANRETURNZERO(SNR);
  372. IFINFINITERETURN(SNR,100000.0);
  373. //printf("SNR = %f\n",SNR);
  374. return (SNR);
  375. }
  376. float arm_snr_q15(q15_t *pRef, q15_t *pTest, uint32_t buffSize)
  377. {
  378. float EnergySignal = 0.0, EnergyError = 0.0;
  379. uint32_t i;
  380. float SNR;
  381. float32_t testVal,refVal;
  382. for (i = 0; i < buffSize; i++)
  383. {
  384. testVal = ((float32_t)pTest[i]) / 32768.0f;
  385. refVal = ((float32_t)pRef[i]) / 32768.0f;
  386. EnergySignal += refVal * refVal;
  387. EnergyError += (refVal - testVal) * (refVal - testVal);
  388. }
  389. SNR = 10 * log10f (EnergySignal / EnergyError);
  390. /* Checking for a NAN value in SNR */
  391. IFNANRETURNZERO(SNR);
  392. IFINFINITERETURN(SNR,100000.0);
  393. //printf("SNR = %f\n",SNR);
  394. return (SNR);
  395. }
  396. float arm_snr_q7(q7_t *pRef, q7_t *pTest, uint32_t buffSize)
  397. {
  398. float EnergySignal = 0.0, EnergyError = 0.0;
  399. uint32_t i;
  400. float SNR;
  401. float32_t testVal,refVal;
  402. for (i = 0; i < buffSize; i++)
  403. {
  404. testVal = ((float32_t)pTest[i]) / 128.0f;
  405. refVal = ((float32_t)pRef[i]) / 128.0f;
  406. EnergySignal += refVal * refVal;
  407. EnergyError += (refVal - testVal) * (refVal - testVal);
  408. }
  409. SNR = 10 * log10f (EnergySignal / EnergyError);
  410. IFNANRETURNZERO(SNR);
  411. IFINFINITERETURN(SNR,100000.0);
  412. return (SNR);
  413. }
  414. double arm_snr_f64(double *pRef, double *pTest, uint32_t buffSize)
  415. {
  416. double EnergySignal = 0.0, EnergyError = 0.0;
  417. uint32_t i;
  418. double SNR;
  419. for (i = 0; i < buffSize; i++)
  420. {
  421. /* Checking for a NAN value in pRef array */
  422. IFNANRETURNZERO(pRef[i]);
  423. /* Checking for a NAN value in pTest array */
  424. IFNANRETURNZERO(pTest[i]);
  425. EnergySignal += pRef[i] * pRef[i];
  426. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  427. }
  428. /* Checking for a NAN value in EnergyError */
  429. IFNANRETURNZERO(EnergyError);
  430. SNR = 10 * log10 (EnergySignal / EnergyError);
  431. /* Checking for a NAN value in SNR */
  432. IFNANRETURNZERO(SNR);
  433. IFINFINITERETURN(SNR,100000.0);
  434. return (SNR);
  435. }
  436. void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold)
  437. {
  438. float32_t snr;
  439. ASSERT_NOT_EMPTY(pa);
  440. ASSERT_NOT_EMPTY(pb);
  441. if (pa.nbSamples() != pb.nbSamples())
  442. {
  443. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  444. }
  445. float32_t *ptrA = pa.ptr();
  446. float32_t *ptrB = pb.ptr();
  447. snr = arm_snr_f32(ptrA, ptrB, pa.nbSamples());
  448. //printf("SNR = %f\n",snr);
  449. if (snr < threshold)
  450. {
  451. char details[200];
  452. sprintf(details,"SNR %g < %g",snr,threshold);
  453. throw (Error(SNR_ERROR,nb,details));
  454. }
  455. }
  456. void assert_snr_error(unsigned long nb,float32_t a,float32_t b, float32_t threshold)
  457. {
  458. float32_t snr;
  459. snr = arm_snr_f32(&a, &b, 1);
  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,AnyPattern<float64_t> &pa,AnyPattern<float64_t> &pb, float64_t threshold)
  469. {
  470. float64_t snr;
  471. ASSERT_NOT_EMPTY(pa);
  472. ASSERT_NOT_EMPTY(pb);
  473. if (pa.nbSamples() != pb.nbSamples())
  474. {
  475. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  476. }
  477. float64_t *ptrA = pa.ptr();
  478. float64_t *ptrB = pb.ptr();
  479. snr = arm_snr_f64(ptrA, ptrB, pa.nbSamples());
  480. //printf("SNR = %f\n",snr);
  481. if (snr < threshold)
  482. {
  483. char details[200];
  484. sprintf(details,"SNR %g < %g",snr,threshold);
  485. throw (Error(SNR_ERROR,nb,details));
  486. }
  487. }
  488. void assert_snr_error(unsigned long nb,AnyPattern<q63_t> &pa,AnyPattern<q63_t> &pb, float32_t threshold)
  489. {
  490. float32_t snr;
  491. ASSERT_NOT_EMPTY(pa);
  492. ASSERT_NOT_EMPTY(pb);
  493. if (pa.nbSamples() != pb.nbSamples())
  494. {
  495. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  496. }
  497. q63_t *ptrA = pa.ptr();
  498. q63_t *ptrB = pb.ptr();
  499. snr = arm_snr_q63(ptrA, ptrB, pa.nbSamples());
  500. //printf("SNR = %f\n",snr);
  501. if (snr < threshold)
  502. {
  503. char details[200];
  504. sprintf(details,"SNR %g < %g",snr,threshold);
  505. throw (Error(SNR_ERROR,nb,details));
  506. }
  507. }
  508. void assert_snr_error(unsigned long nb,q63_t a,q63_t b, float32_t threshold)
  509. {
  510. float32_t snr;
  511. snr = arm_snr_q63(&a, &b, 1);
  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,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold)
  521. {
  522. float32_t snr;
  523. ASSERT_NOT_EMPTY(pa);
  524. ASSERT_NOT_EMPTY(pb);
  525. if (pa.nbSamples() != pb.nbSamples())
  526. {
  527. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  528. }
  529. q31_t *ptrA = pa.ptr();
  530. q31_t *ptrB = pb.ptr();
  531. snr = arm_snr_q31(ptrA, ptrB, pa.nbSamples());
  532. //printf("SNR = %f\n",snr);
  533. if (snr < threshold)
  534. {
  535. char details[200];
  536. sprintf(details,"SNR %g < %g",snr,threshold);
  537. throw (Error(SNR_ERROR,nb,details));
  538. }
  539. }
  540. void assert_snr_error(unsigned long nb,q31_t a,q31_t b, float32_t threshold)
  541. {
  542. float32_t snr;
  543. snr = arm_snr_q31(&a, &b, 1);
  544. if (snr < threshold)
  545. {
  546. char details[200];
  547. sprintf(details,"SNR %g < %g",snr,threshold);
  548. throw (Error(SNR_ERROR,nb,details));
  549. }
  550. }
  551. void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold)
  552. {
  553. float32_t snr;
  554. ASSERT_NOT_EMPTY(pa);
  555. ASSERT_NOT_EMPTY(pb);
  556. if (pa.nbSamples() != pb.nbSamples())
  557. {
  558. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  559. }
  560. q15_t *ptrA = pa.ptr();
  561. q15_t *ptrB = pb.ptr();
  562. snr = arm_snr_q15(ptrA, ptrB, pa.nbSamples());
  563. //printf("SNR = %f\n",snr);
  564. if (snr < threshold)
  565. {
  566. char details[200];
  567. sprintf(details,"SNR %g < %g",snr,threshold);
  568. throw (Error(SNR_ERROR,nb,details));
  569. }
  570. }
  571. void assert_snr_error(unsigned long nb,q15_t a,q15_t b, float32_t threshold)
  572. {
  573. float32_t snr;
  574. snr = arm_snr_q15(&a, &b, 1);
  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,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold)
  584. {
  585. float32_t snr;
  586. ASSERT_NOT_EMPTY(pa);
  587. ASSERT_NOT_EMPTY(pb);
  588. if (pa.nbSamples() != pb.nbSamples())
  589. {
  590. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  591. }
  592. q7_t *ptrA = pa.ptr();
  593. q7_t *ptrB = pb.ptr();
  594. snr = arm_snr_q7(ptrA, ptrB, pa.nbSamples());
  595. //printf("SNR = %f\n",snr);
  596. if (snr < threshold)
  597. {
  598. char details[200];
  599. sprintf(details,"SNR %g < %g",snr,threshold);
  600. throw (Error(SNR_ERROR,nb,details));
  601. }
  602. }
  603. void assert_snr_error(unsigned long nb,q7_t a,q7_t b, float32_t threshold)
  604. {
  605. float32_t snr;
  606. snr = arm_snr_q7(&a, &b, 1);
  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_true(unsigned long nb,bool cond)
  616. {
  617. if (!cond)
  618. {
  619. throw (Error(BOOL_ERROR,nb));
  620. }
  621. }
  622. void assert_false(unsigned long nb,bool cond)
  623. {
  624. if (cond)
  625. {
  626. throw (Error(BOOL_ERROR,nb));
  627. }
  628. }
  629. }