distance_functions.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /******************************************************************************
  2. * @file distance_functions.h
  3. * @brief Public header file for CMSIS DSP Library
  4. * @version V1.10.0
  5. * @date 08 July 2021
  6. * Target Processor: Cortex-M and Cortex-A cores
  7. ******************************************************************************/
  8. /*
  9. * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. #ifndef _DISTANCE_FUNCTIONS_H_
  26. #define _DISTANCE_FUNCTIONS_H_
  27. #include "arm_math_types.h"
  28. #include "arm_math_memory.h"
  29. #include "dsp/none.h"
  30. #include "dsp/utils.h"
  31. #include "dsp/statistics_functions.h"
  32. #include "dsp/basic_math_functions.h"
  33. #include "dsp/fast_math_functions.h"
  34. #include "dsp/matrix_functions.h"
  35. #ifdef __cplusplus
  36. extern "C"
  37. {
  38. #endif
  39. /**
  40. * @defgroup groupDistance Distance Functions
  41. *
  42. * Distance functions for use with clustering algorithms.
  43. * There are distance functions for float vectors and boolean vectors.
  44. *
  45. */
  46. /* 6.14 bug */
  47. #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100) && (__ARMCC_VERSION < 6150001)
  48. __attribute__((weak)) float __powisf2(float a, int b);
  49. #endif
  50. /**
  51. * @brief Euclidean distance between two vectors
  52. * @param[in] pA First vector
  53. * @param[in] pB Second vector
  54. * @param[in] blockSize vector length
  55. * @return distance
  56. *
  57. */
  58. float32_t arm_euclidean_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize);
  59. /**
  60. * @brief Euclidean distance between two vectors
  61. * @param[in] pA First vector
  62. * @param[in] pB Second vector
  63. * @param[in] blockSize vector length
  64. * @return distance
  65. *
  66. */
  67. float64_t arm_euclidean_distance_f64(const float64_t *pA,const float64_t *pB, uint32_t blockSize);
  68. /**
  69. * @brief Bray-Curtis distance between two vectors
  70. * @param[in] pA First vector
  71. * @param[in] pB Second vector
  72. * @param[in] blockSize vector length
  73. * @return distance
  74. *
  75. */
  76. float32_t arm_braycurtis_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize);
  77. /**
  78. * @brief Canberra distance between two vectors
  79. *
  80. * This function may divide by zero when samples pA[i] and pB[i] are both zero.
  81. * The result of the computation will be correct. So the division per zero may be
  82. * ignored.
  83. *
  84. * @param[in] pA First vector
  85. * @param[in] pB Second vector
  86. * @param[in] blockSize vector length
  87. * @return distance
  88. *
  89. */
  90. float32_t arm_canberra_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize);
  91. /**
  92. * @brief Chebyshev distance between two vectors
  93. * @param[in] pA First vector
  94. * @param[in] pB Second vector
  95. * @param[in] blockSize vector length
  96. * @return distance
  97. *
  98. */
  99. float32_t arm_chebyshev_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize);
  100. /**
  101. * @brief Chebyshev distance between two vectors
  102. * @param[in] pA First vector
  103. * @param[in] pB Second vector
  104. * @param[in] blockSize vector length
  105. * @return distance
  106. *
  107. */
  108. float64_t arm_chebyshev_distance_f64(const float64_t *pA,const float64_t *pB, uint32_t blockSize);
  109. /**
  110. * @brief Cityblock (Manhattan) distance between two vectors
  111. * @param[in] pA First vector
  112. * @param[in] pB Second vector
  113. * @param[in] blockSize vector length
  114. * @return distance
  115. *
  116. */
  117. float32_t arm_cityblock_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize);
  118. /**
  119. * @brief Cityblock (Manhattan) distance between two vectors
  120. * @param[in] pA First vector
  121. * @param[in] pB Second vector
  122. * @param[in] blockSize vector length
  123. * @return distance
  124. *
  125. */
  126. float64_t arm_cityblock_distance_f64(const float64_t *pA,const float64_t *pB, uint32_t blockSize);
  127. /**
  128. * @brief Correlation distance between two vectors
  129. *
  130. * The input vectors are modified in place !
  131. *
  132. * @param[in] pA First vector
  133. * @param[in] pB Second vector
  134. * @param[in] blockSize vector length
  135. * @return distance
  136. *
  137. */
  138. float32_t arm_correlation_distance_f32(float32_t *pA,float32_t *pB, uint32_t blockSize);
  139. /**
  140. * @brief Cosine distance between two vectors
  141. *
  142. * @param[in] pA First vector
  143. * @param[in] pB Second vector
  144. * @param[in] blockSize vector length
  145. * @return distance
  146. *
  147. */
  148. float32_t arm_cosine_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize);
  149. /**
  150. * @brief Cosine distance between two vectors
  151. *
  152. * @param[in] pA First vector
  153. * @param[in] pB Second vector
  154. * @param[in] blockSize vector length
  155. * @return distance
  156. *
  157. */
  158. float64_t arm_cosine_distance_f64(const float64_t *pA,const float64_t *pB, uint32_t blockSize);
  159. /**
  160. * @brief Jensen-Shannon distance between two vectors
  161. *
  162. * This function is assuming that elements of second vector are > 0
  163. * and 0 only when the corresponding element of first vector is 0.
  164. * Otherwise the result of the computation does not make sense
  165. * and for speed reasons, the cases returning NaN or Infinity are not
  166. * managed.
  167. *
  168. * When the function is computing x log (x / y) with x 0 and y 0,
  169. * it will compute the right value (0) but a division per zero will occur
  170. * and shoudl be ignored in client code.
  171. *
  172. * @param[in] pA First vector
  173. * @param[in] pB Second vector
  174. * @param[in] blockSize vector length
  175. * @return distance
  176. *
  177. */
  178. float32_t arm_jensenshannon_distance_f32(const float32_t *pA,const float32_t *pB,uint32_t blockSize);
  179. /**
  180. * @brief Minkowski distance between two vectors
  181. *
  182. * @param[in] pA First vector
  183. * @param[in] pB Second vector
  184. * @param[in] n Norm order (>= 2)
  185. * @param[in] blockSize vector length
  186. * @return distance
  187. *
  188. */
  189. float32_t arm_minkowski_distance_f32(const float32_t *pA,const float32_t *pB, int32_t order, uint32_t blockSize);
  190. /**
  191. * @brief Dice distance between two vectors
  192. *
  193. * @param[in] pA First vector of packed booleans
  194. * @param[in] pB Second vector of packed booleans
  195. * @param[in] order Distance order
  196. * @param[in] blockSize Number of samples
  197. * @return distance
  198. *
  199. */
  200. float32_t arm_dice_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  201. /**
  202. * @brief Hamming distance between two vectors
  203. *
  204. * @param[in] pA First vector of packed booleans
  205. * @param[in] pB Second vector of packed booleans
  206. * @param[in] numberOfBools Number of booleans
  207. * @return distance
  208. *
  209. */
  210. float32_t arm_hamming_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  211. /**
  212. * @brief Jaccard distance between two vectors
  213. *
  214. * @param[in] pA First vector of packed booleans
  215. * @param[in] pB Second vector of packed booleans
  216. * @param[in] numberOfBools Number of booleans
  217. * @return distance
  218. *
  219. */
  220. float32_t arm_jaccard_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  221. /**
  222. * @brief Kulsinski distance between two vectors
  223. *
  224. * @param[in] pA First vector of packed booleans
  225. * @param[in] pB Second vector of packed booleans
  226. * @param[in] numberOfBools Number of booleans
  227. * @return distance
  228. *
  229. */
  230. float32_t arm_kulsinski_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  231. /**
  232. * @brief Roger Stanimoto distance between two vectors
  233. *
  234. * @param[in] pA First vector of packed booleans
  235. * @param[in] pB Second vector of packed booleans
  236. * @param[in] numberOfBools Number of booleans
  237. * @return distance
  238. *
  239. */
  240. float32_t arm_rogerstanimoto_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  241. /**
  242. * @brief Russell-Rao distance between two vectors
  243. *
  244. * @param[in] pA First vector of packed booleans
  245. * @param[in] pB Second vector of packed booleans
  246. * @param[in] numberOfBools Number of booleans
  247. * @return distance
  248. *
  249. */
  250. float32_t arm_russellrao_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  251. /**
  252. * @brief Sokal-Michener distance between two vectors
  253. *
  254. * @param[in] pA First vector of packed booleans
  255. * @param[in] pB Second vector of packed booleans
  256. * @param[in] numberOfBools Number of booleans
  257. * @return distance
  258. *
  259. */
  260. float32_t arm_sokalmichener_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  261. /**
  262. * @brief Sokal-Sneath distance between two vectors
  263. *
  264. * @param[in] pA First vector of packed booleans
  265. * @param[in] pB Second vector of packed booleans
  266. * @param[in] numberOfBools Number of booleans
  267. * @return distance
  268. *
  269. */
  270. float32_t arm_sokalsneath_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  271. /**
  272. * @brief Yule distance between two vectors
  273. *
  274. * @param[in] pA First vector of packed booleans
  275. * @param[in] pB Second vector of packed booleans
  276. * @param[in] numberOfBools Number of booleans
  277. * @return distance
  278. *
  279. */
  280. float32_t arm_yule_distance(const uint32_t *pA, const uint32_t *pB, uint32_t numberOfBools);
  281. typedef enum
  282. {
  283. ARM_DTW_SAKOE_CHIBA_WINDOW = 1,
  284. /*ARM_DTW_ITAKURA_WINDOW = 2,*/
  285. ARM_DTW_SLANTED_BAND_WINDOW = 3
  286. } arm_dtw_window;
  287. /**
  288. * @brief Window for dynamic time warping computation
  289. * @param[in] windowType Type of window
  290. * @param[in] windowSize Window size
  291. * @param[in,out] pWindow Window
  292. * @return Error if window type not recognized
  293. *
  294. */
  295. arm_status arm_dtw_init_window_q7(const arm_dtw_window windowType,
  296. const int32_t windowSize,
  297. arm_matrix_instance_q7 *pWindow);
  298. /**
  299. * @brief Dynamic Time Warping distance
  300. * @param[in] pDistance Distance matrix (Query rows * Template columns)
  301. * @param[in] pWindow Windowing (can be NULL if no windowing used)
  302. * @param[out] pDTW Temporary cost buffer (same size)
  303. * @param[out] distance Distance
  304. * @return Error in case no path can be found with window constraint
  305. *
  306. */
  307. arm_status arm_dtw_distance_f32(const arm_matrix_instance_f32 *pDistance,
  308. const arm_matrix_instance_q7 *pWindow,
  309. arm_matrix_instance_f32 *pDTW,
  310. float32_t *distance);
  311. /**
  312. * @brief Mapping between query and template
  313. * @param[in] pDTW Cost matrix (Query rows * Template columns)
  314. * @param[out] pPath Warping path in cost matrix 2*(nb rows + nb columns)
  315. * @param[out] pathLength Length of path in number of points
  316. *
  317. */
  318. void arm_dtw_path_f32(const arm_matrix_instance_f32 *pDTW,
  319. int16_t *pPath,
  320. uint32_t *pathLength);
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif /* ifndef _DISTANCE_FUNCTIONS_H_ */