/** * A Quint instance is a set of five (x,y) equally spaced points. * A parabola is fitted to the derivative of the middle three. * Glen Cowan, RHUL Physics, June 2004 */ import java.math.*; public class Quint { // constructors public Quint(double dx, double[] y){ for (int i=0; i<5; i++){ this.dx = dx; this.x[i] = (double)(i-2) * dx; // define x[2] = 0 this.y[i] = y[i]; this.sigma[i] = 1; } } public Quint(double dx, double[] y, double[] sigma){ this(dx,y); for (int i=0; i<5; i++){ this.sigma[i] = sigma[i]; } } public Quint(double[] x, double[] y){ for (int i=0; i<5; i++){ this.x[i] = x[i]; this.y[i] = y[i]; this.sigma[i] = 1; } this.dx = x[1] - x[0]; } public Quint(double[] x, double[] y, double[] sigma){ this(x,y); for (int i=0; i<5; i++){ this.sigma[i] = sigma[i]; } } // public methods public double[] getX() { return x; } public double[] getY() { return y; } public double[] getSigma() { return sigma; } public void setX(double[] x) { for(int i=0; i<5; i++){ this.x[i] = x[i]; } } public void setY(double[] y) { for(int i=0; i<5; i++){ this.y[i] = y[i]; } } public void setSigma(double[] sigma) { for(int i=0; i<5; i++){ this.sigma[i] = sigma[i]; } } public double mu(){ double a = 2*y[2] - y[0] - y[4]; double b = y[0] - 2*y[1] + 2*y[3] - y[4]; double muVal = x[2] - 0.5*dx*a/b; return muVal; } public double sigmaMu() { double a = 2*y[2] - y[0] - y[4]; double b = y[0] - 2*y[1] + 2*y[3] - y[4]; double a2 = Math.pow(a,2); double b2 = Math.pow(b,2); double apb2 = Math.pow(a+b,2); double amb2 = Math.pow(a-b,2); double[] s2 = new double[5]; for (int i=0; i<5; i++){ s2[i] = Math.pow(sigma[i],2); } double c = Math.pow(0.5*dx/b2, 2); double varMu = c * (apb2*s2[0] + 2*a2*s2[1] + 2*b2*s2[2] + 2*a2*s2[3] + amb2*s2[4]); double sigVal = Math.sqrt(varMu); return sigVal; } public double deriv(int i) { double d; if ( i <= 0 || i >= 4 ) { d = 0; } else { d = (y[i+1] - y[i-1])/(2*dx); } return d; } public double peakDeriv() { double xmp = mu(); double c1 = (deriv(3) - deriv(1))/(2*dx); double c2 = (deriv(3) + deriv(1) - 2*deriv(2))/(2*Math.pow(dx,2)); double ymp = deriv(2) + c1*(xmp - x[2]) + c2*Math.pow((xmp-x[2]),2); return ymp; } public boolean goodPeak(){ boolean allPos = true; boolean allNeg = true; for (int i=1; i<4; i++){ if ( deriv(i) > 0 ) { allNeg = false; } if ( deriv(i) < 0 ) { allPos = false; } } int edgePixel = 1; double maxDeriv = Math.abs(deriv(1)); for (int i=1; i<4; i++){ if (Math.abs(deriv(i)) > maxDeriv) { maxDeriv = Math.abs(deriv(i)); edgePixel = i; } } boolean gP = (allPos || allNeg) && edgePixel == 2; return gP; } // private data fields double[] x = new double[5]; double[] y = new double[5]; double[] sigma = new double[5]; double dx; }