Made 0.4.12 release
[lmms/mlankhorst.git] / include / interpolation.h
blob4ff582775a371d2323bddc8f7f758b7b5cfd6c84
1 /*
2 * interpolation.h - fast implementations of several interpolation-algorithms
4 * Copyright (c) 2004-2005 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5 *
6 * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
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.
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.
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.
26 #ifndef _INTERPOLATION_H
27 #define _INTERPOLATION_H
29 #ifndef __USE_XOPEN
30 #define __USE_XOPEN
31 #endif
33 #include <math.h>
34 #include "lmms_constants.h"
36 inline float hermiteInterpolate( float x0, float x1, float x2, float x3,
37 float frac_pos )
39 const float frsq = frac_pos*frac_pos;
40 const float frsq2 = 2*frsq;
41 return( ( (x2-x0) *0.5f ) * ( frac_pos * (frsq+1) -frsq2 ) +
42 ( frsq2*frac_pos - 3*frsq ) * ( x1-x2 ) +
43 frsq2 * (frac_pos-1) * ( ( x3-x1 ) * 0.25f ) + x1 );
46 const float frsq = frac_pos*frac_pos;
47 //const float frsq2 = 2*frsq;
48 frac_pos *= 0.5;
49 const float frcu = frsq*frac_pos;
50 return (
52 (frcu - frsq + frac_pos) * ((x2 - x0)) +
54 (4*frcu - 3*frsq) * (x1 - x2)
55 //frsq*(2*frac_pos-3) * (x1 - x2)
57 + (frcu - 0.5*frsq)*((x3 - x1))
59 + x1
67 inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x )
69 float frsq = x*x;
70 float frcu = frsq*v0;
71 float t1 = v3 + 3*v1;
73 return( v1 + 0.5f * frcu + x * ( v2 - frcu * ( 1.0f/6.0f ) -
74 t1 * ( 1.0f/6.0f ) - v0 / 3.0f ) + frsq * x * ( t1 *
75 ( 1.0f/6.0f ) - 0.5f * v2 ) + frsq * ( 0.5f * v2 - v1 ) );
80 inline float cosinusInterpolate( float v0, float v1, float x )
82 float f = cosf( x * ( F_PI_2 ) );
83 return( v0*f + v1*( 1.0f-f ) );
88 inline float linearInterpolate( float v0, float v1, float x )
90 return( v0*( 1.0f-x ) + v1*x );
94 #endif