casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SpectralEstimate.h
Go to the documentation of this file.
00001 //# SpectralEstimate.h: Get an initial estimate for spectral lines
00002 //# Copyright (C) 2001,2002,2003,2004
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: SpectralEstimate.h 20229 2008-01-29 15:19:06Z gervandiepen $
00028 
00029 #ifndef COMPONENTS_SPECTRALESTIMATE_H
00030 #define COMPONENTS_SPECTRALESTIMATE_H
00031 
00032 #include <casa/aips.h>
00033 #include <components/SpectralComponents/SpectralElement.h>
00034 #include <components/SpectralComponents/SpectralList.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 //# Forward Declarations
00039 class GaussianSpectralElement;
00040 template <class T> class Vector;
00041 
00042 // <summary>
00043 // Get an initial estimate for spectral lines
00044 // </summary>
00045 
00046 // <use visibility=export>
00047 
00048 // <reviewed reviewer="" date="yyyy/mm/dd" tests="tSpectralFit" demos="">
00049 // </reviewed>
00050 
00051 // <prerequisite>
00052 //   <li> <linkto class=SpectralFit>SpectralFit</linkto> class
00053 // </prerequisite>
00054 //
00055 // <etymology>
00056 // From spectral line and estimate
00057 // </etymology>
00058 //
00059 // <synopsis>
00060 // The SpectralEstimate class obtains an initial guess for spectral
00061 // components. The current implementation uses the entire 
00062 // profile as signal region, or can set a window to be searched around
00063 // the highest peak automatically. A window can also be set manually.
00064 // The second derivative of
00065 // the profile in the signal region is calculated by fitting
00066 // a second degree polynomal. The smoothing parameter Q
00067 // determines the number of points used for this (=2*Q+1).
00068 // The gaussians can then be estimated as described by
00069 // Schwarz, 1968, Bull.Astr.Inst.Netherlands, Volume 19, 405.
00070 //
00071 // The elements guessed  can be used in the
00072 // <linkto class=SpectralFit>SpectralFit</linkto> class.
00073 //
00074 // The default type found is a Gaussian, defined as:
00075 // <srcblock>
00076 //      AMPL.exp[ -(x-CENTER)<sup>2</sup>/2 SIGMA<sup>2</sup>]
00077 // </srcblock>
00078 //
00079 // The parameter estimates are returned in units of zero-based
00080 // pixel indices.
00081 // </synopsis>
00082 //
00083 // <example>
00084 // </example>
00085 //
00086 // <motivation>
00087 // To have an automatic method to find spectral lines
00088 // </motivation>
00089 //
00090 // <todo asof="2001/02/14">
00091 //   <li> find a way to get to absorption lines as well
00092 //   <li> add more estimation options
00093 // </todo>
00094 
00095 class SpectralEstimate {
00096  public:
00097   //# Constants
00098   // Default maximum number of components to be found
00099   static const uInt MAXPAR = 200;
00100   //# Enumerations
00101   //# Friends
00102 
00103   //# Constructors
00104   // Default constructor creates a default estimator (default max number
00105   // of components to be found is 200) with the given maximum number
00106   // of components that will be found. A value of zero will indicate
00107   // an unlimited number.
00108   explicit SpectralEstimate(const uInt maxpar=MAXPAR);
00109   // Create an estimator with the given maximum number of possible
00110   // elements. A value of zero will indicate an unlimited number.
00111   // Construct with a given rms in profiles, a cutoff for amplitudes
00112   // found, and a minimum width. Cutoff and minsigma default to 0.0, maximum
00113   // size of list produced to 200.
00114   explicit SpectralEstimate(const Double rms,
00115                             const Double cutoff=0.0, const Double minsigma=0.0,
00116                             const uInt maxpar=MAXPAR);
00117   // Copy constructor (deep copy)
00118   SpectralEstimate(const SpectralEstimate &other);
00119 
00120   //#Destructor
00121   // Destructor
00122   ~SpectralEstimate();
00123 
00124   //# Operators
00125   // Assignment (copy semantics)
00126   SpectralEstimate &operator=(const SpectralEstimate &other);
00127 
00128   //# Member functions
00129   // Generate the estimates for a profile and return the 
00130   // list found.  The first function returns component parameters 
00131   // in units of pixel indices. The second function calls the first
00132   // and then converts to the specified abcissa space (the supplied 
00133   // vector must be monotonic); if the pixel-based center is out of range 
00134   // of the supplied abcissa vector the conversion is done via extrapolation.
00135   // The der pointer is meant for debugging, and can return 
00136   // the derivative profile.  The second function throws an AipsError
00137   // if the vectors are not the same length.
00138   // <group>
00139   template <class MT>
00140     const SpectralList& estimate(const Vector<MT>& ordinate,
00141                                  Vector<MT> *der = 0);
00142   template <class MT>
00143     const SpectralList& estimate(const Vector<MT>& abcissa,
00144                                  const Vector<MT>& ordinate);
00145   // </group>
00146 
00147   // Return the list found.
00148   const SpectralList &list() const {return slist_p; };
00149 
00150   // Set estimation parameters
00151   // <group>
00152   // Set the profile's estimated rms (forced to abs(rms))
00153   void setRMS(const Double rms=0.0);
00154   // Set the amplitude cutoff for valid estimate (forced to max(0,cutoff))
00155   void setCutoff(const Double cutoff=0.0);
00156   // Set the minimum width allowed (forced to max(0,minsigma))
00157   void setMinSigma(const Double minsigma=0.0);
00158   // Set the number of points consider at each side of test point (i.e. a
00159   // width of 2q+1 is taken). Default internally is 2; max(1,q) taken.
00160   void setQ(const uInt q=2);
00161   // Set a region [lo,hi] over which to estimate. Lo and hi are given as
00162   // zero-based vector indices.
00163   void setRegion(const Int lo, const Int hi);
00164   // Do you want to look in an automatically determined window with signal?
00165   // Default is False, meaning the full (possibly regioned) profile.
00166   void setWindowing(const Bool win=False);
00167   // Set the maximum number of estimates to find (forced to >=1; 200 default)
00168   void setMaxN(const uInt maxpar=MAXPAR);
00169   // </group>
00170 
00171  private:
00172   //#Data
00173   // Use window search
00174   Bool useWindow_p;
00175   // rms estimate in profile
00176   Double rms_p;
00177   // Source cutoff amplitude
00178   Double cutoff_p;
00179   // Window low and end value
00180   // <group>
00181   Int windowLow_p;
00182   Int windowEnd_p;
00183   // </group>
00184   // Region low and high value
00185   // <group>
00186   Int regionLow_p;
00187   Int regionEnd_p;
00188   // </group>
00189   // Smoothing parameter. I.e. 2q+1 points are taken
00190   Int q_p;
00191   // Internal cashing of calculated values based on q
00192   // <group>
00193   Double a_p;
00194   Double b_p;
00195   // </group>
00196   // The minimum Gaussian width
00197   Double sigmin_p;
00198   // The second derivatives
00199   Double *deriv_p;
00200   // The list of components
00201   SpectralList slist_p;
00202   // The length of the current profile being estimated
00203   uInt lprof_p;
00204 
00205   //# Member functions
00206   // Get the window or the total spectrum
00207   template <class MT>
00208     uInt window(const Vector<MT> &prof);
00209   // Get the second derivatives
00210   template <class MT>
00211     void findc2(const Vector<MT> &prof);
00212   // Find the Gaussians
00213   template <class MT>
00214     void findga(const Vector<MT> &prof);
00215   // Convert the parameters of the components in the list from 
00216   // pixel-based indices to the given abcissa-vector space.
00217   template <class MT> GaussianSpectralElement convertElement (const Vector<MT>& abcissa,
00218                                                       const GaussianSpectralElement& el) const;
00219 };
00220 
00221 
00222 } //# NAMESPACE CASA - END
00223 
00224 #ifndef CASACORE_NO_AUTO_TEMPLATES
00225 #include <components/SpectralComponents/Spectral2Estimate.tcc>
00226 #endif //# CASACORE_NO_AUTO_TEMPLATES
00227 #endif