interpolation.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * interpolation.h - fast implementations of several interpolation-algorithms
  3. *
  4. * Copyright (c) 2004-2005 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef INTERPOLATION_H
  25. #define INTERPOLATION_H
  26. #ifndef __USE_XOPEN
  27. #define __USE_XOPEN
  28. #endif
  29. #include <math.h>
  30. #include "lmms_constants.h"
  31. #include "lmms_math.h"
  32. inline float hermiteInterpolate( float x0, float x1, float x2, float x3,
  33. float frac_pos )
  34. {
  35. const float frsq = frac_pos*frac_pos;
  36. const float frsq2 = 2*frsq;
  37. return( ( (x2-x0) *0.5f ) * ( frac_pos * (frsq+1) -frsq2 ) +
  38. ( frsq2*frac_pos - 3*frsq ) * ( x1-x2 ) +
  39. frsq2 * (frac_pos-1) * ( ( x3-x1 ) * 0.25f ) + x1 );
  40. /*
  41. const float frsq = frac_pos*frac_pos;
  42. //const float frsq2 = 2*frsq;
  43. frac_pos *= 0.5;
  44. const float frcu = frsq*frac_pos;
  45. return (
  46. (frcu - frsq + frac_pos) * ((x2 - x0)) +
  47. (4*frcu - 3*frsq) * (x1 - x2)
  48. //frsq*(2*frac_pos-3) * (x1 - x2)
  49. + (frcu - 0.5*frsq)*((x3 - x1))
  50. + x1
  51. );
  52. */
  53. }
  54. inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x )
  55. {
  56. float frsq = x*x;
  57. float frcu = frsq*v0;
  58. float t1 = v3 + 3*v1;
  59. return( v1 + fastFmaf( 0.5f, frcu, x ) * ( v2 - frcu * ( 1.0f/6.0f ) -
  60. fastFmaf( t1, ( 1.0f/6.0f ), -v0 ) * ( 1.0f/3.0f ) ) + frsq * x * ( t1 *
  61. ( 1.0f/6.0f ) - 0.5f * v2 ) + frsq * fastFmaf( 0.5f, v2, -v1 ) );
  62. }
  63. inline float cosinusInterpolate( float v0, float v1, float x )
  64. {
  65. const float f = ( 1.0f - cosf( x * F_PI ) ) * 0.5f;
  66. return fastFmaf( f, v1-v0, v0 );
  67. }
  68. inline float linearInterpolate( float v0, float v1, float x )
  69. {
  70. return fastFmaf( x, v1-v0, v0 );
  71. }
  72. inline float optimalInterpolate( float v0, float v1, float x )
  73. {
  74. const float z = x - 0.5f;
  75. const float even = v1 + v0;
  76. const float odd = v1 - v0;
  77. const float c0 = even * 0.50037842517188658;
  78. const float c1 = odd * 1.00621089801788210;
  79. const float c2 = even * -0.004541102062639801;
  80. const float c3 = odd * -1.57015627178718420;
  81. return fastFmaf( fastFmaf( fastFmaf( c3, z, c2 ), z, c1 ), z, c0 );
  82. }
  83. inline float optimal4pInterpolate( float v0, float v1, float v2, float v3, float x )
  84. {
  85. const float z = x - 0.5f;
  86. const float even1 = v2 + v1;
  87. const float odd1 = v2 - v1;
  88. const float even2 = v3 + v0;
  89. const float odd2 = v3 - v0;
  90. const float c0 = even1 * 0.45868970870461956 + even2 * 0.04131401926395584;
  91. const float c1 = odd1 * 0.48068024766578432 + odd2 * 0.17577925564495955;
  92. const float c2 = even1 * -0.246185007019907091 + even2 * 0.24614027139700284;
  93. const float c3 = odd1 * -0.36030925263849456 + odd2 * 0.10174985775982505;
  94. return fastFmaf( fastFmaf( fastFmaf( c3, z, c2 ), z, c1 ), z, c0 );
  95. }
  96. inline float lagrangeInterpolate( float v0, float v1, float v2, float v3, float x )
  97. {
  98. const float c0 = v1;
  99. const float c1 = v2 - v0 * ( 1.0f / 3.0f ) - v1 * 0.5f - v3 * ( 1.0f / 6.0f );
  100. const float c2 = 0.5f * (v0 + v2) - v1;
  101. const float c3 = ( 1.0f/6.0f ) * ( v3 - v0 ) + 0.5f * ( v1 - v2 );
  102. return fastFmaf( fastFmaf( fastFmaf( c3, x, c2 ), x, c1 ), x, c0 );
  103. }
  104. #endif