arm_dtw_init_window_q7.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <stdlib.h>
  32. /**
  33. @addtogroup DTW
  34. @{
  35. */
  36. /**
  37. * @brief Window for dynamic time warping computation
  38. * @param[in] windowType Type of window
  39. * @param[in] windowSize Window size
  40. * @param[in,out] pWindow Window
  41. * @return Error if window type not recognized
  42. *
  43. * @par Windowing matrix
  44. * The window matrix will contain 1 for the
  45. * position which are accepted and 0 for the
  46. * positions which are rejected.
  47. *
  48. * The input matrix must already contain a buffer
  49. * and the number of rows (query length) and columns
  50. * (template length) must be initialized.
  51. * The function will fill the matrix with 0 and 1.
  52. *
  53. */
  54. arm_status arm_dtw_init_window_q7(const arm_dtw_window windowType,
  55. const int32_t windowSize,
  56. arm_matrix_instance_q7 *pWindow)
  57. {
  58. const int32_t queryLength = pWindow -> numRows;
  59. const int32_t templateLength = pWindow -> numCols;
  60. switch(windowType)
  61. {
  62. case ARM_DTW_SAKOE_CHIBA_WINDOW:
  63. {
  64. for(int32_t q = 0; q < queryLength; q++)
  65. {
  66. for(int32_t t = 0; t < templateLength; t++)
  67. {
  68. pWindow->pData[templateLength*q + t] = (q7_t)(abs(q-t) <= windowSize);
  69. }
  70. }
  71. }
  72. break;
  73. /*
  74. case ARM_DTW_ITAKURA_WINDOW:
  75. {
  76. for(int32_t q = 0; q < queryLength; q++)
  77. {
  78. for(int32_t t = 0; t < templateLength; t++)
  79. {
  80. pWindow->pData[templateLength*q + t] = (q7_t)(
  81. (t < 2 * q) &&
  82. (q <= 2 * t) &&
  83. (q >= queryLength - 1 - 2 * (templateLength - t)) &&
  84. (t > templateLength - 1 - 2 * (queryLength - q)));
  85. }
  86. }
  87. }
  88. break;
  89. */
  90. case ARM_DTW_SLANTED_BAND_WINDOW:
  91. {
  92. for(int32_t q = 0; q < queryLength; q++)
  93. {
  94. for(int32_t t = 0; t < templateLength; t++)
  95. {
  96. float32_t diag = (1.0f * q * templateLength / queryLength);
  97. pWindow->pData[templateLength*q + t] = (q7_t)(fabsf((float32_t)t - diag) <= (float32_t)windowSize);
  98. }
  99. }
  100. }
  101. break;
  102. default:
  103. return(ARM_MATH_ARGUMENT_ERROR);
  104. }
  105. return(ARM_MATH_SUCCESS);
  106. }
  107. /**
  108. * @} end of DTW group
  109. */