Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's C++ for linear and quadratic interpolation.<br> <code>interp1( 5.3, a, n )</code> is a[5] + .3 * (a[6] - a[5]), .3 of the way from a[5] to a[6];<br> <code>interp1array( a, 1000, b, 1500 )</code> would stretch <code>a</code> to <code>b</code>.<br> <code>interp2( 5.3, a, n )</code> draws a parabola through the 3 nearest points a[4] a[5] a[6]: smoother than interp1 but still fast.<br> (Splines use 4 nearest points, smoother yet; if you read python, see <a href="http://advice.mechanicalkern.com/question/22/basic-spline-interpolation-in-a-few-lines-of-numpy" rel="noreferrer">basic-spline-interpolation-in-a-few-lines-of-numpy</a>.</p> <pre><code>// linear, quadratic interpolation in arrays // from interpol.py denis 2010-07-23 July #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // linear interpolate x in an array // inline float interp1( float x, float a[], int n ) { if( x &lt;= 0 ) return a[0]; if( x &gt;= n - 1 ) return a[n-1]; int j = int(x); return a[j] + (x - j) * (a[j+1] - a[j]); } // linear interpolate array a[] -&gt; array b[] void inter1parray( float a[], int n, float b[], int m ) { float step = float( n - 1 ) / (m - 1); for( int j = 0; j &lt; m; j ++ ){ b[j] = interp1( j*step, a, n ); } } //.............................................................................. // parabola through 3 points, -1 &lt; x &lt; 1 float parabola( float x, float f_1, float f0, float f1 ) { if( x &lt;= -1 ) return f_1; if( x &gt;= 1 ) return f1; float l = f0 - x * (f_1 - f0); float r = f0 + x * (f1 - f0); return (l + r + x * (r - l)) / 2; } // quadratic interpolate x in an array float interp2( float x, float a[], int n ) { if( x &lt;= .5 || x &gt;= n - 1.5 ) return interp1( x, a, n ); int j = int( x + .5 ); float t = 2 * (x - j); // -1 .. 1 return parabola( t, (a[j-1] + a[j]) / 2, a[j], (a[j] + a[j+1]) / 2 ); } // quadratic interpolate array a[] -&gt; array b[] void interp2array( float a[], int n, float b[], int m ) { float step = float( n - 1 ) / (m - 1); for( int j = 0; j &lt; m; j ++ ){ b[j] = interp2( j*step, a, n ); } } int main( int argc, char* argv[] ) { // a.out [n m] -- int n = 10, m = 100; int *ns[] = { &amp;n, &amp;m, 0 }, **np = ns; char* arg; for( argv ++; (arg = *argv) &amp;&amp; *np; argv ++, np ++ ) **np = atoi( arg ); printf( "n: %d m: %d\n", n, m ); float a[n], b[m]; for( int j = 0; j &lt; n; j ++ ){ a[j] = j * j; } interp2array( a, n, b, m ); // a[] -&gt; b[] for( int j = 0; j &lt; m; j ++ ){ printf( "%.1f ", b[j] ); } printf( "\n" ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload