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