source: tags/release-1.1/src/Devel/sigma_factors.cc @ 1391

Last change on this file since 1391 was 301, checked in by Matthew Whiting, 17 years ago

Mostly adding the distribution text to the start of files, with a few additional comments added too.

File size: 10.8 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 <ATrous/atrous.hh>
32#include <ATrous/filter.hh>
33#define DEBUG true
34
35void getSigmaFactors1DNew(int &numScales)
36{
37//   extern Filter reconFilter;
38//   std::cerr<<"-->"<<reconFilter.getName()<<"<--"<<std::endl; 
39//  std::cerr << "-->"<<Filter::getName()<<"<--"<<std::endl;
40//  std::cerr << "-->"<<getNumScales(125)<<"<--"<<std::endl;
41 
42//   int filterHW = reconFilter.width()/2;
43//  int filterwidth = Filter::width();
44//  int filterHW = Filter::width()/2;
45  int filterwidth=1;
46  int filterHW=filterwidth/2;
47
48  // starting values:
49  long xdim = 1;
50  long size = xdim;
51  double *c = new double[size];
52  double *w = new double[size];
53  w[0] = 1.;
54
55  double *filter = new double[filterwidth];
56//   for(int i=0;i<filterwidth;i++) filter[i] = reconFilter.coeff(i);
57//  for(int i=0;i<filterwidth;i++) filter[i] = Filter::coeff(i);
58 
59  int spacing = 1;
60//   const float FACTOR = 16.;
61//   const float FACTOR = 1./reconFilter.coeff(0);
62  const float FACTOR = 1.;
63  const float FACFAC = FACTOR*FACTOR;
64
65  if(DEBUG){
66    std::cout << "divide by " << pow(FACTOR,0) << std::endl;
67    for(int i=0;i<size;i++){
68      std::cout<<i<<" "<<w[i]<<std::endl;
69    }
70    std::cout<<"===============\n";
71  }
72
73  for(int scale = 1; scale<=numScales; scale++){
74
75    // re-size and incorporate previous w into new c
76    long oldsize = size;
77    long oldxdim = xdim;
78//     xdim = 2 * xdim + 3;
79//     xdim = reconFilter.getMaxSize(scale);
80    xdim += filterHW*long(pow(2,scale));
81    size = xdim;
82    double *newc = new double[size];
83    for(int i=0;i<size;i++){
84      int oldpos = i - xdim/2;
85      if(// (oldpos<oldsize)&&(oldpos>=0)
86         abs(oldpos)<=oldsize/2) newc[i] = w[oldpos+oldsize/2] * FACTOR;
87      else newc[i] = 0.;
88     
89      if(DEBUG) std::cout<<i<<" "<<oldpos<<" "<<size<<" "<<oldsize<<" "<<oldsize/2<<"   "
90                         <<newc[i]// *pow(16,scale-1)
91                         <<std::endl;
92    }
93    if(DEBUG) std::cout<<"---------------\nspacing = " << spacing << std::endl;
94    delete [] c;
95    c = newc;
96    double *neww = new double[size];
97    delete [] w;
98    w = neww;
99    for(int i=0;i<size;i++) w[i] = 0.;
100
101    long double sum = 0.;
102
103    for(int xpos = 0; xpos<xdim; xpos++){
104      // loops over each pixel in the image
105      w[xpos] = 0;
106
107      for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
108        int x = xpos + spacing*xoffset;
109        int filterpos = (xoffset+filterHW);
110        if((x>=0)&&(x<xdim))  // if outside boundary --> set to 0.
111          w[xpos] += filter[filterpos] * c[x];
112      } //-> end of xoffset loop
113       
114      sum += (c[xpos]-w[xpos])*(c[xpos]-w[xpos]) / pow(FACFAC,scale);
115
116    } //-> end of xpos loop
117
118    if(DEBUG){
119      std::cout << "divide by " << pow(FACTOR,scale) << std::endl;
120      for(int i=0;i<size;i++){
121        std::cout<<i<<" "<<w[i]// *pow(16,scale)
122                 <<std::endl;
123      }
124      std::cout<<"===============\n";
125    }
126
127    std::cerr << "Scale "<<std::setw(2)<<scale<<":";
128    std::cerr.setf(std::ios::scientific);
129    std::cerr.setf(std::ios::left);
130    std::cerr <<"\t1D = "<<std::setw(15)<<std::setprecision(9)<<sqrt(sum)<<std::endl;
131    std::cerr.unsetf(std::ios::left);
132     
133
134    spacing *= 2;
135
136  } //-> end of scale loop
137
138}
139
140void getSigmaFactors2DNew(int &numScales)
141{
142/*
143
144 
145  extern Filter reconFilter;
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  long xdim = 1;
158  long size = xdim*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 = 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    long oldsize = size;
173    long oldxdim = xdim;
174//     xdim = 2 * oldxdim + 3;
175    xdim += filterHW*long(pow(2,scale));
176    size = xdim * xdim;
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
247
248void getSigmaFactors3DNew(int &numScales)
249{
250  /* 
251  extern Filter reconFilter;
252  int filterHW = reconFilter.width()/2;
253  int fsize = reconFilter.width()*reconFilter.width()*reconFilter.width();
254  double *filter = new double[fsize];
255  for(int i=0;i<reconFilter.width();i++){
256    for(int j=0;j<reconFilter.width();j++){
257      for(int k=0;k<reconFilter.width();k++){
258        filter[i +j*reconFilter.width() + k*reconFilter.width()*reconFilter.width()] =
259          reconFilter.coeff(i) * reconFilter.coeff(j) * reconFilter.coeff(k);
260      }
261    }
262  }
263
264  // starting values:
265  long xdim = 1;
266  long size = xdim*xdim*xdim;
267  double *c = new double[size];
268  double *w = new double[size];
269  int x,y,z,centrepos;
270  w[0] = 1.;
271 
272  int spacing = 1;
273//   const float FACTOR = 16.*16.*16.;
274  const float FACTOR = 1.;
275  const float FACFAC = FACTOR * FACTOR;
276
277  for(int scale = 1; scale<=numScales; scale++){
278
279    // re-size and incorporate previous w into new c
280    long oldsize = size;
281    long oldxdim = xdim;
282//     xdim = 2 * xdim + 3;
283    xdim += filterHW*long(pow(2,scale));
284    size = xdim * xdim * xdim;
285    double *newc = new double[size];
286    for(int i=0;i<size;i++){
287      x = i%xdim;
288      y = (i/xdim) % xdim;
289      z = i / (xdim*xdim);
290      centrepos = 0;
291      if((abs(x-xdim/2)<=oldxdim/2)&&
292         (abs(y-xdim/2)<=oldxdim/2)&&
293         (abs(z-xdim/2)<=oldxdim/2)){
294        centrepos = x-xdim/2+oldxdim/2 + oldxdim*(y-xdim/2+oldxdim/2)
295          + oldxdim*oldxdim*(z-xdim/2+oldxdim/2);
296        newc[i] = w[centrepos] * FACTOR;
297      }
298      else newc[i] = 0.;
299//       std::cout<<i<<" "<<x<<" "<<y<<" "<<z<<" "<<oldpos<<" "<<size<<" "<<oldsize
300//             <<"   "<<newc[i]*pow(16,3*(scale-1))<<std::endl;
301    }
302//     std::cout<<"---------------\n";
303    delete [] c;
304    c = newc;
305    double *neww = new double[size];
306    delete [] w;
307    w = neww;
308    for(int i=0;i<size;i++) w[i] = 0.;
309    long double sum = 0.;
310
311    for(int zpos = 0; zpos<xdim; zpos++){
312      for(int ypos = 0; ypos<xdim; ypos++){
313        for(int xpos = 0; xpos<xdim; xpos++){
314          // loops over each pixel in the image
315          int pos = zpos*xdim*xdim + ypos*xdim + xpos;
316          w[pos] = 0;
317
318          for(int zoffset=-filterHW; zoffset<=filterHW; zoffset++){
319            z = zpos + spacing*zoffset;
320            for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
321              y = ypos + spacing*yoffset;
322              for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
323                x = xpos + spacing*xoffset;
324
325                int filterpos = (zoffset+filterHW)*reconFilter.width()*reconFilter.width() +
326                  (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
327                int oldpos = z*xdim*xdim + y*xdim + x;
328
329                // only consider points inside boundary -- ie. =0 outside
330                if((z>=0)&&(z<xdim)&&(y>=0)&&(y<xdim)&&(x>=0)&&(x<xdim))
331                  w[pos] += filter[filterpos] * c[oldpos];
332
333              } //-> end of xoffset loop
334            } //-> end of yoffset loop
335          } //-> end of zoffset loop
336 
337          sum += (c[pos]-w[pos])*(c[pos]-w[pos]) / pow(FACFAC,scale);
338           
339        } //-> end of xpos loop
340      } //-> end of ypos loop
341    } //-> end of zpos loop
342
343//     for(int i=0;i<size;i++){
344//       std::cout<<i<<" "<<w[i]*pow(16,3*scale)<<std::endl;
345//     }
346//     std::cout<<"===============\n";
347
348    std::cerr << "Scale "<<std::setw(2)<<scale<<":";
349    std::cerr.setf(std::ios::scientific);
350    std::cerr.setf(std::ios::left);
351    std::cerr <<"\t3D = "<<std::setw(11)<<std::setprecision(9)<<sqrt(sum)<<std::endl;
352    std::cerr.unsetf(std::ios::left);
353
354    spacing *= 2;
355
356  } //-> end of scale loop
357
358*/
359
360
361}
Note: See TracBrowser for help on using the repository browser.