Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
SplineFitter.java
1 package ij.measure;
2 
10 public class SplineFitter {
11  private double[] y2;
12 
13  public SplineFitter(int[] x, int[] y, int n) {
14  initSpline(x, y, n);
15  }
16 
21  void initSpline(int[] x, int[] y, int n) {
22  int i,k;
23  double p,qn,sig,un;
24  y2 = new double[n]; // cached
25  double[] u = new double[n];
26  for (i=1; i<n-1; i++) {
27  // 888 chk for div by 0?
28  sig = ((double) x[i]-x[i-1]) / ((double) x[i+1] - x[i-1]);
29  p = sig * y2[i-1] + 2.0;
30  y2[i] = (sig-1.0) / p;
31  u[i] = (((double) y[i+1]-y[i]) / (x[i+1]-x[i])) -
32  (((double) y[i]-y[i-1]) / (x[i]-x[i-1]));
33  u[i] = (6.0 * u[i]/ (x[i+1]-x[i-1]) - sig*u[i-1]) / p;
34  }
35  qn = un = 0.0;
36  y2[n-1] = (un-qn*u[n-2]) / (qn*y2[n-2]+1.0);
37  for (k=n-2; k>=0; k--)
38  y2[k] = y2[k]*y2[k+1]+u[k];
39  }
40 
42  public double evalSpline(int x[], int y[], int n, double xp) {
43  int klo,khi,k;
44  double h,b,a;
45  klo = 0;
46  khi = n-1;
47  while (khi-klo > 1) {
48  k = (khi+klo) >> 1;
49  if (x[k] > xp) khi = k;
50  else klo = k;
51  }
52  h = x[khi] - x[klo];
53  /* orig code */
54  /* if (h==0.0) FatalError("bad xvalues in splint\n"); */
55  if (h==0.0) return (0.0); /* arbitr ret for now */
56  a = (x[khi]-xp)/h;
57  b = (xp-x[klo])/h;
58 
59  // should have better err checking
60  if(y2==null) return (0.0);
61 
62  return (a*y[klo] + b*y[khi] + ((a*a*a-a)*y2[klo] +(b*b*b-b)*y2[khi])
63  * (h*h) / 6.0);
64  }
65 }