DenoisingLib.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef DenoisingLib_H_
00024 #define DenoisingLib_H_
00025
00026
00027 #include <casacore/casa/Arrays/Matrix.h>
00028 #include <casacore/casa/Arrays/Vector.h>
00029
00030
00031 #include <gsl/gsl_multifit.h>
00032
00033
00034 namespace casa {
00035
00036 namespace denoising {
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
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
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
00084 model_p.row(0) = 1.0;
00085
00086
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
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
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
00114 model_p.row(0) = 1.0;
00115 if (order > 0) model_p.row(1) = scale*(data_x-mid);
00116
00117
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;
00145
00146 };
00147
00149
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
00170 setData(data);
00171
00172
00173 gsl_vector *coeff = calcFitCoeffCore(data_p.column(0));
00174
00175
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
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
00209 gsl_vector *gsl_coeff_p;
00210 gsl_matrix *gsl_covariance_p;
00211 gsl_multifit_linear_workspace *gsl_workspace_p;
00212
00213
00214 Matrix<Double> data_p;
00215
00216
00217 int errno_p;
00218 double chisq_p;
00219 };
00220
00222
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
00244 Vector<Double> weights_p;
00245 gsl_vector gls_weights_p;
00246 };
00247
00248 }
00249
00250 }
00251
00252
00253 #endif
00254