source: tags/release-1.2.2/src/Devel/sigma_factors.cc

Last change on this file was 876, checked in by MatthewWhiting, 12 years ago

Getting include statements right for the Devel part

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 <stdlib.h>
33#include <duchamp/ATrous/atrous.hh>
34#include <duchamp/ATrous/filter.hh>
35#define DEBUG true
36//#define DEBUG false
37
38void getSigmaFactors1DNew(duchamp::Filter &reconFilter, int &numScales)
39{
40  std::cerr<<"-->"<<reconFilter.getName()<<"<--"<<std::endl; 
41//  std::cerr << "-->"<<Filter::getName()<<"<--"<<std::endl;
42//  std::cerr << "-->"<<getNumScales(125)<<"<--"<<std::endl;
43 
44  int filterwidth = reconFilter.width();
45   // int filterHW = reconFilter.width()/2;
46//  int filterwidth = Filter::width();
47//  int filterHW = Filter::width()/2;
48//  int filterwidth=1;
49  int filterHW=filterwidth/2;
50
51  // starting values:
52  long xdim = 1;
53  long size = xdim;
54  double *c = new double[size];
55  double *w = new double[size];
56  w[0] = 1.;
57
58  double *filter = new double[filterwidth];
59  for(int i=0;i<filterwidth;i++) filter[i] = reconFilter.coeff(i);
60//  for(int i=0;i<filterwidth;i++) filter[i] = Filter::coeff(i);
61 
62  int spacing = 1;
63//   const float FACTOR = 16.;
64  const float FACTOR = reconFilter.coeff(0)==0?1.:1./reconFilter.coeff(0);
65  // const float FACTOR = 1.;
66  const float FACFAC = FACTOR*FACTOR;
67
68  if(DEBUG){
69    std::cout << "divide by " << pow(FACTOR,0) << std::endl;
70    for(int i=0;i<size;i++){
71      std::cout<<i<<" "<<w[i]<<std::endl;
72    }
73    std::cout<<"===============\n";
74  }
75
76  for(int scale = 1; scale<=numScales; scale++){
77
78    // re-size and incorporate previous w into new c
79    long oldsize = size;
80    long oldxdim = xdim;
81//     xdim = 2 * xdim + 3;
82//     xdim = reconFilter.getMaxSize(scale);
83    xdim += filterHW*long(pow(2,scale));
84    size = xdim;
85    double *newc = new double[size];
86    for(int i=0;i<size;i++){
87      int oldpos = i - xdim/2;
88      if(// (oldpos<oldsize)&&(oldpos>=0)
89         abs(oldpos)<=oldsize/2) newc[i] = w[oldpos+oldsize/2] * FACTOR;
90      else newc[i] = 0.;
91     
92      if(DEBUG) std::cout<<i<<" "<<oldpos<<" "<<size<<" "<<oldsize<<" "<<oldsize/2<<"   "
93                         <<newc[i]// *pow(16,scale-1)
94                         <<std::endl;
95    }
96    if(DEBUG) std::cout<<"---------------\nspacing = " << spacing << std::endl;
97    delete [] c;
98    c = newc;
99    double *neww = new double[size];
100    delete [] w;
101    w = neww;
102    for(int i=0;i<size;i++) w[i] = 0.;
103
104    long double sum = 0.;
105
106    for(int xpos = 0; xpos<xdim; xpos++){
107      // loops over each pixel in the image
108      w[xpos] = 0;
109
110      for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
111        int x = xpos + spacing*xoffset;
112        int filterpos = (xoffset+filterHW);
113        if((x>=0)&&(x<xdim))  // if outside boundary --> set to 0.
114          w[xpos] += filter[filterpos] * c[x];
115      } //-> end of xoffset loop
116       
117      sum += (c[xpos]-w[xpos])*(c[xpos]-w[xpos]) / pow(FACFAC,scale);
118
119    } //-> end of xpos loop
120
121    if(DEBUG){
122      std::cout << "divide by " << pow(FACTOR,scale) << std::endl;
123      for(int i=0;i<size;i++){
124        std::cout<<i<<" "<<w[i]// *pow(16,scale)
125                 <<std::endl;
126      }
127      std::cout<<"===============\n";
128    }
129
130    std::cerr << "Scale "<<std::setw(2)<<scale<<":";
131    std::cerr.setf(std::ios::scientific);
132    std::cerr.setf(std::ios::left);
133    std::cerr <<"\t1D = "<<std::setw(15)<<std::setprecision(9)<<sqrt(sum)<<std::endl;
134    std::cerr.unsetf(std::ios::left);
135     
136
137    spacing *= 2;
138
139  } //-> end of scale loop
140
141}
142
143void getSigmaFactors2DNew(duchamp::Filter &reconFilter, int &numScales)
144{
145
146  int filterHW = reconFilter.width()/2;
147  int fsize = reconFilter.width()*reconFilter.width();
148  double *filter = new double[fsize];
149  for(int i=0;i<reconFilter.width();i++){
150    for(int j=0;j<reconFilter.width();j++){
151      filter[i +j*reconFilter.width()] = reconFilter.coeff(i) * reconFilter.coeff(j);
152    }
153  }
154
155 
156  // starting values:
157  size_t xdim = 1;
158  size_t size = xdim*xdim;
159  double *c = new double[size];
160  double *w = new double[size];
161  w[0] = 1.;
162
163  int spacing = 1;
164   const float FACTOR = reconFilter.coeff(0)==0?1.:1./(reconFilter.coeff(0)*reconFilter.coeff(0));
165   // const float FACTOR = 16.*16.;
166  // const float FACTOR = 1.;
167  const float FACFAC = FACTOR * FACTOR;
168
169  for(int scale = 1; scale<=numScales; scale++){
170
171    // re-size and incorporate previous w into new c
172    size_t oldsize = size;
173    size_t oldxdim = xdim;
174//     xdim = 2 * oldxdim + 3;
175    xdim += filterHW*long(pow(2,scale));
176    size = xdim * xdim;
177    //    std::cerr << xdim << " " << size << " " << size*sizeof(float) << "\n";
178    double *newc = new double[size];
179    for(int i=0;i<size;i++){
180      int x = i%xdim;
181      int y = i/xdim;
182      int centrepos=0;
183      if((abs(x-xdim/2)<=oldxdim/2)&&(abs(y-xdim/2)<=oldxdim/2)){
184        centrepos = x-xdim/2+oldxdim/2 + oldxdim*(y-xdim/2+oldxdim/2);
185        newc[i] = w[centrepos] * FACTOR;
186      }
187      else newc[i] = 0.;
188       // std::cout<<i<<" "<<x<<" "<<y<<" "<<centrepos<<" "<<xdim<<" "<<size
189       //              <<" "<<oldxdim<<" "<<oldsize<<"   "<<newc[i]*pow(256,scale-1)<<std::endl;
190    }
191//     std::cout<<"---------------\n";
192    delete [] c;
193    c = newc;
194//     std::cerr<<".";
195    double *neww = new double[size];
196    delete [] w;
197    w = neww;
198    for(int i=0;i<size;i++) w[i] = 0.;
199//     std::cerr<<".";
200
201    long double sum = 0.;
202
203    for(int ypos = 0; ypos<xdim; ypos++){
204      for(int xpos = 0; xpos<xdim; xpos++){
205        // loops over each pixel in the image
206        int pos = ypos*xdim + xpos;
207        w[pos] = 0;
208
209        for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
210          int y = ypos + spacing*yoffset;
211
212          for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
213            int x = xpos + spacing*xoffset;
214
215            int filterpos = (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
216            int oldpos = y*xdim + x;
217                   
218            if((y>=0)&&(y<xdim)&&(x>=0)&&(x<xdim))
219              w[pos] += filter[filterpos] * c[oldpos];
220
221          } //-> end of xoffset loop
222        } //-> end of yoffset loop
223 
224        sum += (c[pos]-w[pos])*(c[pos]-w[pos]) / pow(FACFAC,scale);
225           
226      } //-> end of xpos loop
227    } //-> end of ypos loop
228
229//     for(int i=0;i<size;i++){
230//       std::cout<<i<<" "<<i%xdim<<" "<<i/xdim<<" "<<w[i]*pow(256,scale)<<std::endl;
231//     }
232//     std::cout<<"===============\n";
233
234    std::cerr << "Scale "<<std::setw(2)<<scale<<":";
235    std::cerr.setf(std::ios::scientific);
236    std::cerr.setf(std::ios::left);
237    std::cerr <<"\t2D = "<<std::setw(11)<<std::setprecision(9)<<sqrt(sum)<<std::endl;
238    std::cerr.unsetf(std::ios::left);
239     
240    spacing *= 2;
241
242  } //-> end of scale loop
243
244
245}
246
247
248void getSigmaFactors3DNew(duchamp::Filter &reconFilter, int &numScales)
249{
250 
251  int filterHW = reconFilter.width()/2;
252  int fsize = reconFilter.width()*reconFilter.width()*reconFilter.width();
253  double *filter = new double[fsize];
254  for(int i=0;i<reconFilter.width();i++){
255    for(int j=0;j<reconFilter.width();j++){
256      for(int k=0;k<reconFilter.width();k++){
257        filter[i +j*reconFilter.width() + k*reconFilter.width()*reconFilter.width()] =
258          reconFilter.coeff(i) * reconFilter.coeff(j) * reconFilter.coeff(k);
259      }
260    }
261  }
262
263  // starting values:
264  long xdim = 1;
265  long size = xdim*xdim*xdim;
266  double *c = new double[size];
267  double *w = new double[size];
268  int x,y,z,centrepos;
269  w[0] = 1.;
270 
271  int spacing = 1;
272//   const float FACTOR = 16.*16.*16.;
273  const float FACTOR = 1.;
274  const float FACFAC = FACTOR * FACTOR;
275
276  for(int scale = 1; scale<=numScales; scale++){
277
278    // re-size and incorporate previous w into new c
279    long oldsize = size;
280    long oldxdim = xdim;
281//     xdim = 2 * xdim + 3;
282    xdim += filterHW*long(pow(2,scale));
283    size = xdim * xdim * xdim;
284    double *newc = new double[size];
285    for(int i=0;i<size;i++){
286      x = i%xdim;
287      y = (i/xdim) % xdim;
288      z = i / (xdim*xdim);
289      centrepos = 0;
290      if((abs(x-xdim/2)<=oldxdim/2)&&
291         (abs(y-xdim/2)<=oldxdim/2)&&
292         (abs(z-xdim/2)<=oldxdim/2)){
293        centrepos = x-xdim/2+oldxdim/2 + oldxdim*(y-xdim/2+oldxdim/2)
294          + oldxdim*oldxdim*(z-xdim/2+oldxdim/2);
295        newc[i] = w[centrepos] * FACTOR;
296      }
297      else newc[i] = 0.;
298//       std::cout<<i<<" "<<x<<" "<<y<<" "<<z<<" "<<oldpos<<" "<<size<<" "<<oldsize
299//             <<"   "<<newc[i]*pow(16,3*(scale-1))<<std::endl;
300    }
301//     std::cout<<"---------------\n";
302    delete [] c;
303    c = newc;
304    double *neww = new double[size];
305    delete [] w;
306    w = neww;
307    for(int i=0;i<size;i++) w[i] = 0.;
308    long double sum = 0.;
309
310    for(int zpos = 0; zpos<xdim; zpos++){
311      for(int ypos = 0; ypos<xdim; ypos++){
312        for(int xpos = 0; xpos<xdim; xpos++){
313          // loops over each pixel in the image
314          int pos = zpos*xdim*xdim + ypos*xdim + xpos;
315          w[pos] = 0;
316
317          for(int zoffset=-filterHW; zoffset<=filterHW; zoffset++){
318            z = zpos + spacing*zoffset;
319            for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
320              y = ypos + spacing*yoffset;
321              for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
322                x = xpos + spacing*xoffset;
323
324                int filterpos = (zoffset+filterHW)*reconFilter.width()*reconFilter.width() +
325                  (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
326                int oldpos = z*xdim*xdim + y*xdim + x;
327
328                // only consider points inside boundary -- ie. =0 outside
329                if((z>=0)&&(z<xdim)&&(y>=0)&&(y<xdim)&&(x>=0)&&(x<xdim))
330                  w[pos] += filter[filterpos] * c[oldpos];
331
332              } //-> end of xoffset loop
333            } //-> end of yoffset loop
334          } //-> end of zoffset loop
335 
336          sum += (c[pos]-w[pos])*(c[pos]-w[pos]) / pow(FACFAC,scale);
337           
338        } //-> end of xpos loop
339      } //-> end of ypos loop
340    } //-> end of zpos loop
341
342//     for(int i=0;i<size;i++){
343//       std::cout<<i<<" "<<w[i]*pow(16,3*scale)<<std::endl;
344//     }
345//     std::cout<<"===============\n";
346
347    std::cerr << "Scale "<<std::setw(2)<<scale<<":";
348    std::cerr.setf(std::ios::scientific);
349    std::cerr.setf(std::ios::left);
350    std::cerr <<"\t3D = "<<std::setw(11)<<std::setprecision(9)<<sqrt(sum)<<std::endl;
351    std::cerr.unsetf(std::ios::left);
352
353    spacing *= 2;
354
355  } //-> end of scale loop
356
357
358
359
360}
Note: See TracBrowser for help on using the repository browser.