source: trunk/src/Utils/interpolation.cc @ 466

Last change on this file since 466 was 466, checked in by MatthewWhiting, 16 years ago

Added interpolation.cc file to Utils, although functions are not used by any of the code...

File size: 2.2 KB
Line 
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
33float 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  bool foundIt=false;
40  while( ((y[i]-yval)*(y[min]-yval) > 0) && i<max) i++;
41  if(i==max){
42    if(y[min]>y[max]){ if(y[min]>yval) i=max; else i=min; }
43    else{ if(y[min]>yval) i=min; else i=max; }
44  }
45  float slope = (y[i]-y[i-1])/(x[i]-x[i-1]);
46  float xval = x[i-1] + (yval-y[i-1])/slope;
47  return xval;
48}
49
50
51float interpolateToFindY(float xval, float *x, float *y, int size, int min, int max)
52{
53  if(min<0) min=0;
54  if(max<0) max=size;
55
56  int i=min+1;
57  while( ((x[i]-xval)*(x[i-1]-xval)>0) && i<max) i++;
58  float slope = (y[i]-y[i-1])/(x[i]-x[i-1]);
59  float yval = y[i-1] + (xval-x[i-1])*slope;
60  return yval;
61}
Note: See TracBrowser for help on using the repository browser.