1 | // ----------------------------------------------------------------------- |
---|
2 | // baselineSubtract.cc: Obtaining and subtracting spectral baselines. |
---|
3 | // ----------------------------------------------------------------------- |
---|
4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
5 | // |
---|
6 | // This program is free software; you can redistribute it and/or modify it |
---|
7 | // under the terms of the GNU General Public License as published by the |
---|
8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
9 | // option) any later version. |
---|
10 | // |
---|
11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
14 | // for more details. |
---|
15 | // |
---|
16 | // You should have received a copy of the GNU General Public License |
---|
17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
19 | // |
---|
20 | // Correspondence concerning Duchamp may be directed to: |
---|
21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
22 | // Postal address: Dr. Matthew Whiting |
---|
23 | // Australia Telescope National Facility, CSIRO |
---|
24 | // PO Box 76 |
---|
25 | // Epping NSW 1710 |
---|
26 | // AUSTRALIA |
---|
27 | // ----------------------------------------------------------------------- |
---|
28 | #include <iostream> |
---|
29 | #include <iomanip> |
---|
30 | #include <math.h> |
---|
31 | #include <duchamp/param.hh> |
---|
32 | #include <duchamp/ATrous/filter.hh> |
---|
33 | #include <duchamp/ATrous/atrous.hh> |
---|
34 | #include <duchamp/Utils/utils.hh> |
---|
35 | #include <duchamp/Utils/feedback.hh> |
---|
36 | |
---|
37 | namespace duchamp |
---|
38 | { |
---|
39 | |
---|
40 | void baselineSubtract(long numSpec, long specLength, float *originalCube, |
---|
41 | float *baselineValues, Param &par) |
---|
42 | { |
---|
43 | /** |
---|
44 | * A routine to find the baseline of each spectrum in a cube (the spectral |
---|
45 | * direction is assumed to be the third dimension) and subtract it off |
---|
46 | * the original. |
---|
47 | * \param numSpec Number of spatial pixels in original cube. |
---|
48 | * \param specLength Size of spectral dimension of original cube. |
---|
49 | * \param originalCube The cube that is to have its baseline removed. |
---|
50 | * \param baselineValues The cube of baseline values -- the same |
---|
51 | * size as the original. |
---|
52 | * \param par The Param set: information on BLANK values and on how |
---|
53 | * the subtraction is done. |
---|
54 | */ |
---|
55 | |
---|
56 | float *spec = new float[specLength]; |
---|
57 | float *thisBaseline = new float[specLength]; |
---|
58 | int minscale = par.getMinScale(); |
---|
59 | par.setMinScale(par.filter().getNumScales(specLength)); |
---|
60 | float atrouscut = par.getAtrousCut(); |
---|
61 | par.setAtrousCut(1); |
---|
62 | bool flagVerb = par.isVerbose(); |
---|
63 | par.setVerbosity(false); |
---|
64 | |
---|
65 | ProgressBar bar; |
---|
66 | if(flagVerb) bar.init(numSpec); |
---|
67 | for(int pix=0; pix<numSpec; pix++){ // for each spatial pixel... |
---|
68 | |
---|
69 | if(flagVerb) bar.update(pix+1); |
---|
70 | |
---|
71 | for(int z=0; z<specLength; z++) |
---|
72 | spec[z] = originalCube[z*numSpec + pix]; |
---|
73 | |
---|
74 | atrous1DReconstruct(specLength,spec,thisBaseline,par); |
---|
75 | |
---|
76 | for(int z=0; z<specLength; z++) { |
---|
77 | baselineValues[z*numSpec+pix] = thisBaseline[z]; |
---|
78 | if(!par.isBlank(originalCube[z*numSpec+pix])) |
---|
79 | originalCube[z*numSpec+pix] = originalCube[z*numSpec+pix] - |
---|
80 | baselineValues[z*numSpec+pix]; |
---|
81 | } |
---|
82 | |
---|
83 | } |
---|
84 | if(flagVerb) bar.rewind(); |
---|
85 | |
---|
86 | par.setMinScale(minscale); |
---|
87 | par.setAtrousCut(atrouscut); |
---|
88 | par.setVerbosity(flagVerb); |
---|
89 | |
---|
90 | delete [] spec; |
---|
91 | delete [] thisBaseline; |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | void getBaseline(long size, float *input, float *baseline, Param &par) |
---|
96 | { |
---|
97 | /** |
---|
98 | * Uses the a trous reconstruction, keeping only the highest two scales, |
---|
99 | * to reconstruct the baseline. |
---|
100 | * To avoid contamination by very strong signals, the input spectrum is |
---|
101 | * trimmed at 5*MADFM above the median before reconstruction. |
---|
102 | * This reduces the strong dips created by the presence of very strong |
---|
103 | * signals. |
---|
104 | * \param size Length of the spectrum. |
---|
105 | * \param input The input array : this is not affected. |
---|
106 | * \param baseline The returned baseline array. This needs to be allocated |
---|
107 | * before the function is called. |
---|
108 | * \param par The Param set, needed for the atrous reconstruction. |
---|
109 | */ |
---|
110 | |
---|
111 | int minscale = par.getMinScale(); |
---|
112 | par.setMinScale(par.filter().getNumScales(size)); |
---|
113 | float atrouscut = par.getAtrousCut(); |
---|
114 | par.setAtrousCut(1); |
---|
115 | bool flagVerb = par.isVerbose(); |
---|
116 | par.setVerbosity(false); |
---|
117 | |
---|
118 | float *spec = new float[size]; |
---|
119 | float med,sig; |
---|
120 | findMedianStats(input,size,med,sig); |
---|
121 | float threshold = 5. * sig; |
---|
122 | for(int i=0;i<size;i++) { |
---|
123 | if(fabs(input[i]-med)>threshold){ |
---|
124 | if(input[i]>med) spec[i] = med + threshold; |
---|
125 | else spec[i] = med - threshold; |
---|
126 | } |
---|
127 | else spec[i] = input[i]; |
---|
128 | } |
---|
129 | |
---|
130 | // atrous1DReconstruct(size, input, baseline, par); |
---|
131 | atrous1DReconstruct(size, spec, baseline, par); |
---|
132 | |
---|
133 | par.setMinScale(minscale); |
---|
134 | par.setAtrousCut(atrouscut); |
---|
135 | par.setVerbosity(flagVerb); |
---|
136 | |
---|
137 | delete [] spec; |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | void getBaseline(long size, float *input, float *baseline) |
---|
143 | { |
---|
144 | /** |
---|
145 | * This version is designed for programs not using Param classes -- it |
---|
146 | * keeps that side of things hidden from the user. |
---|
147 | * Uses the a trous reconstruction, keeping only the highest two scales, |
---|
148 | * to reconstruct the baseline. |
---|
149 | * To avoid contamination by very strong signals, the input spectrum is |
---|
150 | * trimmed at 8*MADFM above the median before reconstruction. This |
---|
151 | * reduces the strong dips created by the presence of very strong |
---|
152 | * signals. |
---|
153 | * \param size Length of the spectrum. |
---|
154 | * \param input The input array : this is not affected. |
---|
155 | * \param baseline The returned baseline array. This needs to be allocated |
---|
156 | * before the function is called. |
---|
157 | */ |
---|
158 | |
---|
159 | Param par; |
---|
160 | par.setMinScale(par.filter().getNumScales(size)); |
---|
161 | par.setAtrousCut(1); |
---|
162 | par.setVerbosity(false); |
---|
163 | |
---|
164 | float *spec = new float[size]; |
---|
165 | float med,sig; |
---|
166 | findMedianStats(input,size,med,sig); |
---|
167 | float threshold = 8. * sig; |
---|
168 | for(int i=0;i<size;i++) { |
---|
169 | if(fabs(input[i]-med)>threshold){ |
---|
170 | if(input[i]>med) spec[i] = med + threshold; |
---|
171 | else spec[i] = med - threshold; |
---|
172 | } |
---|
173 | else spec[i] = input[i]; |
---|
174 | } |
---|
175 | |
---|
176 | atrous1DReconstruct(size, spec, baseline, par); |
---|
177 | |
---|
178 | delete [] spec; |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | } |
---|