Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

AO_algo.h

Go to the documentation of this file.
00001 /*
00002 Arroyo - software for the simulation of electromagnetic wave propagation
00003 through turbulence and optics.
00004 
00005 Copyright (c) 2000-2004 California Institute of Technology.  Written by
00006 Dr. Matthew Britton.  For comments or questions about this software,
00007 please contact the author at mbritton@astro.caltech.edu.
00008 
00009 This program is free software; you can redistribute it and/or modify it
00010 under the terms of the GNU General Public License as  published by the
00011 Free Software Foundation; either version 2 of the License, or (at your
00012 option) any later version.
00013 
00014 This program is provided "as is" and distributed in the hope that it
00015 will be useful, but WITHOUT ANY WARRANTY; without even the implied
00016 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  In no
00017 event shall California Institute of Technology be liable to any party
00018 for direct, indirect, special, incidental or consequential damages,
00019 including lost profits, arising out of the use of this software and its
00020 documentation, even if the California Institute of Technology has been
00021 advised of the possibility of such damage.   The California Institute of
00022 Technology has no obligation to provide maintenance, support, updates,
00023 enhancements or modifications.  See the GNU General Public License for
00024 more details.
00025 
00026 You should have received a copy of the GNU General Public License along
00027 with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00029 */
00030 
00031 #ifndef AO_ALGO_H
00032 #define AO_ALGO_H
00033 
00034 #include <cmath>
00035 #include <vector>
00036 #include <string> 
00037 #include "sim_utils.h"
00038 #include "AO_cpp.h"
00039 #include "three_frame.h"
00040 
00041 
00042 namespace Arroyo {
00043 
00046   double chisq_2gf(const vector<float> & data, vector<double> & centers, 
00047                    vector<double> & widths, vector<double> & amps, 
00048                    double & dcconst, int verbose = 0);
00049 
00052   void twogauss_fit(const vector<float> & data, vector<double> & centers, 
00053         vector<double> & widths, vector<double> & amps, double & dcconst);
00054 
00057   inline void box_mueller(double & r1, double & r2) {
00058     double r, fac;
00059 
00060     do {
00061 #if defined(__sun)
00062       r1 = 2*(random()/(double)LONG_MAX) - 1;
00063       r2 = 2*(random()/(double)LONG_MAX) - 1;
00064 #else
00065       r1 = 2*(random()/(double)RAND_MAX) - 1;
00066       r2 = 2*(random()/(double)RAND_MAX) - 1;
00067 #endif
00068       r = r1*r1+r2*r2;
00069     } while(r>=1 || r==0);
00070 
00071     fac = sqrt(-2*log(r)/r);
00072     r1 *= fac;
00073     r2 *= fac;
00074   }
00075 
00079   template<class U, class V>
00080     void fit_straight_line(const long & nelems, const double * x,
00081                         const U * y, const V * wts, 
00082                         double &slope, double & sigsq_slope,
00083                         double & yintercept, double & sigsq_yintercept, 
00084                         double & chisq){
00085 
00086     slope = sigsq_slope = yintercept = sigsq_yintercept = chisq = 0;
00087 
00088     if(nelems <2){
00089       cerr << "fit_straight_line error - only " << nelems << " points\n";
00090       throw(string("fit_straight_line"));
00091     }
00092 
00093     double s, sx, sy, s_tt, ti;
00094 
00095     int nnonzerowts = 0;
00096     for(int i=0; i<nelems; i++)
00097       if(wts[i] != 0) nnonzerowts++;
00098 
00099     // If two or more weights are nonzero, proceed with weights
00100     if(nnonzerowts>=3){
00101       s = sx = sy = s_tt = 0;
00102       for(int i=0; i<nelems; i++){
00103         s += wts[i]*wts[i];
00104         sx += x[i]*wts[i]*wts[i];
00105         sy += y[i]*wts[i]*wts[i];
00106       }
00107       for(int i=0; i<nelems; i++){
00108         ti = wts[i]*(x[i] - sx/s);
00109         s_tt += ti*ti;
00110         slope += wts[i]*ti*y[i];
00111       }
00112       /*      cout << endl << "weights: s " << s << "\t sx " << sx << "\tsy " << sy */
00113       /*         << "\ts_tt " << s_tt; */
00114     } 
00115     // otherwise proceed w/o weights 
00116     else if(nnonzerowts<=2){  
00117       sx = sy = s_tt = 0;
00118       s = (double)nelems;
00119       for(int i=0; i<nelems; i++){
00120         sx += x[i];
00121         sy += y[i];
00122       }
00123       for(int i=0; i<nelems; i++){
00124         ti = x[i] - sx/s;
00125         s_tt += ti*ti;
00126         slope += ti*y[i];
00127       }
00128       /*      cout << "no weights: s " << s << "\t sx " << sx << "\tsy " << sy */
00129       /*         << "\ts_tt " << s_tt; */
00130     }
00131 
00132     slope /= s_tt;
00133     yintercept = (sy - sx*slope)/s;
00134 
00135 
00136     double chi = 0;
00137     if(nnonzerowts>=3){
00138       sigsq_yintercept = (1+sx*sx/(s*s_tt))/s;
00139       sigsq_slope = 1/s_tt; 
00140       for(int i=0; i<nelems; i++){
00141         chi = (y[i] - yintercept - slope*x[i])*wts[i];
00142         chisq += chi*chi;
00143       }
00144     } else {
00145       for(int i=0; i<nelems; i++){
00146         chi = (y[i] - yintercept - slope*x[i]);
00147         chisq += chi*chi;
00148       }
00149     } 
00150   }
00151 
00152 
00159   template<class T>
00160     void cyclic_permutation(vector<long> axes, long xshift,
00161                                         long yshift, T * data){
00162 
00163     while(xshift<0) xshift += axes[1];
00164     while(yshift<0) yshift += axes[0];
00165 
00166     if(xshift==0 && yshift==0) return;
00167 
00168     long tmpelem = axes[0];
00169     if(axes[1]>axes[0]) tmpelem=axes[1];
00170 
00171     T * tmparr;
00172     try{tmparr = new T[tmpelem];}
00173     catch(...){
00174       cerr << "cyclic_permutation error - could not allocate memory: "
00175                         << tmpelem << " bytes\n";
00176       throw;
00177     }
00178 
00179     if(yshift!=0){
00180       for(int i=0; i<axes[1]; i++){
00181         for(int j=0; j<axes[0]; j++)
00182           tmparr[j] = data[i*axes[0]+j];
00183         for(int j=0; j<axes[0]; j++)
00184           data[i*axes[0]+j] = tmparr[(j+yshift)%axes[0]];
00185       }
00186     }
00187     if(xshift!=0){
00188       for(int j=0; j<axes[0]; j++){
00189         for(int i=0; i<axes[1]; i++)
00190           tmparr[i] = data[i*axes[0]+j];
00191         for(int i=0; i<axes[1]; i++)
00192           data[i*axes[0]+j] = tmparr[(i+xshift)%axes[1]];
00193       }
00194     }
00195     delete [] tmparr;
00196   }
00197 
00198 
00205   template<class T>
00206     void complex_cyclic_permutation(vector<long> axes,
00207                         long xshift, long yshift, T * data){
00208 
00209     while(xshift<0) xshift += axes[1];
00210     while(yshift<0) yshift += axes[0];
00211 
00212     if(xshift==0 && yshift==0) return;
00213 
00214     long tmpelem = 2*axes[0];
00215     if(axes[1]>axes[0]) tmpelem=2*axes[1];
00216 
00217     T * tmparr;
00218     //try{tmparr = new T[2*tmpelem];}
00219     alloc_size sz(tmpelem, 2);
00220     try{
00221       tmparr = new T[sz];
00222     }
00223     catch(...){
00224       cerr << "complex_cyclic_permutation error - could not allocate memory: "
00225                         << sz << " bytes\n";
00226       throw;
00227     }
00228 
00229     if(yshift!=0){
00230       for(int i=0; i<axes[1]; i++){
00231         for(int j=0; j<axes[0]; j++){
00232           tmparr[2*j] = data[2*(i*axes[0]+j)];
00233           tmparr[2*j+1] = data[2*(i*axes[0]+j)+1];
00234         }
00235         for(int j=0; j<axes[0]; j++){
00236           data[2*(i*axes[0]+j)] = tmparr[2*((j+yshift)%axes[0])];
00237           data[2*(i*axes[0]+j)+1] = tmparr[2*((j+yshift)%axes[0])+1];
00238         }
00239       }
00240     }
00241 
00242     if(xshift!=0){
00243       for(int j=0; j<axes[0]; j++){
00244         for(int i=0; i<axes[1]; i++){
00245           tmparr[2*i] = data[2*(i*axes[0]+j)];
00246           tmparr[2*i+1] = data[2*(i*axes[0]+j)+1];
00247         }
00248         for(int i=0; i<axes[1]; i++){
00249           data[2*(i*axes[0]+j)] = tmparr[2*((i+xshift)%axes[1])];
00250           data[2*(i*axes[0]+j)+1] = tmparr[2*((i+xshift)%axes[1])+1];
00251         }
00252       }
00253     }
00254 
00255     delete [] tmparr;
00256   }
00257 
00258 
00259 }
00260 
00261 #endif

Generated on Thu Nov 29 17:16:28 2007 for arroyo by  doxygen 1.3.9.1