arm_spline_interp_f32.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_spline_interp_f32.c
  4. * Description: Floating-point cubic spline interpolation
  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/interpolation_functions.h"
  29. /**
  30. @ingroup groupInterpolation
  31. */
  32. /**
  33. @defgroup SplineInterpolate Cubic Spline Interpolation
  34. Spline interpolation is a method of interpolation where the interpolant
  35. is a piecewise-defined polynomial called "spline".
  36. @par Introduction
  37. Given a function f defined on the interval [a,b], a set of n nodes x(i)
  38. where a=x(1)<x(2)<...<x(n)=b and a set of n values y(i) = f(x(i)),
  39. a cubic spline interpolant S(x) is defined as:
  40. <pre>
  41. S1(x) x(1) < x < x(2)
  42. S(x) = ...
  43. Sn-1(x) x(n-1) < x < x(n)
  44. </pre>
  45. where
  46. <pre>
  47. Si(x) = a_i+b_i(x-xi)+c_i(x-xi)^2+d_i(x-xi)^3 i=1, ..., n-1
  48. </pre>
  49. @par Algorithm
  50. Having defined h(i) = x(i+1) - x(i)
  51. <pre>
  52. h(i-1)c(i-1)+2[h(i-1)+h(i)]c(i)+h(i)c(i+1) = 3/h(i)*[a(i+1)-a(i)]-3/h(i-1)*[a(i)-a(i-1)] i=2, ..., n-1
  53. </pre>
  54. It is possible to write the previous conditions in matrix form (Ax=B).
  55. In order to solve the system two boundary conidtions are needed.
  56. - Natural spline: S1''(x1)=2*c(1)=0 ; Sn''(xn)=2*c(n)=0
  57. In matrix form:
  58. <pre>
  59. | 1 0 0 ... 0 0 0 || c(1) | | 0 |
  60. | h(0) 2[h(0)+h(1)] h(1) ... 0 0 0 || c(2) | | 3/h(2)*[a(3)-a(2)]-3/h(1)*[a(2)-a(1)] |
  61. | ... ... ... ... ... ... ... || ... |=| ... |
  62. | 0 0 0 ... h(n-2) 2[h(n-2)+h(n-1)] h(n-1) || c(n-1) | | 3/h(n-1)*[a(n)-a(n-1)]-3/h(n-2)*[a(n-1)-a(n-2)] |
  63. | 0 0 0 ... 0 0 1 || c(n) | | 0 |
  64. </pre>
  65. - Parabolic runout spline: S1''(x1)=2*c(1)=S2''(x2)=2*c(2) ; Sn-1''(xn-1)=2*c(n-1)=Sn''(xn)=2*c(n)
  66. In matrix form:
  67. <pre>
  68. | 1 -1 0 ... 0 0 0 || c(1) | | 0 |
  69. | h(0) 2[h(0)+h(1)] h(1) ... 0 0 0 || c(2) | | 3/h(2)*[a(3)-a(2)]-3/h(1)*[a(2)-a(1)] |
  70. | ... ... ... ... ... ... ... || ... |=| ... |
  71. | 0 0 0 ... h(n-2) 2[h(n-2)+h(n-1)] h(n-1) || c(n-1) | | 3/h(n-1)*[a(n)-a(n-1)]-3/h(n-2)*[a(n-1)-a(n-2)] |
  72. | 0 0 0 ... 0 -1 1 || c(n) | | 0 |
  73. </pre>
  74. A is a tridiagonal matrix (a band matrix of bandwidth 3) of size N=n+1. The factorization
  75. algorithms (A=LU) can be simplified considerably because a large number of zeros appear
  76. in regular patterns. The Crout method has been used:
  77. 1) Solve LZ=B
  78. <pre>
  79. u(1,2) = A(1,2)/A(1,1)
  80. z(1) = B(1)/l(11)
  81. FOR i=2, ..., N-1
  82. l(i,i) = A(i,i)-A(i,i-1)u(i-1,i)
  83. u(i,i+1) = a(i,i+1)/l(i,i)
  84. z(i) = [B(i)-A(i,i-1)z(i-1)]/l(i,i)
  85. l(N,N) = A(N,N)-A(N,N-1)u(N-1,N)
  86. z(N) = [B(N)-A(N,N-1)z(N-1)]/l(N,N)
  87. </pre>
  88. 2) Solve UX=Z
  89. <pre>
  90. c(N)=z(N)
  91. FOR i=N-1, ..., 1
  92. c(i)=z(i)-u(i,i+1)c(i+1)
  93. </pre>
  94. c(i) for i=1, ..., n-1 are needed to compute the n-1 polynomials.
  95. b(i) and d(i) are computed as:
  96. - b(i) = [y(i+1)-y(i)]/h(i)-h(i)*[c(i+1)+2*c(i)]/3
  97. - d(i) = [c(i+1)-c(i)]/[3*h(i)]
  98. Moreover, a(i)=y(i).
  99. @par Behaviour outside the given intervals
  100. It is possible to compute the interpolated vector for x values outside the
  101. input range (xq<x(1); xq>x(n)). The coefficients used to compute the y values for
  102. xq<x(1) are going to be the ones used for the first interval, while for xq>x(n) the
  103. coefficients used for the last interval.
  104. */
  105. /**
  106. @addtogroup SplineInterpolate
  107. @{
  108. */
  109. /**
  110. * @brief Processing function for the floating-point cubic spline interpolation.
  111. * @param[in] S points to an instance of the floating-point spline structure.
  112. * @param[in] xq points to the x values of the interpolated data points.
  113. * @param[out] pDst points to the block of output data.
  114. * @param[in] blockSize number of samples of output data.
  115. */
  116. void arm_spline_f32(
  117. arm_spline_instance_f32 * S,
  118. const float32_t * xq,
  119. float32_t * pDst,
  120. uint32_t blockSize)
  121. {
  122. const float32_t * x = S->x;
  123. const float32_t * y = S->y;
  124. int32_t n = S->n_x;
  125. /* Coefficients (a==y for i<=n-1) */
  126. float32_t * b = (S->coeffs);
  127. float32_t * c = (S->coeffs)+(n-1);
  128. float32_t * d = (S->coeffs)+(2*(n-1));
  129. const float32_t * pXq = xq;
  130. int32_t blkCnt = (int32_t)blockSize;
  131. int32_t blkCnt2;
  132. int32_t i;
  133. float32_t x_sc;
  134. #ifdef ARM_MATH_NEON
  135. float32x4_t xiv;
  136. float32x4_t aiv;
  137. float32x4_t biv;
  138. float32x4_t civ;
  139. float32x4_t div;
  140. float32x4_t xqv;
  141. float32x4_t temp;
  142. float32x4_t diff;
  143. float32x4_t yv;
  144. #endif
  145. /* Create output for x(i)<x<x(i+1) */
  146. for (i=0; i<n-1; i++)
  147. {
  148. #ifdef ARM_MATH_NEON
  149. xiv = vdupq_n_f32(x[i]);
  150. aiv = vdupq_n_f32(y[i]);
  151. biv = vdupq_n_f32(b[i]);
  152. civ = vdupq_n_f32(c[i]);
  153. div = vdupq_n_f32(d[i]);
  154. while( *(pXq+4) <= x[i+1] && blkCnt > 4 )
  155. {
  156. /* Load [xq(k) xq(k+1) xq(k+2) xq(k+3)] */
  157. xqv = vld1q_f32(pXq);
  158. pXq+=4;
  159. /* Compute [xq(k)-x(i) xq(k+1)-x(i) xq(k+2)-x(i) xq(k+3)-x(i)] */
  160. diff = vsubq_f32(xqv, xiv);
  161. temp = diff;
  162. /* y(i) = a(i) + ... */
  163. yv = aiv;
  164. /* ... + b(i)*(x-x(i)) + ... */
  165. yv = vmlaq_f32(yv, biv, temp);
  166. /* ... + c(i)*(x-x(i))^2 + ... */
  167. temp = vmulq_f32(temp, diff);
  168. yv = vmlaq_f32(yv, civ, temp);
  169. /* ... + d(i)*(x-x(i))^3 */
  170. temp = vmulq_f32(temp, diff);
  171. yv = vmlaq_f32(yv, div, temp);
  172. /* Store [y(k) y(k+1) y(k+2) y(k+3)] */
  173. vst1q_f32(pDst, yv);
  174. pDst+=4;
  175. blkCnt-=4;
  176. }
  177. #endif
  178. while( *pXq <= x[i+1] && blkCnt > 0 )
  179. {
  180. x_sc = *pXq++;
  181. *pDst = y[i]+b[i]*(x_sc-x[i])+c[i]*(x_sc-x[i])*(x_sc-x[i])+d[i]*(x_sc-x[i])*(x_sc-x[i])*(x_sc-x[i]);
  182. pDst++;
  183. blkCnt--;
  184. }
  185. }
  186. /* Create output for remaining samples (x>=x(n)) */
  187. #ifdef ARM_MATH_NEON
  188. /* Compute 4 outputs at a time */
  189. blkCnt2 = blkCnt >> 2;
  190. while(blkCnt2 > 0)
  191. {
  192. /* Load [xq(k) xq(k+1) xq(k+2) xq(k+3)] */
  193. xqv = vld1q_f32(pXq);
  194. pXq+=4;
  195. /* Compute [xq(k)-x(i) xq(k+1)-x(i) xq(k+2)-x(i) xq(k+3)-x(i)] */
  196. diff = vsubq_f32(xqv, xiv);
  197. temp = diff;
  198. /* y(i) = a(i) + ... */
  199. yv = aiv;
  200. /* ... + b(i)*(x-x(i)) + ... */
  201. yv = vmlaq_f32(yv, biv, temp);
  202. /* ... + c(i)*(x-x(i))^2 + ... */
  203. temp = vmulq_f32(temp, diff);
  204. yv = vmlaq_f32(yv, civ, temp);
  205. /* ... + d(i)*(x-x(i))^3 */
  206. temp = vmulq_f32(temp, diff);
  207. yv = vmlaq_f32(yv, div, temp);
  208. /* Store [y(k) y(k+1) y(k+2) y(k+3)] */
  209. vst1q_f32(pDst, yv);
  210. pDst+=4;
  211. blkCnt2--;
  212. }
  213. /* Tail */
  214. blkCnt2 = blkCnt & 3;
  215. #else
  216. blkCnt2 = blkCnt;
  217. #endif
  218. while(blkCnt2 > 0)
  219. {
  220. x_sc = *pXq++;
  221. *pDst = y[i-1]+b[i-1]*(x_sc-x[i-1])+c[i-1]*(x_sc-x[i-1])*(x_sc-x[i-1])+d[i-1]*(x_sc-x[i-1])*(x_sc-x[i-1])*(x_sc-x[i-1]);
  222. pDst++;
  223. blkCnt2--;
  224. }
  225. }
  226. /**
  227. @} end of SplineInterpolate group
  228. */