25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

248 satır
5.9 KiB

  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_jensenshannon_distance_f32.c
  4. * Description: Jensen-Shannon distance between two vectors
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/distance_functions.h"
  29. #include <limits.h>
  30. #include <math.h>
  31. /**
  32. @addtogroup JensenShannon
  33. @{
  34. */
  35. #if !defined(ARM_MATH_MVEF) || defined(ARM_MATH_AUTOVECTORIZE)
  36. /// @private
  37. __STATIC_INLINE float32_t rel_entr(float32_t x, float32_t y)
  38. {
  39. return (x * logf(x / y));
  40. }
  41. #endif
  42. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  43. #include "arm_helium_utils.h"
  44. #include "arm_vec_math.h"
  45. float32_t arm_jensenshannon_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  46. {
  47. uint32_t blkCnt;
  48. float32_t tmp;
  49. f32x4_t a, b, t, tmpV, accumV;
  50. accumV = vdupq_n_f32(0.0f);
  51. blkCnt = blockSize >> 2;
  52. while (blkCnt > 0U) {
  53. a = vld1q(pA);
  54. b = vld1q(pB);
  55. t = vaddq(a, b);
  56. t = vmulq(t, 0.5f);
  57. tmpV = vmulq(a, vrecip_medprec_f32(t));
  58. tmpV = vlogq_f32(tmpV);
  59. accumV = vfmaq(accumV, a, tmpV);
  60. tmpV = vmulq_f32(b, vrecip_medprec_f32(t));
  61. tmpV = vlogq_f32(tmpV);
  62. accumV = vfmaq(accumV, b, tmpV);
  63. pA += 4;
  64. pB += 4;
  65. blkCnt--;
  66. }
  67. /*
  68. * tail
  69. * (will be merged thru tail predication)
  70. */
  71. blkCnt = blockSize & 3;
  72. if (blkCnt > 0U) {
  73. mve_pred16_t p0 = vctp32q(blkCnt);
  74. a = vldrwq_z_f32(pA, p0);
  75. b = vldrwq_z_f32(pB, p0);
  76. t = vaddq(a, b);
  77. t = vmulq(t, 0.5f);
  78. tmpV = vmulq_f32(a, vrecip_medprec_f32(t));
  79. tmpV = vlogq_f32(tmpV);
  80. accumV = vfmaq_m_f32(accumV, a, tmpV, p0);
  81. tmpV = vmulq_f32(b, vrecip_medprec_f32(t));
  82. tmpV = vlogq_f32(tmpV);
  83. accumV = vfmaq_m_f32(accumV, b, tmpV, p0);
  84. }
  85. arm_sqrt_f32(vecAddAcrossF32Mve(accumV) / 2.0f, &tmp);
  86. return (tmp);
  87. }
  88. #else
  89. #if defined(ARM_MATH_NEON)
  90. #include "NEMath.h"
  91. /**
  92. * @brief Jensen-Shannon distance between two vectors
  93. *
  94. * This function is assuming that elements of second vector are > 0
  95. * and 0 only when the corresponding element of first vector is 0.
  96. * Otherwise the result of the computation does not make sense
  97. * and for speed reasons, the cases returning NaN or Infinity are not
  98. * managed.
  99. *
  100. * When the function is computing x log (x / y) with x == 0 and y == 0,
  101. * it will compute the right result (0) but a division by zero will occur
  102. * and should be ignored in client code.
  103. *
  104. * @param[in] pA First vector
  105. * @param[in] pB Second vector
  106. * @param[in] blockSize vector length
  107. * @return distance
  108. *
  109. */
  110. float32_t arm_jensenshannon_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  111. {
  112. float32_t accum, result, tmp,a,b;
  113. uint32_t blkCnt;
  114. float32x4_t aV,bV,t, tmpV, accumV;
  115. float32x2_t accumV2;
  116. accum = 0.0f;
  117. accumV = vdupq_n_f32(0.0f);
  118. blkCnt = blockSize >> 2;
  119. while(blkCnt > 0)
  120. {
  121. aV = vld1q_f32(pA);
  122. bV = vld1q_f32(pB);
  123. t = vaddq_f32(aV,bV);
  124. t = vmulq_n_f32(t, 0.5f);
  125. tmpV = vmulq_f32(aV, vinvq_f32(t));
  126. tmpV = vlogq_f32(tmpV);
  127. accumV = vmlaq_f32(accumV, aV, tmpV);
  128. tmpV = vmulq_f32(bV, vinvq_f32(t));
  129. tmpV = vlogq_f32(tmpV);
  130. accumV = vmlaq_f32(accumV, bV, tmpV);
  131. pA += 4;
  132. pB += 4;
  133. blkCnt --;
  134. }
  135. accumV2 = vpadd_f32(vget_low_f32(accumV),vget_high_f32(accumV));
  136. accum = vget_lane_f32(accumV2, 0) + vget_lane_f32(accumV2, 1);
  137. blkCnt = blockSize & 3;
  138. while(blkCnt > 0)
  139. {
  140. a = *pA;
  141. b = *pB;
  142. tmp = (a + b) / 2.0f;
  143. accum += rel_entr(a, tmp);
  144. accum += rel_entr(b, tmp);
  145. pA++;
  146. pB++;
  147. blkCnt --;
  148. }
  149. arm_sqrt_f32(accum/2.0f, &result);
  150. return(result);
  151. }
  152. #else
  153. /**
  154. * @brief Jensen-Shannon distance between two vectors
  155. *
  156. * This function is assuming that elements of second vector are > 0
  157. * and 0 only when the corresponding element of first vector is 0.
  158. * Otherwise the result of the computation does not make sense
  159. * and for speed reasons, the cases returning NaN or Infinity are not
  160. * managed.
  161. *
  162. * When the function is computing x log (x / y) with x == 0 and y == 0,
  163. * it will compute the right result (0) but a division by zero will occur
  164. * and should be ignored in client code.
  165. *
  166. * @param[in] pA First vector
  167. * @param[in] pB Second vector
  168. * @param[in] blockSize vector length
  169. * @return distance
  170. *
  171. */
  172. float32_t arm_jensenshannon_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  173. {
  174. float32_t left, right,sum, result, tmp;
  175. uint32_t i;
  176. left = 0.0f;
  177. right = 0.0f;
  178. for(i=0; i < blockSize; i++)
  179. {
  180. tmp = (pA[i] + pB[i]) / 2.0f;
  181. left += rel_entr(pA[i], tmp);
  182. right += rel_entr(pB[i], tmp);
  183. }
  184. sum = left + right;
  185. arm_sqrt_f32(sum/2.0f, &result);
  186. return(result);
  187. }
  188. #endif
  189. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  190. /**
  191. * @} end of JensenShannon group
  192. */