Error.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "Error.h"
  29. namespace Client {
  30. template <>
  31. void assert_near_equal(unsigned long nb,float32_t pa, float32_t pb, float32_t threshold)
  32. {
  33. if (fabs(pa - pb) > threshold)
  34. {
  35. throw (Error(EQUAL_ERROR,nb));
  36. }
  37. };
  38. template <>
  39. void assert_near_equal(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, float32_t threshold)
  40. {
  41. if (pa.nbSamples() != pb.nbSamples())
  42. {
  43. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  44. }
  45. unsigned long i=0;
  46. float32_t *ptrA = pa.ptr();
  47. float32_t *ptrB = pb.ptr();
  48. for(i=0; i < pa.nbSamples(); i++)
  49. {
  50. if (fabs(ptrA[i] - ptrB[i]) > threshold)
  51. {
  52. throw (Error(NEAR_EQUAL_ERROR,nb));
  53. }
  54. }
  55. };
  56. void assert_relative_error(unsigned long nb,float32_t &a, float32_t &b, float32_t threshold)
  57. {
  58. float32_t rel,delta,average;
  59. delta=abs(a-b);
  60. average = (abs(a) + abs(b)) / 2.0f;
  61. if (average !=0)
  62. {
  63. rel = delta / average;
  64. if (rel > threshold)
  65. {
  66. throw (Error(RELATIVE_ERROR,nb));
  67. }
  68. }
  69. };
  70. void assert_relative_error(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, float32_t threshold)
  71. {
  72. if (pa.nbSamples() != pb.nbSamples())
  73. {
  74. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  75. }
  76. unsigned long i=0;
  77. float32_t *ptrA = pa.ptr();
  78. float32_t *ptrB = pb.ptr();
  79. for(i=0; i < pa.nbSamples(); i++)
  80. {
  81. assert_relative_error(nb,ptrA[i],ptrB[i],threshold);
  82. }
  83. };
  84. /**
  85. * @brief Caluclation of SNR
  86. * @param float* Pointer to the reference buffer
  87. * @param float* Pointer to the test buffer
  88. * @param uint32_t total number of samples
  89. * @return float SNR
  90. * The function calculates signal to noise ratio for the reference output
  91. * and test output
  92. */
  93. /* If NaN, force SNR to 0.0 to ensure test will fail */
  94. #define IFNANRETURNZERO(val)\
  95. if (isnan((val))) \
  96. { \
  97. return(0.0); \
  98. }
  99. #define IFINFINITERETURN(val,def)\
  100. if (isinf((val))) \
  101. { \
  102. return(def); \
  103. }
  104. float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize)
  105. {
  106. float EnergySignal = 0.0, EnergyError = 0.0;
  107. uint32_t i;
  108. float SNR;
  109. for (i = 0; i < buffSize; i++)
  110. {
  111. /* Checking for a NAN value in pRef array */
  112. IFNANRETURNZERO(pRef[i]);
  113. /* Checking for a NAN value in pTest array */
  114. IFNANRETURNZERO(pTest[i]);
  115. EnergySignal += pRef[i] * pRef[i];
  116. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  117. }
  118. /* Checking for a NAN value in EnergyError */
  119. IFNANRETURNZERO(EnergyError);
  120. SNR = 10 * log10f (EnergySignal / EnergyError);
  121. /* Checking for a NAN value in SNR */
  122. IFNANRETURNZERO(SNR);
  123. IFINFINITERETURN(SNR,100000.0);
  124. return (SNR);
  125. }
  126. float arm_snr_q31(q31_t *pRef, q31_t *pTest, uint32_t buffSize)
  127. {
  128. float EnergySignal = 0.0, EnergyError = 0.0;
  129. uint32_t i;
  130. float SNR;
  131. float32_t testVal,refVal;
  132. for (i = 0; i < buffSize; i++)
  133. {
  134. testVal = ((float32_t)pTest[i]) / 2147483648.0f;
  135. refVal = ((float32_t)pRef[i]) / 2147483648.0f;
  136. EnergySignal += refVal * refVal;
  137. EnergyError += (refVal - testVal) * (refVal - testVal);
  138. }
  139. SNR = 10 * log10f (EnergySignal / EnergyError);
  140. /* Checking for a NAN value in SNR */
  141. IFNANRETURNZERO(SNR);
  142. IFINFINITERETURN(SNR,100000.0);
  143. //printf("SNR = %f\n",SNR);
  144. return (SNR);
  145. }
  146. float arm_snr_q15(q15_t *pRef, q15_t *pTest, uint32_t buffSize)
  147. {
  148. float EnergySignal = 0.0, EnergyError = 0.0;
  149. uint32_t i;
  150. float SNR;
  151. float32_t testVal,refVal;
  152. for (i = 0; i < buffSize; i++)
  153. {
  154. testVal = ((float32_t)pTest[i]) / 32768.0f;
  155. refVal = ((float32_t)pRef[i]) / 32768.0f;
  156. EnergySignal += refVal * refVal;
  157. EnergyError += (refVal - testVal) * (refVal - testVal);
  158. }
  159. SNR = 10 * log10f (EnergySignal / EnergyError);
  160. /* Checking for a NAN value in SNR */
  161. IFNANRETURNZERO(SNR);
  162. IFINFINITERETURN(SNR,100000.0);
  163. //printf("SNR = %f\n",SNR);
  164. return (SNR);
  165. }
  166. float arm_snr_q7(q7_t *pRef, q7_t *pTest, uint32_t buffSize)
  167. {
  168. float EnergySignal = 0.0, EnergyError = 0.0;
  169. uint32_t i;
  170. float SNR;
  171. float32_t testVal,refVal;
  172. for (i = 0; i < buffSize; i++)
  173. {
  174. testVal = ((float32_t)pTest[i]) / 128.0f;
  175. refVal = ((float32_t)pRef[i]) / 128.0f;
  176. EnergySignal += refVal * refVal;
  177. EnergyError += (refVal - testVal) * (refVal - testVal);
  178. }
  179. SNR = 10 * log10f (EnergySignal / EnergyError);
  180. IFNANRETURNZERO(SNR);
  181. IFINFINITERETURN(SNR,100000.0);
  182. return (SNR);
  183. }
  184. double arm_snr_f64(double *pRef, double *pTest, uint32_t buffSize)
  185. {
  186. double EnergySignal = 0.0, EnergyError = 0.0;
  187. uint32_t i;
  188. double SNR;
  189. for (i = 0; i < buffSize; i++)
  190. {
  191. /* Checking for a NAN value in pRef array */
  192. IFNANRETURNZERO(pRef[i]);
  193. /* Checking for a NAN value in pTest array */
  194. IFNANRETURNZERO(pTest[i]);
  195. EnergySignal += pRef[i] * pRef[i];
  196. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  197. }
  198. /* Checking for a NAN value in EnergyError */
  199. IFNANRETURNZERO(EnergyError);
  200. SNR = 10 * log10 (EnergySignal / EnergyError);
  201. /* Checking for a NAN value in SNR */
  202. IFNANRETURNZERO(SNR);
  203. IFINFINITERETURN(SNR,100000.0);
  204. return (SNR);
  205. }
  206. void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold)
  207. {
  208. float32_t snr;
  209. if (pa.nbSamples() != pb.nbSamples())
  210. {
  211. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  212. }
  213. float32_t *ptrA = pa.ptr();
  214. float32_t *ptrB = pb.ptr();
  215. snr = arm_snr_f32(ptrA, ptrB, pa.nbSamples());
  216. if (snr < threshold)
  217. {
  218. throw (Error(SNR_ERROR,nb));
  219. }
  220. }
  221. void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold)
  222. {
  223. float32_t snr;
  224. if (pa.nbSamples() != pb.nbSamples())
  225. {
  226. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  227. }
  228. q31_t *ptrA = pa.ptr();
  229. q31_t *ptrB = pb.ptr();
  230. snr = arm_snr_q31(ptrA, ptrB, pa.nbSamples());
  231. if (snr < threshold)
  232. {
  233. throw (Error(SNR_ERROR,nb));
  234. }
  235. }
  236. void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold)
  237. {
  238. float32_t snr;
  239. if (pa.nbSamples() != pb.nbSamples())
  240. {
  241. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  242. }
  243. q15_t *ptrA = pa.ptr();
  244. q15_t *ptrB = pb.ptr();
  245. snr = arm_snr_q15(ptrA, ptrB, pa.nbSamples());
  246. //printf("SNR = %f\n",snr);
  247. if (snr < threshold)
  248. {
  249. throw (Error(SNR_ERROR,nb));
  250. }
  251. }
  252. void assert_snr_error(unsigned long nb,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold)
  253. {
  254. float32_t snr;
  255. if (pa.nbSamples() != pb.nbSamples())
  256. {
  257. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  258. }
  259. q7_t *ptrA = pa.ptr();
  260. q7_t *ptrB = pb.ptr();
  261. snr = arm_snr_q7(ptrA, ptrB, pa.nbSamples());
  262. //printf("SNR = %f\n",snr);
  263. if (snr < threshold)
  264. {
  265. throw (Error(SNR_ERROR,nb));
  266. }
  267. }
  268. void assert_true(unsigned long nb,bool cond)
  269. {
  270. if (!cond)
  271. {
  272. throw (Error(BOOL_ERROR,nb));
  273. }
  274. }
  275. void assert_false(unsigned long nb,bool cond)
  276. {
  277. if (cond)
  278. {
  279. throw (Error(BOOL_ERROR,nb));
  280. }
  281. }
  282. }