source: trunk/src/Devel/sigma_factors.cc @ 860

Last change on this file since 860 was 860, checked in by MatthewWhiting, 13 years ago

Various tweaks to Devel code, including getting the sigma_factors calculator to work properly.

File size: 11.0 KB
Line 
1// -----------------------------------------------------------------------
2// sigma_factors.cc: Functions to calculate the scaling factors for
3//                   the rms in the case of wavelet reconstruction.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <iostream>
30#include <iomanip>
31#include <math.h>
32#include <duchamp/ATrous/atrous.hh>
33#include <duchamp/ATrous/filter.hh>
34#define DEBUG true
35//#define DEBUG false
36
37void getSigmaFactors1DNew(duchamp::Filter &reconFilter, int &numScales)
38{
39  std::cerr<<"-->"<<reconFilter.getName()<<"<--"<<std::endl; 
40//  std::cerr << "-->"<<Filter::getName()<<"<--"<<std::endl;
41//  std::cerr << "-->"<<getNumScales(125)<<"<--"<<std::endl;
42 
43  int filterwidth = reconFilter.width();
44   // int filterHW = reconFilter.width()/2;
45//  int filterwidth = Filter::width();
46//  int filterHW = Filter::width()/2;
47//  int filterwidth=1;
48  int filterHW=filterwidth/2;
49
50  // starting values:
51  long xdim = 1;
52  long size = xdim;
53  double *c = new double[size];
54  double *w = new double[size];
55  w[0] = 1.;
56
57  double *filter = new double[filterwidth];
58  for(int i=0;i<filterwidth;i++) filter[i] = reconFilter.coeff(i);
59//  for(int i=0;i<filterwidth;i++) filter[i] = Filter::coeff(i);
60 
61  int spacing = 1;
62//   const float FACTOR = 16.;
63  const float FACTOR = reconFilter.coeff(0)==0?1.:1./reconFilter.coeff(0);
64  // const float FACTOR = 1.;
65  const float FACFAC = FACTOR*FACTOR;
66
67  if(DEBUG){
68    std::cout << "divide by " << pow(FACTOR,0) << std::endl;
69    for(int i=0;i<size;i++){
70      std::cout<<i<<" "<<w[i]<<std::endl;
71    }
72    std::cout<<"===============\n";
73  }
74
75  for(int scale = 1; scale<=numScales; scale++){
76
77    // re-size and incorporate previous w into new c
78    long oldsize = size;
79    long oldxdim = xdim;
80//     xdim = 2 * xdim + 3;
81//     xdim = reconFilter.getMaxSize(scale);
82    xdim += filterHW*long(pow(2,scale));
83    size = xdim;
84    double *newc = new double[size];
85    for(int i=0;i<size;i++){
86      int oldpos = i - xdim/2;
87      if(// (oldpos<oldsize)&&(oldpos>=0)
88         abs(oldpos)<=oldsize/2) newc[i] = w[oldpos+oldsize/2] * FACTOR;
89      else newc[i] = 0.;
90     
91      if(DEBUG) std::cout<<i<<" "<<oldpos<<" "<<size<<" "<<oldsize<<" "<<oldsize/2<<"   "
92                         <<newc[i]// *pow(16,scale-1)
93                         <<std::endl;
94    }
95    if(DEBUG) std::cout<<"---------------\nspacing = " << spacing << std::endl;
96    delete [] c;
97    c = newc;
98    double *neww = new double[size];
99    delete [] w;
100    w = neww;
101    for(int i=0;i<size;i++) w[i] = 0.;
102
103    long double sum = 0.;
104
105    for(int xpos = 0; xpos<xdim; xpos++){
106      // loops over each pixel in the image
107      w[xpos] = 0;
108
109      for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
110        int x = xpos + spacing*xoffset;
111        int filterpos = (xoffset+filterHW);
112        if((x>=0)&&(x<xdim))  // if outside boundary --> set to 0.
113          w[xpos] += filter[filterpos] * c[x];
114      } //-> end of xoffset loop
115       
116      sum += (c[xpos]-w[xpos])*(c[xpos]-w[xpos]) / pow(FACFAC,scale);
117
118    } //-> end of xpos loop
119
120    if(DEBUG){
121      std::cout << "divide by " << pow(FACTOR,scale) << std::endl;
122      for(int i=0;i<size;i++){
123        std::cout<<i<<" "<<w[i]// *pow(16,scale)
124                 <<std::endl;
125      }
126      std::cout<<"===============\n";
127    }
128
129    std::cerr << "Scale "<<std::setw(2)<<scale<<":";
130    std::cerr.setf(std::ios::scientific);
131    std::cerr.setf(std::ios::left);
132    std::cerr <<"\t1D = "<<std::setw(15)<<std::setprecision(9)<<sqrt(sum)<<std::endl;
133    std::cerr.unsetf(std::ios::left);
134     
135
136    spacing *= 2;
137
138  } //-> end of scale loop
139
140}
141
142void getSigmaFactors2DNew(duchamp::Filter &reconFilter, int &numScales)
143{
144
145  int filterHW = reconFilter.width()/2;
146  int fsize = reconFilter.width()*reconFilter.width();
147  double *filter = new double[fsize];
148  for(int i=0;i<reconFilter.width();i++){
149    for(int j=0;j<reconFilter.width();j++){
150      filter[i +j*reconFilter.width()] = reconFilter.coeff(i) * reconFilter.coeff(j);
151    }
152  }
153
154 
155  // starting values:
156  size_t xdim = 1;
157  size_t size = xdim*xdim;
158  double *c = new double[size];
159  double *w = new double[size];
160  w[0] = 1.;
161
162  int spacing = 1;
163   const float FACTOR = reconFilter.coeff(0)==0?1.:1./(reconFilter.coeff(0)*reconFilter.coeff(0));
164   // const float FACTOR = 16.*16.;
165  // const float FACTOR = 1.;
166  const float FACFAC = FACTOR * FACTOR;
167
168  for(int scale = 1; scale<=numScales; scale++){
169
170    // re-size and incorporate previous w into new c
171    size_t oldsize = size;
172    size_t oldxdim = xdim;
173//     xdim = 2 * oldxdim + 3;
174    xdim += filterHW*long(pow(2,scale));
175    size = xdim * xdim;
176    //    std::cerr << xdim << " " << size << " " << size*sizeof(float) << "\n";
177    double *newc = new double[size];
178    for(int i=0;i<size;i++){
179      int x = i%xdim;
180      int y = i/xdim;
181      int centrepos=0;
182      if((abs(x-xdim/2)<=oldxdim/2)&&(abs(y-xdim/2)<=oldxdim/2)){
183        centrepos = x-xdim/2+oldxdim/2 + oldxdim*(y-xdim/2+oldxdim/2);
184        newc[i] = w[centrepos] * FACTOR;
185      }
186      else newc[i] = 0.;
187       // std::cout<<i<<" "<<x<<" "<<y<<" "<<centrepos<<" "<<xdim<<" "<<size
188       //              <<" "<<oldxdim<<" "<<oldsize<<"   "<<newc[i]*pow(256,scale-1)<<std::endl;
189    }
190//     std::cout<<"---------------\n";
191    delete [] c;
192    c = newc;
193//     std::cerr<<".";
194    double *neww = new double[size];
195    delete [] w;
196    w = neww;
197    for(int i=0;i<size;i++) w[i] = 0.;
198//     std::cerr<<".";
199
200    long double sum = 0.;
201
202    for(int ypos = 0; ypos<xdim; ypos++){
203      for(int xpos = 0; xpos<xdim; xpos++){
204        // loops over each pixel in the image
205        int pos = ypos*xdim + xpos;
206        w[pos] = 0;
207
208        for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
209          int y = ypos + spacing*yoffset;
210
211          for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
212            int x = xpos + spacing*xoffset;
213
214            int filterpos = (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
215            int oldpos = y*xdim + x;
216                   
217            if((y>=0)&&(y<xdim)&&(x>=0)&&(x<xdim))
218              w[pos] += filter[filterpos] * c[oldpos];
219
220          } //-> end of xoffset loop
221        } //-> end of yoffset loop
222 
223        sum += (c[pos]-w[pos])*(c[pos]-w[pos]) / pow(FACFAC,scale);
224           
225      } //-> end of xpos loop
226    } //-> end of ypos loop
227
228//     for(int i=0;i<size;i++){
229//       std::cout<<i<<" "<<i%xdim<<" "<<i/xdim<<" "<<w[i]*pow(256,scale)<<std::endl;
230//     }
231//     std::cout<<"===============\n";
232
233    std::cerr << "Scale "<<std::setw(2)<<scale<<":";
234    std::cerr.setf(std::ios::scientific);
235    std::cerr.setf(std::ios::left);
236    std::cerr <<"\t2D = "<<std::setw(11)<<std::setprecision(9)<<sqrt(sum)<<std::endl;
237    std::cerr.unsetf(std::ios::left);
238     
239    spacing *= 2;
240
241  } //-> end of scale loop
242
243
244}
245
246
247void getSigmaFactors3DNew(duchamp::Filter &reconFilter, int &numScales)
248{
249 
250  int filterHW = reconFilter.width()/2;
251  int fsize = reconFilter.width()*reconFilter.width()*reconFilter.width();
252  double *filter = new double[fsize];
253  for(int i=0;i<reconFilter.width();i++){
254    for(int j=0;j<reconFilter.width();j++){
255      for(int k=0;k<reconFilter.width();k++){
256        filter[i +j*reconFilter.width() + k*reconFilter.width()*reconFilter.width()] =
257          reconFilter.coeff(i) * reconFilter.coeff(j) * reconFilter.coeff(k);
258      }
259    }
260  }
261
262  // starting values:
263  long xdim = 1;
264  long size = xdim*xdim*xdim;
265  double *c = new double[size];
266  double *w = new double[size];
267  int x,y,z,centrepos;
268  w[0] = 1.;
269 
270  int spacing = 1;
271//   const float FACTOR = 16.*16.*16.;
272  const float FACTOR = 1.;
273  const float FACFAC = FACTOR * FACTOR;
274
275  for(int scale = 1; scale<=numScales; scale++){
276
277    // re-size and incorporate previous w into new c
278    long oldsize = size;
279    long oldxdim = xdim;
280//     xdim = 2 * xdim + 3;
281    xdim += filterHW*long(pow(2,scale));
282    size = xdim * xdim * xdim;
283    double *newc = new double[size];
284    for(int i=0;i<size;i++){
285      x = i%xdim;
286      y = (i/xdim) % xdim;
287      z = i / (xdim*xdim);
288      centrepos = 0;
289      if((abs(x-xdim/2)<=oldxdim/2)&&
290         (abs(y-xdim/2)<=oldxdim/2)&&
291         (abs(z-xdim/2)<=oldxdim/2)){
292        centrepos = x-xdim/2+oldxdim/2 + oldxdim*(y-xdim/2+oldxdim/2)
293          + oldxdim*oldxdim*(z-xdim/2+oldxdim/2);
294        newc[i] = w[centrepos] * FACTOR;
295      }
296      else newc[i] = 0.;
297//       std::cout<<i<<" "<<x<<" "<<y<<" "<<z<<" "<<oldpos<<" "<<size<<" "<<oldsize
298//             <<"   "<<newc[i]*pow(16,3*(scale-1))<<std::endl;
299    }
300//     std::cout<<"---------------\n";
301    delete [] c;
302    c = newc;
303    double *neww = new double[size];
304    delete [] w;
305    w = neww;
306    for(int i=0;i<size;i++) w[i] = 0.;
307    long double sum = 0.;
308
309    for(int zpos = 0; zpos<xdim; zpos++){
310      for(int ypos = 0; ypos<xdim; ypos++){
311        for(int xpos = 0; xpos<xdim; xpos++){
312          // loops over each pixel in the image
313          int pos = zpos*xdim*xdim + ypos*xdim + xpos;
314          w[pos] = 0;
315
316          for(int zoffset=-filterHW; zoffset<=filterHW; zoffset++){
317            z = zpos + spacing*zoffset;
318            for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
319              y = ypos + spacing*yoffset;
320              for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
321                x = xpos + spacing*xoffset;
322
323                int filterpos = (zoffset+filterHW)*reconFilter.width()*reconFilter.width() +
324                  (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
325                int oldpos = z*xdim*xdim + y*xdim + x;
326
327                // only consider points inside boundary -- ie. =0 outside
328                if((z>=0)&&(z<xdim)&&(y>=0)&&(y<xdim)&&(x>=0)&&(x<xdim))
329                  w[pos] += filter[filterpos] * c[oldpos];
330
331              } //-> end of xoffset loop
332            } //-> end of yoffset loop
333          } //-> end of zoffset loop
334 
335          sum += (c[pos]-w[pos])*(c[pos]-w[pos]) / pow(FACFAC,scale);
336           
337        } //-> end of xpos loop
338      } //-> end of ypos loop
339    } //-> end of zpos loop
340
341//     for(int i=0;i<size;i++){
342//       std::cout<<i<<" "<<w[i]*pow(16,3*scale)<<std::endl;
343//     }
344//     std::cout<<"===============\n";
345
346    std::cerr << "Scale "<<std::setw(2)<<scale<<":";
347    std::cerr.setf(std::ios::scientific);
348    std::cerr.setf(std::ios::left);
349    std::cerr <<"\t3D = "<<std::setw(11)<<std::setprecision(9)<<sqrt(sum)<<std::endl;
350    std::cerr.unsetf(std::ios::left);
351
352    spacing *= 2;
353
354  } //-> end of scale loop
355
356
357
358
359}
Note: See TracBrowser for help on using the repository browser.