Error.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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.0;
  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. SNR functions below are just computing the error noise.
  86. Signal power needs to be computed.
  87. */
  88. /**
  89. * @brief Caluclation of SNR
  90. * @param float* Pointer to the reference buffer
  91. * @param float* Pointer to the test buffer
  92. * @param uint32_t total number of samples
  93. * @return float SNR
  94. * The function Caluclates signal to noise ratio for the reference output
  95. * and test output
  96. */
  97. float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize)
  98. {
  99. float EnergySignal = 0.0, EnergyError = 0.0;
  100. uint32_t i;
  101. float SNR;
  102. int temp;
  103. int *test;
  104. for (i = 0; i < buffSize; i++)
  105. {
  106. /* Checking for a NAN value in pRef array */
  107. test = (int *)(&pRef[i]);
  108. temp = *test;
  109. if (temp == 0x7FC00000)
  110. {
  111. return(100000.0);
  112. }
  113. /* Checking for a NAN value in pTest array */
  114. test = (int *)(&pTest[i]);
  115. temp = *test;
  116. if (temp == 0x7FC00000)
  117. {
  118. return(100000.0);
  119. }
  120. EnergySignal += pRef[i] * pRef[i];
  121. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  122. }
  123. /* Checking for a NAN value in EnergyError */
  124. test = (int *)(&EnergyError);
  125. temp = *test;
  126. if (temp == 0x7FC00000)
  127. {
  128. return(100000.0);
  129. }
  130. SNR = 10 * log10f (EnergySignal / EnergyError);
  131. /* Checking for a NAN value in SNR */
  132. test = (int *)(&SNR);
  133. temp = *test;
  134. if (temp == 0x7FC00000)
  135. {
  136. return(100000.0);
  137. }
  138. return (SNR);
  139. }
  140. float arm_snr_q31(q31_t *pRef, q31_t *pTest, uint32_t buffSize)
  141. {
  142. float EnergySignal = 0.0, EnergyError = 0.0;
  143. uint32_t i;
  144. float SNR;
  145. int temp;
  146. float32_t test,ref;
  147. for (i = 0; i < buffSize; i++)
  148. {
  149. test = (float32_t)pTest[i] / 2147483648.0f;
  150. ref = (float32_t)pRef[i] / 2147483648.0f;
  151. EnergySignal += pRef[i] * pRef[i];
  152. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  153. }
  154. SNR = 10 * log10f (EnergySignal / EnergyError);
  155. return (SNR);
  156. }
  157. float arm_snr_q15(q15_t *pRef, q15_t *pTest, uint32_t buffSize)
  158. {
  159. float EnergySignal = 0.0, EnergyError = 0.0;
  160. uint32_t i;
  161. float SNR;
  162. int temp;
  163. float32_t test,ref;
  164. for (i = 0; i < buffSize; i++)
  165. {
  166. test = (float32_t)pTest[i] / 32768.0f;
  167. ref = (float32_t)pRef[i] / 32768.0f;
  168. EnergySignal += pRef[i] * pRef[i];
  169. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  170. }
  171. SNR = 10 * log10f (EnergySignal / EnergyError);
  172. return (SNR);
  173. }
  174. float arm_snr_q7(q7_t *pRef, q7_t *pTest, uint32_t buffSize)
  175. {
  176. float EnergySignal = 0.0, EnergyError = 0.0;
  177. uint32_t i;
  178. float SNR;
  179. int temp;
  180. float32_t test,ref;
  181. for (i = 0; i < buffSize; i++)
  182. {
  183. test = (float32_t)pTest[i] / 128.0f;
  184. ref = (float32_t)pRef[i] / 128.0f;
  185. EnergySignal += pRef[i] * pRef[i];
  186. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  187. }
  188. SNR = 10 * log10f (EnergySignal / EnergyError);
  189. return (SNR);
  190. }
  191. double arm_snr_f64(double *pRef, double *pTest, uint32_t buffSize)
  192. {
  193. double EnergySignal = 0.0, EnergyError = 0.0;
  194. uint32_t i;
  195. double SNR;
  196. int temp;
  197. int *test;
  198. for (i = 0; i < buffSize; i++)
  199. {
  200. /* Checking for a NAN value in pRef array */
  201. test = (int *)(&pRef[i]);
  202. temp = *test;
  203. if (temp == 0x7FC00000)
  204. {
  205. return(100000.0);
  206. }
  207. /* Checking for a NAN value in pTest array */
  208. test = (int *)(&pTest[i]);
  209. temp = *test;
  210. if (temp == 0x7FC00000)
  211. {
  212. return(100000.0);
  213. }
  214. EnergySignal += pRef[i] * pRef[i];
  215. EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
  216. }
  217. /* Checking for a NAN value in EnergyError */
  218. test = (int *)(&EnergyError);
  219. temp = *test;
  220. if (temp == 0x7FC00000)
  221. {
  222. return(100000.0);
  223. }
  224. SNR = 10 * log10 (EnergySignal / EnergyError);
  225. /* Checking for a NAN value in SNR */
  226. test = (int *)(&SNR);
  227. temp = *test;
  228. if (temp == 0x7FC00000)
  229. {
  230. return(10000.0);
  231. }
  232. return (SNR);
  233. }
  234. void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold)
  235. {
  236. float32_t snr;
  237. if (pa.nbSamples() != pb.nbSamples())
  238. {
  239. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  240. }
  241. float32_t *ptrA = pa.ptr();
  242. float32_t *ptrB = pb.ptr();
  243. snr = arm_snr_f32(ptrA, ptrB, pa.nbSamples());
  244. if (snr < threshold)
  245. {
  246. throw (Error(SNR_ERROR,nb));
  247. }
  248. }
  249. void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold)
  250. {
  251. float32_t snr;
  252. if (pa.nbSamples() != pb.nbSamples())
  253. {
  254. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  255. }
  256. q31_t *ptrA = pa.ptr();
  257. q31_t *ptrB = pb.ptr();
  258. snr = arm_snr_q31(ptrA, ptrB, pa.nbSamples());
  259. if (snr < threshold)
  260. {
  261. throw (Error(SNR_ERROR,nb));
  262. }
  263. }
  264. void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold)
  265. {
  266. float32_t snr;
  267. if (pa.nbSamples() != pb.nbSamples())
  268. {
  269. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  270. }
  271. q15_t *ptrA = pa.ptr();
  272. q15_t *ptrB = pb.ptr();
  273. snr = arm_snr_q15(ptrA, ptrB, pa.nbSamples());
  274. if (snr < threshold)
  275. {
  276. throw (Error(SNR_ERROR,nb));
  277. }
  278. }
  279. void assert_snr_error(unsigned long nb,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold)
  280. {
  281. float32_t snr;
  282. if (pa.nbSamples() != pb.nbSamples())
  283. {
  284. throw (Error(DIFFERENT_LENGTH_ERROR,nb));
  285. }
  286. q7_t *ptrA = pa.ptr();
  287. q7_t *ptrB = pb.ptr();
  288. snr = arm_snr_q7(ptrA, ptrB, pa.nbSamples());
  289. if (snr < threshold)
  290. {
  291. throw (Error(SNR_ERROR,nb));
  292. }
  293. }
  294. void assert_true(unsigned long nb,bool cond)
  295. {
  296. if (!cond)
  297. {
  298. throw (Error(BOOL_ERROR,nb));
  299. }
  300. }
  301. void assert_false(unsigned long nb,bool cond)
  302. {
  303. if (cond)
  304. {
  305. throw (Error(BOOL_ERROR,nb));
  306. }
  307. }
  308. }