source: trunk/src/ATrous/atrous_1d_reconstruct.cc @ 174

Last change on this file since 174 was 174, checked in by Matthew Whiting, 18 years ago
  • Minor fixes to the code in the three atrous reconstruct functions, to make them a bit more readable.
  • Deleted the old functions in the ATrous directory (that have not been touched for ages and not used by anything).
File size: 4.7 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <math.h>
4#include <ATrous/atrous.hh>
5#include <Utils/utils.hh>
6
7using std::endl;
8using std::setw;
9
10void atrous1DReconstruct(long &xdim, float *&input,float *&output, Param &par)
11{
12  /**
13   *  atrous1DReconstruct(xdim, input, output, par)
14   *
15   *  A routine that uses the a trous wavelet method to reconstruct a
16   *   1-dimensional spectrum.
17   *  The Param object "par" contains all necessary info about the filter and
18   *   reconstruction parameters, although a Filter object has to be declared
19   *   elsewhere previously.
20   *  The input array is in "input", of length "xdim", and the reconstructed
21   *   array is in "output".
22   */
23
24
25  extern Filter reconFilter;
26  const float SNR_THRESH=par.getAtrousCut();
27  const int MIN_SCALE=par.getMinScale();
28
29  bool flagBlank=par.getFlagBlankPix();
30  float blankPixValue = par.getBlankPixVal();
31  int numScales = reconFilter.getNumScales(xdim);
32  double *sigmaFactors = new double[numScales+1];
33  for(int i=0;i<=numScales;i++){
34    if(i<=reconFilter.maxFactor(1))
35      sigmaFactors[i] = reconFilter.sigmaFactor(1,i);
36    else sigmaFactors[i] = sigmaFactors[i-1] / sqrt(2.);
37  }
38
39  float mean,sigma,originalSigma,originalMean,oldsigma,newsigma;
40  bool *isGood = new bool[xdim];
41  for(int pos=0;pos<xdim;pos++)
42    isGood[pos] = !par.isBlank(input[pos]);
43
44  float *coeffs = new float[xdim];
45  float *wavelet = new float[xdim];
46
47  for(int pos=0;pos<xdim;pos++) output[pos]=0.;
48
49  int filterHW = reconFilter.width()/2;
50  double *filter = new double[reconFilter.width()];
51  for(int i=0;i<reconFilter.width();i++) filter[i] = reconFilter.coeff(i);
52
53
54  // No trimming done in 1D case.
55
56  int iteration=0;
57  newsigma = 1.e9;
58  do{
59    if(par.isVerbose())
60      std::cout << "Iteration #"<<++iteration<<":             ";
61    // first, get the value of oldsigma and set it to the previous
62    //   newsigma value
63    oldsigma = newsigma;
64    // all other times round, we are transforming the residual array
65    for(int i=0;i<xdim;i++)  coeffs[i] = input[i] - output[i];
66   
67    float *array = new float[xdim];
68    int goodSize=0;
69    for(int i=0;i<xdim;i++) if(isGood[i]) array[goodSize++] = input[i];
70    findMedianStats(array,goodSize,originalMean,originalSigma);
71    originalSigma /= correctionFactor; // correct from MADFM to sigma estimator
72    delete [] array;
73
74    int spacing = 1;
75    for(int scale = 1; scale<=numScales; scale++){
76
77      if(par.isVerbose()) {
78        std::cout << "\b\b\b\b\b\b\b\b\b\b\b\bScale ";
79        std::cout << setw(2)<<scale<<" /"<<setw(2)<<numScales<<std::flush;
80      }
81
82      for(int xpos = 0; xpos<xdim; xpos++){
83            // loops over each pixel in the image
84        int pos = xpos;
85
86        wavelet[pos] = coeffs[pos];
87       
88        if(!isGood[pos] )  wavelet[pos] = 0.;
89        else{
90
91          for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
92            int x = xpos + spacing*xoffset;
93
94            while((x<0)||(x>=xdim)){
95              if(x<0) x = 0 - x;                    // boundary conditions are
96              else if(x>=xdim) x = 2*(xdim-1) - x;  //    reflection.
97            }
98
99            int filterpos = (xoffset+filterHW);
100            int oldpos = x;
101
102            if(isGood[oldpos])
103              wavelet[pos] -= filter[filterpos]*coeffs[oldpos];
104             
105          } //-> end of xoffset loop
106        } //-> end of else{ ( from if(!isGood[pos])  )
107           
108      } //-> end of xpos loop
109
110      // Need to do this after we've done *all* the convolving
111      for(int pos=0;pos<xdim;pos++) coeffs[pos] = coeffs[pos] - wavelet[pos];
112
113      // Have found wavelet coeffs for this scale -- now threshold
114      if(scale>=MIN_SCALE){
115        array = new float[xdim];
116        goodSize=0;
117        for(int pos=0;pos<xdim;pos++)
118          if(isGood[pos]) array[goodSize++] = wavelet[pos];
119        findMedianStats(array,goodSize,mean,sigma);
120        delete [] array;
121       
122        for(int pos=0;pos<xdim;pos++){
123          // preserve the Blank pixel values in the output.
124          if(!isGood[pos]) output[pos] = blankPixValue;
125          else if( fabs(wavelet[pos]) >
126                   (mean+SNR_THRESH*originalSigma*sigmaFactors[scale]) )
127            output[pos] += wavelet[pos];
128        }
129      }
130 
131      spacing *= 2;
132
133    } //-> end of scale loop
134
135    for(int pos=0;pos<xdim;pos++) if(isGood[pos]) output[pos] += coeffs[pos];
136
137    array = new float[xdim];
138    goodSize=0;
139    for(int i=0;i<xdim;i++)
140      if(isGood[i]) array[goodSize++] = input[i] - output[i];
141    findMedianStats(array,goodSize,mean,newsigma);
142    newsigma /= correctionFactor; // correct from MADFM to sigma estimator.
143    delete [] array;
144
145    if(par.isVerbose())
146      std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
147
148  } while( (iteration==1) ||
149           (fabs(oldsigma-newsigma)/newsigma > reconTolerance) );
150
151  if(par.isVerbose()) std::cout << "Completed "<<iteration<<" iterations. ";
152
153  delete [] coeffs;
154  delete [] wavelet;
155  delete [] isGood;
156  delete [] filter;
157  delete [] sigmaFactors;
158}
Note: See TracBrowser for help on using the repository browser.