LMMS
Loading...
Searching...
No Matches
interpolation.h
Go to the documentation of this file.
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
25#ifndef LMMS_INTERPOLATION_H
26#define LMMS_INTERPOLATION_H
27
28#include <cmath>
29#include <numbers>
30
31namespace lmms
32{
33
34inline float hermiteInterpolate( float x0, float x1, float x2, float x3,
35 float frac_pos )
36{
37 const float frsq = frac_pos*frac_pos;
38 const float frsq2 = 2*frsq;
39 return( ( (x2-x0) *0.5f ) * ( frac_pos * (frsq+1) -frsq2 ) +
40 ( frsq2*frac_pos - 3*frsq ) * ( x1-x2 ) +
41 frsq2 * (frac_pos-1) * ( ( x3-x1 ) * 0.25f ) + x1 );
42
43/*
44 const float frsq = frac_pos*frac_pos;
45 //const float frsq2 = 2*frsq;
46 frac_pos *= 0.5;
47 const float frcu = frsq*frac_pos;
48 return (
49
50 (frcu - frsq + frac_pos) * ((x2 - x0)) +
51
52 (4*frcu - 3*frsq) * (x1 - x2)
53 //frsq*(2*frac_pos-3) * (x1 - x2)
54
55 + (frcu - 0.5*frsq)*((x3 - x1))
56
57 + x1
58
59 );
60*/
61}
62
63
64
65inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x )
66{
67 float frsq = x * x;
68 float frcu = frsq * v0;
69 float t1 = v1 * 3.f + v3;
70
71 return v1 + (0.5f * frcu + x) * (v2 - frcu * (1.0f / 6.0f) -
72 (t1 * (1.0f / 6.0f) - v0) * (1.0f / 3.0f)) + frsq * x * (t1 *
73 (1.0f / 6.0f) - 0.5f * v2) + frsq * (0.5f * v2 - v1);
74}
75
76
77
78inline float cosinusInterpolate( float v0, float v1, float x )
79{
80 const float f = (1.0f - std::cos(x * std::numbers::pi_v<float>)) * 0.5f;
81 return f * (v1 - v0) + v0;
82}
83
84
85inline float optimalInterpolate( float v0, float v1, float x )
86{
87 const float z = x - 0.5f;
88 const float even = v1 + v0;
89 const float odd = v1 - v0;
90
91 const float c0 = even * 0.50037842517188658;
92 const float c1 = odd * 1.00621089801788210;
93 const float c2 = even * -0.004541102062639801;
94 const float c3 = odd * -1.57015627178718420;
95
96 return ((c3 * z + c2) * z + c1) * z + c0;
97}
98
99
100inline float optimal4pInterpolate( float v0, float v1, float v2, float v3, float x )
101{
102 const float z = x - 0.5f;
103 const float even1 = v2 + v1;
104 const float odd1 = v2 - v1;
105 const float even2 = v3 + v0;
106 const float odd2 = v3 - v0;
107
108 const float c0 = even1 * 0.45868970870461956 + even2 * 0.04131401926395584;
109 const float c1 = odd1 * 0.48068024766578432 + odd2 * 0.17577925564495955;
110 const float c2 = even1 * -0.246185007019907091 + even2 * 0.24614027139700284;
111 const float c3 = odd1 * -0.36030925263849456 + odd2 * 0.10174985775982505;
112
113 return ((c3 * z + c2) * z + c1) * z + c0;
114}
115
116
117
118inline float lagrangeInterpolate( float v0, float v1, float v2, float v3, float x )
119{
120 const float c0 = v1;
121 const float c1 = v2 - v0 * ( 1.0f / 3.0f ) - v1 * 0.5f - v3 * ( 1.0f / 6.0f );
122 const float c2 = 0.5f * (v0 + v2) - v1;
123 const float c3 = ( 1.0f/6.0f ) * ( v3 - v0 ) + 0.5f * ( v1 - v2 );
124 return ((c3 * x + c2) * x + c1) * x + c0;
125}
126
127
128
129} // namespace lmms
130
131#endif // LMMS_INTERPOLATION_H
unsigned z
Definition inflate.c:1589
unsigned x[BMAX+1]
Definition inflate.c:1586
unsigned f
Definition inflate.c:1572
static void v2(register WDL_FFT_REAL *a)
Definition fft.c:1099
static void c2(register WDL_FFT_COMPLEX *a)
Definition fft.c:270
Definition AudioAlsa.cpp:35
float optimalInterpolate(float v0, float v1, float x)
Definition interpolation.h:85
float hermiteInterpolate(float x0, float x1, float x2, float x3, float frac_pos)
Definition interpolation.h:34
float cubicInterpolate(float v0, float v1, float v2, float v3, float x)
Definition interpolation.h:65
float lagrangeInterpolate(float v0, float v1, float v2, float v3, float x)
Definition interpolation.h:118
float optimal4pInterpolate(float v0, float v1, float v2, float v3, float x)
Definition interpolation.h:100
float cosinusInterpolate(float v0, float v1, float x)
Definition interpolation.h:78