arm_dtw_distance_f32.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_braycurtis_distance_f32.c
  4. * Description: Bray-Curtis 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 "dsp/matrix_utils.h"
  30. #include <limits.h>
  31. #include <math.h>
  32. #define E(MAT,R,C) \
  33. (*((MAT)->pData + (MAT)->numCols*(R) + (C)))
  34. #define WIN(R,C) \
  35. ((pWindow == NULL) ? 1 : \
  36. ((*((pWindow)->pData + (pWindow)->numCols*(R) + (C)))==1))
  37. /**
  38. @ingroup FloatDist
  39. */
  40. /**
  41. @defgroup DTW Dynamic Time Warping Distance
  42. Dynamic Time Warping Distance.
  43. This is not really a distance since triangular inequality is
  44. not respected.
  45. The step pattern used is symmetric2.
  46. Future versions of this function will provide more customization options.
  47. */
  48. /**
  49. @addtogroup DTW
  50. @{
  51. */
  52. /**
  53. * @brief Dynamic Time Warping distance
  54. * @param[in] pDistance Distance matrix (Query rows * Template columns)
  55. * @param[in] pWindow Windowing matrix (can be NULL if no windowing used)
  56. * @param[out] pDTW Temporary cost buffer (same size)
  57. * @param[out] distance Distance
  58. * @return ARM_MATH_ARGUMENT_ERROR in case no path can be found with window constraint
  59. *
  60. * @par Windowing matrix
  61. *
  62. * The windowing matrix is used to impose some
  63. * constraints on the search for a path.
  64. * The algorithm will run faster (smaller search
  65. * path) but may not be able to find a solution.
  66. *
  67. * The distance matrix must be initialized only
  68. * where the windowing matrix is containing 1.
  69. * Thus, use of a window also decreases the number
  70. * of distances which must be computed.
  71. */
  72. arm_status arm_dtw_distance_f32(const arm_matrix_instance_f32 *pDistance,
  73. const arm_matrix_instance_q7 *pWindow,
  74. arm_matrix_instance_f32 *pDTW,
  75. float32_t *distance)
  76. {
  77. const uint32_t queryLength = pDistance -> numRows;
  78. const uint32_t templateLength = pDistance -> numCols;
  79. float32_t result;
  80. float32_t *temp = pDTW->pData;
  81. for(uint32_t q=0 ; q < queryLength; q++)
  82. {
  83. for(uint32_t t= 0; t < templateLength; t++)
  84. {
  85. *temp++ = F32_MAX;
  86. }
  87. }
  88. pDTW->pData[0] = E(pDistance,0,0);
  89. for(uint32_t q = 1; q < queryLength; q++)
  90. {
  91. if (!WIN(q,0))
  92. {
  93. continue;
  94. }
  95. E(pDTW,q,0) = E(pDTW,q-1,0) + E(pDistance,q,0);
  96. }
  97. for(uint32_t t = 1; t < templateLength; t++)
  98. {
  99. if (!WIN(0,t))
  100. {
  101. continue;
  102. }
  103. E(pDTW,0,t) = E(pDTW,0,t-1) + E(pDistance,0,t);
  104. }
  105. for(uint32_t q = 1; q < queryLength; q++)
  106. {
  107. for(uint32_t t = 1; t < templateLength; t++)
  108. {
  109. if (!WIN(q,t))
  110. {
  111. continue;
  112. }
  113. E(pDTW,q,t) =
  114. MIN(E(pDTW,q-1,t-1) + 2.0f * E(pDistance,q,t),
  115. MIN(E(pDTW,q,t-1) + E(pDistance,q,t),
  116. E(pDTW,q-1,t) + E(pDistance,q,t)));
  117. }
  118. }
  119. if (E(pDTW,queryLength-1,templateLength-1) == F32_MAX)
  120. {
  121. return(ARM_MATH_ARGUMENT_ERROR);
  122. }
  123. result = E(pDTW,queryLength-1,templateLength-1);
  124. result = result / (queryLength + templateLength);
  125. *distance = result;
  126. return(ARM_MATH_SUCCESS);
  127. }
  128. /**
  129. * @} end of DTW group
  130. */