arm_dtw_path_f32.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dtw_path_f32.c
  4. * Description: Warping path
  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-2022 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. #define E(MAT,R,C) \
  32. (*((MAT)->pData + (MAT)->numCols*(R) + (C)))
  33. /**
  34. @addtogroup DTW
  35. @{
  36. */
  37. /**
  38. * @brief Mapping between query and template
  39. * @param[in] pDTW Cost matrix (Query rows * Template columns)
  40. * @param[out] pPath Warping path in cost matrix 2*(nb rows + nb columns)
  41. * @param[out] pathLength Length of path in number of points
  42. *
  43. * @par Warping path
  44. *
  45. * The warping path has length which is at most
  46. * 2*(query length + template length) in float.
  47. * 2 because it is a list of coordinates :
  48. * (query index, template index) coordinate.
  49. *
  50. * The buffer pPath must be big enough to contain
  51. * the warping path.
  52. *
  53. * pathLength is the number of points in
  54. * the returned path. The resturned path
  55. * may be smaller than query + template.
  56. *
  57. */
  58. void arm_dtw_path_f32(const arm_matrix_instance_f32 *pDTW,
  59. int16_t *pPath,
  60. uint32_t *pathLength)
  61. {
  62. int q,t;
  63. float32_t temp;
  64. *pathLength = 0;
  65. q=pDTW->numRows-1;
  66. t=pDTW->numCols-1;
  67. while((q>0) || (t>0))
  68. {
  69. int p=-1;
  70. float32_t current=F32_MAX;
  71. if (q>0)
  72. {
  73. temp = E(pDTW,q-1,t);
  74. if (temp<current)
  75. {
  76. current=temp;
  77. p=2;
  78. }
  79. }
  80. if (t>0)
  81. {
  82. temp = E(pDTW,q,t-1);
  83. if (temp<current)
  84. {
  85. current=temp;
  86. p=0;
  87. }
  88. }
  89. if ((q>0) && (t>0))
  90. {
  91. temp = E(pDTW,q-1,t-1);
  92. if (temp<current)
  93. {
  94. current=temp;
  95. p=1;
  96. }
  97. }
  98. pPath[2 * (*pathLength)] = q;
  99. pPath[2 * (*pathLength) + 1] = t;
  100. *pathLength = *pathLength + 1;
  101. switch(p)
  102. {
  103. case 0:
  104. t = t-1;
  105. break;
  106. case 1:
  107. t=t-1;
  108. q=q-1;
  109. break;
  110. case 2:
  111. q=q-1;
  112. break;
  113. }
  114. }
  115. pPath[2 * (*pathLength)] = 0;
  116. pPath[2 * (*pathLength) + 1] = 0;
  117. *pathLength = *pathLength + 1;
  118. /* Reverse the path */
  119. int16_t *fh,*sh;
  120. fh = pPath;
  121. sh = pPath + 2* (*pathLength)-2;
  122. int halfLength = (*pathLength)>>1;
  123. for(int i = 0; i< halfLength; i++)
  124. {
  125. temp = fh[0];
  126. fh[0] = sh[0];
  127. sh[0] = temp;
  128. temp = fh[1];
  129. fh[1] = sh[1];
  130. sh[1] = temp;
  131. fh += 2;
  132. sh -= 2;
  133. }
  134. }
  135. /**
  136. * @} end of DTW group
  137. */