NonUniformInterpolation.java

Go to the documentation of this file.
00001 package theba.core.math;
00002 
00010 public class NonUniformInterpolation {
00011 
00012         double[] samples;
00013 
00014         double[] values;
00015 
00024         public NonUniformInterpolation(double[] invals, double[] outvals) {
00025                 if (invals.length != outvals.length)
00026                         throw new RuntimeException("Lengths must be equal!");
00027                 samples = invals;
00028                 values = outvals;
00029         }
00030 
00036         public double interpolate(double val) {
00037                 double result;
00038                 int next = 0;
00039                 int prev = 0;
00040                 int i = 0;
00041                 for (i = 0; i < samples.length; i++) {
00042                         prev = next;
00043                         next = i;
00044                         if (samples[next] > val)
00045                                 break;
00046                 }
00047 
00048                 // we probably should extrapolatevalues if outside table-lengthm
00049                 // but currently we just ignore them
00050                 if (i == samples.length - 1) {
00051                 }
00052                 double diff = samples[next] - samples[prev];
00053                 double d = (samples[next] - val) / diff;
00054                 result = values[next] * (1.0 - d) + values[prev] * d;
00055 
00056                 return result;
00057         }
00058 
00064         public static void main(String[] args) {
00065                 double[] samplepoints = { 0.0, 0.5, 1.0, 1.5, 2.0, 2.5 };
00066 
00067                 double[] xvalues = { 0, 1, 2, 3, 4, 5, 6 };
00068                 double[] yvalues = { 0, 1, 1, 3, 3, 5, 6 };
00069 
00070                 NonUniformInterpolation nx = new NonUniformInterpolation(samplepoints,
00071                                 xvalues);
00072                 NonUniformInterpolation ny = new NonUniformInterpolation(samplepoints,
00073                                 yvalues);
00074 
00075                 System.out.println("x" + nx.interpolate(1.25));
00076                 System.out.println("y" + ny.interpolate(1.25));
00077 
00078         }
00079 }

Generated on Fri Nov 13 08:57:07 2009 for Theba by  doxygen 1.6.1