DenoisingLib.h

Go to the documentation of this file.
00001 //# DenoisingLib.h: This file contains the interface definition of the MSTransformManager class.
00002 //#
00003 //#  CASA - Common Astronomy Software Applications (http://casa.nrao.edu/)
00004 //#  Copyright (C) Associated Universities, Inc. Washington DC, USA 2011, All rights reserved.
00005 //#  Copyright (C) European Southern Observatory, 2011, All rights reserved.
00006 //#
00007 //#  This library is free software; you can redistribute it and/or
00008 //#  modify it under the terms of the GNU Lesser General Public
00009 //#  License as published by the Free software Foundation; either
00010 //#  version 2.1 of the License, or (at your option) any later version.
00011 //#
00012 //#  This library is distributed in the hope that it will be useful,
00013 //#  but WITHOUT ANY WARRANTY, without even the implied warranty of
00014 //#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 //#  Lesser General Public License for more details.
00016 //#
00017 //#  You should have received a copy of the GNU Lesser General Public
00018 //#  License along with this library; if not, write to the Free Software
00019 //#  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00020 //#  MA 02111-1307  USA
00021 //# $Id: $
00022 
00023 #ifndef DenoisingLib_H_
00024 #define DenoisingLib_H_
00025 
00026 // casacore containers
00027 #include <casacore/casa/Arrays/Matrix.h>
00028 #include <casacore/casa/Arrays/Vector.h>
00029 
00030 // GSL includes
00031 #include <gsl/gsl_multifit.h>
00032 
00033 
00034 namespace casa { //# NAMESPACE CASA - BEGIN
00035 
00036 namespace denoising { //# NAMESPACE DENOISING - BEGIN
00037 
00038 void GslVectorWrap(Vector<Double> casa_vector, gsl_vector &wrapper);
00039 void GslMatrixWrap(Matrix<Double> &casa_matrix, gsl_matrix &wrapper, size_t ncols=0);
00040 
00042 // GslLinearModelBase class
00044 
00045 template<class T> class GslLinearModelBase
00046 {
00047 
00048 public:
00049 
00050         GslLinearModelBase(size_t ndata, size_t ncomponents)
00051         {
00052                 ndata_p = ndata;
00053                 ncomponents_p = ncomponents;
00054                 model_p.resize(ncomponents_p,ndata_p,False);
00055         }
00056 
00057         size_t ndata() {return ndata_p;}
00058         size_t ncomponents() {return ncomponents_p;}
00059         Matrix<Double>& getModelMatrix(){return model_p;}
00060 
00061 protected:
00062 
00063         Matrix<T> model_p;
00064         size_t ndata_p;
00065         size_t ncomponents_p;
00066 };
00067 
00069 // GslPolynomialModel class
00071 
00072 template<class T> class GslPolynomialModel : public GslLinearModelBase<T>
00073 {
00074         using GslLinearModelBase<T>::model_p;
00075         using GslLinearModelBase<T>::ndata_p;
00076         using GslLinearModelBase<T>::ncomponents_p;
00077 
00078 public:
00079 
00080         GslPolynomialModel(size_t ndata, size_t order) :
00081                 GslLinearModelBase<T>(ndata,order+1)
00082         {
00083                 // Initialize model
00084                 model_p.row(0) = 1.0;
00085 
00086                 // Populate linear components
00087                 if (order > 0)
00088                 {
00089                         Vector<T> linearComponent;
00090                         linearComponent.reference(model_p.row(1));
00091                         indgen(linearComponent,-1.0,2.0/(ndata_p-1));
00092                 }
00093 
00094                 // Populate higher orders
00095                 for (size_t model_idx=2;model_idx<ncomponents_p;model_idx++)
00096                 {
00097                         for (size_t data_idx=0; data_idx<ndata_p; data_idx++)
00098                         {
00099                                 model_p(model_idx,data_idx) = pow(model_p(1,data_idx),model_idx);
00100                         }
00101                 }
00102         }
00103 
00104         GslPolynomialModel(const Vector<T> &data_x, size_t order) :
00105                 GslLinearModelBase<T>(data_x.size(),order+1)
00106         {
00107                 // Calculate scale
00108                 T min,max,mid,scale;
00109                 minMax(min,max,data_x);
00110                 mid = 0.5*(min+max);
00111                 scale = 1.0 / (min-mid);
00112 
00113                 // Populate linear components
00114                 model_p.row(0) = 1.0;
00115                 if (order > 0) model_p.row(1) = scale*(data_x-mid);
00116 
00117                 // Populate higher orders
00118                 for (size_t model_idx=2;model_idx<ncomponents_p;model_idx++)
00119                 {
00120                         for (size_t data_idx=0; data_idx<ndata_p; data_idx++)
00121                         {
00122                                 model_p(model_idx,data_idx) = pow(model_p(1,data_idx),model_idx);
00123                         }
00124                 }
00125         }
00126 
00127         Vector<Float>& getLinearComponentFloat()
00128         {
00129                 if (linear_component_float_p.size() != ndata_p) initLinearComponentFloat();
00130                 return linear_component_float_p;
00131         }
00132 
00133 private:
00134 
00135         void initLinearComponentFloat()
00136         {
00137                 linear_component_float_p.resize(ndata_p);
00138                 for (size_t data_idx=0; data_idx<ndata_p; data_idx++)
00139                 {
00140                         linear_component_float_p(data_idx) = model_p(1,data_idx);
00141                 }
00142         }
00143 
00144         Vector<Float> linear_component_float_p; // Float-compatibility
00145 
00146 };
00147 
00149 // GslMultifitLinearBase class_
00151 
00152 class GslMultifitLinearBase
00153 {
00154 
00155 public:
00156 
00157         GslMultifitLinearBase();
00158         GslMultifitLinearBase(GslLinearModelBase<Double> &model);
00159 
00160         ~GslMultifitLinearBase();
00161 
00162         void resetModel(GslLinearModelBase<Double> &model);
00163 
00164         void resetNComponents(size_t ncomponents);
00165 
00166         Vector<Complex> calcFitCoeff(Vector<Complex> &data);
00167         template<class T> Vector<T> calcFitCoeff(Vector<T> &data)
00168         {
00169                 // Store input data as double
00170                 setData(data);
00171 
00172                 // Call fit method to calculate coefficients
00173                 gsl_vector *coeff = calcFitCoeffCore(data_p.column(0));
00174 
00175                 // Get coefficients
00176                 Vector<T> fitCoeff(ncomponents_p);
00177                 for (size_t coeff_idx=0;coeff_idx<ncomponents_p;coeff_idx++)
00178                 {
00179                         fitCoeff(coeff_idx) = gsl_vector_get(coeff,coeff_idx);
00180                 }
00181 
00182                 gsl_vector_free (coeff);
00183 
00184                 return fitCoeff;
00185         }
00186 
00187 
00188 protected:
00189 
00190         void allocGslResources();
00191         void freeGslResources();
00192 
00193         virtual void setModel(GslLinearModelBase<Double> &model);
00194 
00195         void setData(Vector<Float> &data);
00196         void setData(Vector<Double> &data);
00197         void setData(Vector<Complex> &data);
00198 
00199         virtual gsl_vector* calcFitCoeffCore(Vector<Double> data);
00200 
00201         // Model
00202         size_t ndata_p;
00203         size_t ncomponents_p;
00204         size_t max_ncomponents_p;
00205         gsl_matrix gsl_model_p;
00206         GslLinearModelBase<Double> *model_p;
00207 
00208         // GSL Resources
00209         gsl_vector *gsl_coeff_p;
00210         gsl_matrix *gsl_covariance_p;
00211         gsl_multifit_linear_workspace *gsl_workspace_p;
00212 
00213         // Data
00214         Matrix<Double> data_p;
00215 
00216         // Fit result
00217         int errno_p;
00218         double chisq_p;
00219 };
00220 
00222 // GslMultifitWeightedLinear class
00224 
00225 class GslMultifitWeightedLinear : public GslMultifitLinearBase
00226 {
00227 
00228 public:
00229 
00230         GslMultifitWeightedLinear();
00231         GslMultifitWeightedLinear(GslLinearModelBase<Double> &model);
00232         ~GslMultifitWeightedLinear();
00233 
00234         void setWeights(Vector<Float> &weights);
00235         void setFlags(Vector<Bool> &flags,Bool goodIsTrue=True);
00236         void setWeightsAndFlags(Vector<Float> &weights, Vector<Bool> &flags, Bool goodIsTrue=True);
00237 
00238 protected:
00239 
00240         void setModel(GslLinearModelBase<Double> &model);
00241         gsl_vector* calcFitCoeffCore(Vector<Double> data);
00242 
00243         // Weights
00244         Vector<Double> weights_p;
00245         gsl_vector gls_weights_p;
00246 };
00247 
00248 } //# NAMESPACE DENOISING - END
00249 
00250 } //# NAMESPACE CASA - END
00251 
00252 
00253 #endif /* DenoisingLib_H_ */
00254 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1