[466] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // interpolation.cc: Intepolate in or extrapolate from an array to |
---|
| 3 | // find values |
---|
| 4 | // ----------------------------------------------------------------------- |
---|
| 5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 6 | // |
---|
| 7 | // This program is free software; you can redistribute it and/or modify it |
---|
| 8 | // under the terms of the GNU General Public License as published by the |
---|
| 9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 10 | // option) any later version. |
---|
| 11 | // |
---|
| 12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 15 | // for more details. |
---|
| 16 | // |
---|
| 17 | // You should have received a copy of the GNU General Public License |
---|
| 18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 20 | // |
---|
| 21 | // Correspondence concerning Duchamp may be directed to: |
---|
| 22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 23 | // Postal address: Dr. Matthew Whiting |
---|
| 24 | // Australia Telescope National Facility, CSIRO |
---|
| 25 | // PO Box 76 |
---|
| 26 | // Epping NSW 1710 |
---|
| 27 | // AUSTRALIA |
---|
| 28 | // ----------------------------------------------------------------------- |
---|
| 29 | #include <iostream> |
---|
| 30 | #include <math.h> |
---|
| 31 | #include <duchamp/Utils/utils.hh> |
---|
| 32 | |
---|
| 33 | float interpolateToFindX(float yval, float *x, float *y, int size, int min, int max) |
---|
| 34 | { |
---|
| 35 | if(min<0) min=0; |
---|
| 36 | if(max<0) max=size-1; |
---|
| 37 | |
---|
| 38 | int i=min+1; |
---|
| 39 | while( ((y[i]-yval)*(y[min]-yval) > 0) && i<max) i++; |
---|
| 40 | if(i==max){ |
---|
| 41 | if(y[min]>y[max]){ if(y[min]>yval) i=max; else i=min; } |
---|
| 42 | else{ if(y[min]>yval) i=min; else i=max; } |
---|
| 43 | } |
---|
| 44 | float slope = (y[i]-y[i-1])/(x[i]-x[i-1]); |
---|
| 45 | float xval = x[i-1] + (yval-y[i-1])/slope; |
---|
| 46 | return xval; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | float interpolateToFindY(float xval, float *x, float *y, int size, int min, int max) |
---|
| 51 | { |
---|
| 52 | if(min<0) min=0; |
---|
| 53 | if(max<0) max=size; |
---|
| 54 | |
---|
| 55 | int i=min+1; |
---|
| 56 | while( ((x[i]-xval)*(x[i-1]-xval)>0) && i<max) i++; |
---|
| 57 | float slope = (y[i]-y[i-1])/(x[i]-x[i-1]); |
---|
| 58 | float yval = y[i-1] + (xval-x[i-1])*slope; |
---|
| 59 | return yval; |
---|
| 60 | } |
---|