[299] | 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 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
| 29 | #include <iomanip> |
---|
[103] | 30 | #include <math.h> |
---|
[175] | 31 | #include <param.hh> |
---|
[232] | 32 | #include <ATrous/filter.hh> |
---|
[3] | 33 | #include <ATrous/atrous.hh> |
---|
[74] | 34 | #include <Utils/utils.hh> |
---|
[213] | 35 | #include <Utils/feedback.hh> |
---|
[3] | 36 | |
---|
[378] | 37 | namespace duchamp |
---|
[3] | 38 | { |
---|
[232] | 39 | |
---|
[378] | 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 | */ |
---|
[3] | 55 | |
---|
[378] | 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); |
---|
[3] | 64 | |
---|
[378] | 65 | ProgressBar bar; |
---|
| 66 | if(flagVerb) bar.init(numSpec); |
---|
| 67 | for(int pix=0; pix<numSpec; pix++){ // for each spatial pixel... |
---|
[3] | 68 | |
---|
[378] | 69 | if(flagVerb) bar.update(pix+1); |
---|
[3] | 70 | |
---|
[378] | 71 | for(int z=0; z<specLength; z++) |
---|
| 72 | spec[z] = originalCube[z*numSpec + pix]; |
---|
[3] | 73 | |
---|
[378] | 74 | atrous1DReconstruct(specLength,spec,thisBaseline,par); |
---|
[3] | 75 | |
---|
[378] | 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 | } |
---|
[3] | 82 | |
---|
[378] | 83 | } |
---|
| 84 | if(flagVerb) bar.rewind(); |
---|
| 85 | |
---|
| 86 | par.setMinScale(minscale); |
---|
| 87 | par.setAtrousCut(atrouscut); |
---|
| 88 | par.setVerbosity(flagVerb); |
---|
[3] | 89 | |
---|
[378] | 90 | delete [] spec; |
---|
| 91 | delete [] thisBaseline; |
---|
[3] | 92 | |
---|
[378] | 93 | } |
---|
[3] | 94 | |
---|
[378] | 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 | */ |
---|
[3] | 110 | |
---|
[378] | 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); |
---|
[43] | 117 | |
---|
[378] | 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]; |
---|
[43] | 128 | } |
---|
| 129 | |
---|
[378] | 130 | // atrous1DReconstruct(size, input, baseline, par); |
---|
| 131 | atrous1DReconstruct(size, spec, baseline, par); |
---|
[43] | 132 | |
---|
[378] | 133 | par.setMinScale(minscale); |
---|
| 134 | par.setAtrousCut(atrouscut); |
---|
| 135 | par.setVerbosity(flagVerb); |
---|
[43] | 136 | |
---|
[378] | 137 | delete [] spec; |
---|
[43] | 138 | |
---|
[378] | 139 | } |
---|
[43] | 140 | |
---|
| 141 | |
---|
[378] | 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 | */ |
---|
[3] | 158 | |
---|
[378] | 159 | Param par; |
---|
| 160 | par.setMinScale(par.filter().getNumScales(size)); |
---|
| 161 | par.setAtrousCut(1); |
---|
| 162 | par.setVerbosity(false); |
---|
[43] | 163 | |
---|
[378] | 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]; |
---|
[43] | 174 | } |
---|
| 175 | |
---|
[378] | 176 | atrous1DReconstruct(size, spec, baseline, par); |
---|
[43] | 177 | |
---|
[378] | 178 | delete [] spec; |
---|
[43] | 179 | |
---|
[378] | 180 | } |
---|
| 181 | |
---|
[3] | 182 | } |
---|