[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // atrous_1d_reconstruct.cc: 1-dimensional wavelet reconstruction. |
---|
| 3 | // ----------------------------------------------------------------------- |
---|
| 4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 5 | // |
---|
| 6 | // This program is free software; you can redistribute it and/or modify it |
---|
| 7 | // under the terms of the GNU General Public License as published by the |
---|
| 8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 9 | // option) any later version. |
---|
| 10 | // |
---|
| 11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 14 | // for more details. |
---|
| 15 | // |
---|
| 16 | // You should have received a copy of the GNU General Public License |
---|
| 17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | // |
---|
| 20 | // Correspondence concerning Duchamp may be directed to: |
---|
| 21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 22 | // Postal address: Dr. Matthew Whiting |
---|
| 23 | // Australia Telescope National Facility, CSIRO |
---|
| 24 | // PO Box 76 |
---|
| 25 | // Epping NSW 1710 |
---|
| 26 | // AUSTRALIA |
---|
| 27 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
[348] | 29 | #include <sstream> |
---|
[3] | 30 | #include <iomanip> |
---|
| 31 | #include <math.h> |
---|
[393] | 32 | #include <duchamp/duchamp.hh> |
---|
| 33 | #include <duchamp/param.hh> |
---|
| 34 | #include <duchamp/Utils/utils.hh> |
---|
| 35 | #include <duchamp/Utils/feedback.hh> |
---|
| 36 | #include <duchamp/ATrous/atrous.hh> |
---|
| 37 | #include <duchamp/ATrous/filter.hh> |
---|
| 38 | #include <duchamp/Utils/Statistics.hh> |
---|
[190] | 39 | using Statistics::madfmToSigma; |
---|
[3] | 40 | |
---|
[378] | 41 | namespace duchamp |
---|
[3] | 42 | { |
---|
[86] | 43 | |
---|
[887] | 44 | void atrous1DReconstruct(size_t &xdim, float *&input, float *&output, Param &par) |
---|
[378] | 45 | { |
---|
[528] | 46 | /// A routine that uses the a trous wavelet method to reconstruct a |
---|
| 47 | /// 1-dimensional spectrum. |
---|
| 48 | /// The Param object "par" contains all necessary info about the filter and |
---|
| 49 | /// reconstruction parameters. |
---|
| 50 | /// |
---|
| 51 | /// If all pixels are BLANK (and we are testing for BLANKs), the |
---|
| 52 | /// reconstruction will simply give BLANKs back, so we return the |
---|
| 53 | /// input array as the output array. |
---|
| 54 | /// |
---|
| 55 | /// \param xdim The length of the spectrum. |
---|
| 56 | /// \param input The input spectrum. |
---|
| 57 | /// \param output The returned reconstructed spectrum. This array needs to |
---|
| 58 | /// be declared beforehand. |
---|
| 59 | /// \param par The Param set. |
---|
[3] | 60 | |
---|
[378] | 61 | const float SNR_THRESH=par.getAtrousCut(); |
---|
[846] | 62 | const unsigned int MIN_SCALE=par.getMinScale(); |
---|
[378] | 63 | static bool firstTime = true; |
---|
[3] | 64 | |
---|
[884] | 65 | unsigned int numScales = par.filter().getNumScales(xdim); |
---|
| 66 | unsigned int maxScale = par.getMaxScale(); |
---|
[378] | 67 | if((maxScale>0)&&(maxScale<=numScales)) |
---|
| 68 | maxScale = std::min(maxScale,numScales); |
---|
| 69 | else{ |
---|
| 70 | if((firstTime)&&(maxScale!=0)){ |
---|
| 71 | firstTime=false; |
---|
[913] | 72 | DUCHAMPWARN("Reading parameters","The requested value of the parameter scaleMax, \"" << maxScale << "\" is outside the allowed range (1-"<< numScales <<") -- setting to " << numScales); |
---|
[378] | 73 | } |
---|
| 74 | maxScale = numScales; |
---|
| 75 | } |
---|
| 76 | double *sigmaFactors = new double[numScales+1]; |
---|
[846] | 77 | for(size_t i=0;i<=numScales;i++){ |
---|
[378] | 78 | if(i<=par.filter().maxFactor(1)) |
---|
| 79 | sigmaFactors[i] = par.filter().sigmaFactor(1,i); |
---|
| 80 | else sigmaFactors[i] = sigmaFactors[i-1] / sqrt(2.); |
---|
| 81 | } |
---|
[3] | 82 | |
---|
[894] | 83 | float mean,originalSigma,oldsigma,newsigma; |
---|
[378] | 84 | bool *isGood = new bool[xdim]; |
---|
[846] | 85 | size_t goodSize=0; |
---|
| 86 | for(size_t pos=0;pos<xdim;pos++) { |
---|
[378] | 87 | isGood[pos] = !par.isBlank(input[pos]); |
---|
| 88 | if(isGood[pos]) goodSize++; |
---|
| 89 | } |
---|
[3] | 90 | |
---|
[378] | 91 | if(goodSize == 0){ |
---|
| 92 | // There are no good pixels -- everything is BLANK for some reason. |
---|
| 93 | // Return the input array as the output. |
---|
[3] | 94 | |
---|
[846] | 95 | for(size_t pos=0;pos<xdim; pos++) output[pos] = input[pos]; |
---|
[3] | 96 | |
---|
[378] | 97 | } |
---|
| 98 | else{ |
---|
| 99 | // Otherwise, all is good, and we continue. |
---|
[3] | 100 | |
---|
| 101 | |
---|
[378] | 102 | float *coeffs = new float[xdim]; |
---|
| 103 | float *wavelet = new float[xdim]; |
---|
[849] | 104 | // float *residual = new float[xdim]; |
---|
[3] | 105 | |
---|
[846] | 106 | for(size_t pos=0;pos<xdim;pos++) output[pos]=0.; |
---|
[3] | 107 | |
---|
[378] | 108 | int filterHW = par.filter().width()/2; |
---|
| 109 | double *filter = new double[par.filter().width()]; |
---|
[846] | 110 | for(size_t i=0;i<par.filter().width();i++) filter[i] = par.filter().coeff(i); |
---|
[231] | 111 | |
---|
| 112 | |
---|
[378] | 113 | // No trimming done in 1D case. |
---|
[3] | 114 | |
---|
[378] | 115 | int iteration=0; |
---|
| 116 | newsigma = 1.e9; |
---|
| 117 | do{ |
---|
[231] | 118 | if(par.isVerbose()) { |
---|
[378] | 119 | std::cout << "Iteration #"<<++iteration<<":"; |
---|
| 120 | printSpace(13); |
---|
[231] | 121 | } |
---|
[378] | 122 | // first, get the value of oldsigma and set it to the previous |
---|
| 123 | // newsigma value |
---|
| 124 | oldsigma = newsigma; |
---|
| 125 | // all other times round, we are transforming the residual array |
---|
[846] | 126 | for(size_t i=0;i<xdim;i++) coeffs[i] = input[i] - output[i]; |
---|
[378] | 127 | |
---|
[849] | 128 | // findMedianStats(input,xdim,isGood,originalMean,originalSigma); |
---|
| 129 | // originalSigma = madfmToSigma(originalSigma); |
---|
| 130 | if(par.getFlagRobustStats()) |
---|
| 131 | originalSigma = madfmToSigma(findMADFM(input,isGood,xdim)); |
---|
| 132 | else |
---|
[889] | 133 | originalSigma = findStddev<float>(input,isGood,xdim); |
---|
[231] | 134 | |
---|
[378] | 135 | int spacing = 1; |
---|
[846] | 136 | for(unsigned int scale = 1; scale<=numScales; scale++){ |
---|
[231] | 137 | |
---|
[378] | 138 | if(par.isVerbose()) { |
---|
| 139 | std::cout << "Scale " << std::setw(2) << scale |
---|
| 140 | << " /" << std::setw(2) << numScales <<std::flush; |
---|
| 141 | } |
---|
| 142 | |
---|
[884] | 143 | for(size_t xpos = 0; xpos<xdim; xpos++){ |
---|
[378] | 144 | // loops over each pixel in the image |
---|
| 145 | |
---|
[884] | 146 | wavelet[xpos] = coeffs[xpos]; |
---|
[3] | 147 | |
---|
[884] | 148 | if(!isGood[xpos] ) wavelet[xpos] = 0.; |
---|
[378] | 149 | else{ |
---|
[3] | 150 | |
---|
[378] | 151 | for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){ |
---|
[846] | 152 | long x = xpos + spacing*xoffset; |
---|
[3] | 153 | |
---|
[846] | 154 | while((x<0)||(x>=long(xdim))){ |
---|
[378] | 155 | // boundary conditions are reflection. |
---|
| 156 | if(x<0) x = 0 - x; |
---|
[846] | 157 | else if(x>=long(xdim)) x = 2*(xdim-1) - x; |
---|
[378] | 158 | } |
---|
[3] | 159 | |
---|
[846] | 160 | size_t filterpos = (xoffset+filterHW); |
---|
| 161 | size_t oldpos = x; |
---|
[3] | 162 | |
---|
[378] | 163 | if(isGood[oldpos]) |
---|
[884] | 164 | wavelet[xpos] -= filter[filterpos]*coeffs[oldpos]; |
---|
[3] | 165 | |
---|
[378] | 166 | } //-> end of xoffset loop |
---|
[884] | 167 | } //-> end of else{ ( from if(!isGood[xpos]) ) |
---|
[3] | 168 | |
---|
[378] | 169 | } //-> end of xpos loop |
---|
[3] | 170 | |
---|
[378] | 171 | // Need to do this after we've done *all* the convolving |
---|
[846] | 172 | for(size_t pos=0;pos<xdim;pos++) coeffs[pos] = coeffs[pos] - wavelet[pos]; |
---|
[3] | 173 | |
---|
[378] | 174 | // Have found wavelet coeffs for this scale -- now threshold |
---|
| 175 | if(scale>=MIN_SCALE){ |
---|
[849] | 176 | // findMedianStats(wavelet,xdim,isGood,mean,sigma); |
---|
| 177 | if(par.getFlagRobustStats()) |
---|
[889] | 178 | mean = findMedian<float>(wavelet,isGood,xdim); |
---|
[849] | 179 | else |
---|
[889] | 180 | mean = findMean<float>(wavelet,isGood,xdim); |
---|
[3] | 181 | |
---|
[846] | 182 | for(size_t pos=0;pos<xdim;pos++){ |
---|
[378] | 183 | // preserve the Blank pixel values in the output. |
---|
| 184 | if(!isGood[pos]) output[pos] = input[pos]; |
---|
| 185 | else if( fabs(wavelet[pos]) > |
---|
| 186 | (mean+SNR_THRESH*originalSigma*sigmaFactors[scale]) ) |
---|
| 187 | output[pos] += wavelet[pos]; |
---|
| 188 | } |
---|
[231] | 189 | } |
---|
[3] | 190 | |
---|
[378] | 191 | spacing *= 2; |
---|
[3] | 192 | |
---|
[378] | 193 | } //-> end of scale loop |
---|
[3] | 194 | |
---|
[378] | 195 | // Only add the final smoothed array if we are doing *all* the scales. |
---|
| 196 | if(numScales == par.filter().getNumScales(xdim)) |
---|
[846] | 197 | for(size_t pos=0;pos<xdim;pos++) |
---|
[378] | 198 | if(isGood[pos]) output[pos] += coeffs[pos]; |
---|
[3] | 199 | |
---|
[849] | 200 | // for(size_t pos=0;pos<xdim;pos++) residual[pos]=input[pos]-output[pos]; |
---|
| 201 | // findMedianStats(residual,xdim,isGood,mean,newsigma); |
---|
| 202 | // newsigma = madfmToSigma(newsigma); |
---|
| 203 | if(par.getFlagRobustStats()) |
---|
| 204 | newsigma = madfmToSigma(findMADFMDiff(input,output,isGood,xdim)); |
---|
| 205 | else |
---|
[889] | 206 | newsigma = findStddevDiff<float>(input,output,isGood,xdim); |
---|
[3] | 207 | |
---|
[378] | 208 | if(par.isVerbose()) printBackSpace(26); |
---|
[3] | 209 | |
---|
[378] | 210 | } while( (iteration==1) || |
---|
| 211 | (fabs(oldsigma-newsigma)/newsigma > reconTolerance) ); |
---|
[3] | 212 | |
---|
[378] | 213 | if(par.isVerbose()) std::cout << "Completed "<<iteration<<" iterations. "; |
---|
[3] | 214 | |
---|
[378] | 215 | delete [] filter; |
---|
[849] | 216 | // delete [] residual; |
---|
[650] | 217 | delete [] wavelet; |
---|
[378] | 218 | delete [] coeffs; |
---|
[231] | 219 | |
---|
[378] | 220 | } |
---|
| 221 | |
---|
| 222 | delete [] isGood; |
---|
| 223 | delete [] sigmaFactors; |
---|
[231] | 224 | } |
---|
| 225 | |
---|
[3] | 226 | } |
---|