distance_functions.h 11 KB

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