[3] | 1 | #include <iostream> |
---|
| 2 | #include <iomanip> |
---|
[103] | 3 | #include <math.h> |
---|
[175] | 4 | #include <duchamp.hh> |
---|
| 5 | #include <param.hh> |
---|
[3] | 6 | #include <ATrous/atrous.hh> |
---|
[74] | 7 | #include <Utils/utils.hh> |
---|
[3] | 8 | |
---|
[86] | 9 | void baselineSubtract(long numSpec, long specLength, float *originalCube, |
---|
| 10 | float *baselineValues, Param &par) |
---|
[3] | 11 | { |
---|
| 12 | /** |
---|
[86] | 13 | * baselineSubtract(long numSpec, long specLength, float *originalCube, |
---|
| 14 | * float *baselineValues, Param &par) |
---|
[3] | 15 | * |
---|
[86] | 16 | * A routine to find the baseline of spectra in a cube (spectral direction |
---|
| 17 | * assumed to be the third dimension) and subtract it off the original. |
---|
| 18 | * The original cube has numSpec spatial pixels, each containing a spectrum |
---|
| 19 | * of length specLength. |
---|
[3] | 20 | * The original cube is read in, and returned with the baseline removed. |
---|
| 21 | * This baseline is stored in the array baselineValues. |
---|
| 22 | * The Param variable par is needed to test for blank pixels -- these are kept as blank. |
---|
| 23 | */ |
---|
| 24 | extern Filter reconFilter; |
---|
| 25 | float *spec = new float[specLength]; |
---|
| 26 | float *thisBaseline = new float[specLength]; |
---|
| 27 | int minscale = par.getMinScale(); |
---|
| 28 | par.setMinScale(reconFilter.getNumScales(specLength)); |
---|
| 29 | float atrouscut = par.getAtrousCut(); |
---|
| 30 | par.setAtrousCut(1); |
---|
| 31 | bool flagVerb = par.isVerbose(); |
---|
| 32 | par.setVerbosity(false); |
---|
| 33 | |
---|
[187] | 34 | ProgressBar bar; |
---|
| 35 | if(flagVerb) bar.init(numSpec); |
---|
[3] | 36 | for(int pix=0; pix<numSpec; pix++){ // for each spatial pixel... |
---|
| 37 | |
---|
[187] | 38 | if(flagVerb) bar.update(pix+1); |
---|
[3] | 39 | |
---|
| 40 | for(int z=0; z<specLength; z++) |
---|
| 41 | spec[z] = originalCube[z*numSpec + pix]; |
---|
| 42 | |
---|
| 43 | atrous1DReconstruct(specLength,spec,thisBaseline,par); |
---|
| 44 | |
---|
| 45 | for(int z=0; z<specLength; z++) { |
---|
| 46 | baselineValues[z*numSpec+pix] = thisBaseline[z]; |
---|
| 47 | if(!par.isBlank(originalCube[z*numSpec+pix])) |
---|
[175] | 48 | originalCube[z*numSpec+pix] = originalCube[z*numSpec+pix] - |
---|
| 49 | baselineValues[z*numSpec+pix]; |
---|
[3] | 50 | } |
---|
| 51 | |
---|
| 52 | } |
---|
[187] | 53 | if(flagVerb) bar.rewind(); |
---|
[3] | 54 | |
---|
| 55 | par.setMinScale(minscale); |
---|
| 56 | par.setAtrousCut(atrouscut); |
---|
| 57 | par.setVerbosity(flagVerb); |
---|
| 58 | |
---|
| 59 | delete [] spec; |
---|
| 60 | delete [] thisBaseline; |
---|
| 61 | |
---|
| 62 | } |
---|
| 63 | |
---|
[43] | 64 | void getBaseline(long size, float *input, float *baseline, Param &par) |
---|
| 65 | { |
---|
| 66 | /** |
---|
| 67 | * getBaseline(long size, float *input, float *baseline, Param &par) |
---|
| 68 | * A function to find the baseline of an input (1-D) spectrum. |
---|
[175] | 69 | * Uses the a trous reconstruction, keeping only the highest two scales, |
---|
| 70 | * to reconstruct the baseline. |
---|
| 71 | * To avoid contamination by very strong signals, the input spectrum is |
---|
| 72 | * trimmed at 8*MADFM above the median before reconstruction. |
---|
| 73 | * This reduces the strong dips created by the presence of very strong |
---|
| 74 | * signals. |
---|
[43] | 75 | * The baseline array is returned -- no change is made to the input array. |
---|
| 76 | */ |
---|
[3] | 77 | |
---|
[43] | 78 | extern Filter reconFilter; |
---|
| 79 | int minscale = par.getMinScale(); |
---|
| 80 | par.setMinScale(reconFilter.getNumScales(size)); |
---|
| 81 | float atrouscut = par.getAtrousCut(); |
---|
| 82 | par.setAtrousCut(1); |
---|
| 83 | bool flagVerb = par.isVerbose(); |
---|
| 84 | par.setVerbosity(false); |
---|
| 85 | |
---|
| 86 | float *spec = new float[size]; |
---|
| 87 | float med,sig; |
---|
| 88 | findMedianStats(input,size,med,sig); |
---|
| 89 | float threshold = 8. * sig; |
---|
| 90 | for(int i=0;i<size;i++) { |
---|
[125] | 91 | if(fabs(input[i]-med)>threshold){ |
---|
[43] | 92 | if(input[i]>med) spec[i] = med + threshold; |
---|
| 93 | else spec[i] = med - threshold; |
---|
| 94 | } |
---|
| 95 | else spec[i] = input[i]; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | // atrous1DReconstruct(size, input, baseline, par); |
---|
| 99 | atrous1DReconstruct(size, spec, baseline, par); |
---|
| 100 | |
---|
| 101 | par.setMinScale(minscale); |
---|
| 102 | par.setAtrousCut(atrouscut); |
---|
| 103 | par.setVerbosity(flagVerb); |
---|
| 104 | |
---|
| 105 | delete [] spec; |
---|
| 106 | |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
[74] | 110 | void getBaseline(long size, float *input, float *baseline) |
---|
[3] | 111 | { |
---|
| 112 | /** |
---|
[74] | 113 | * getBaseline(long size, float *input, float *baseline) |
---|
| 114 | * A function to find the baseline of an input (1-D) spectrum. |
---|
[175] | 115 | * This version is designed for programs not using Param classes -- it |
---|
| 116 | * keeps that side of things hidden from the user. |
---|
| 117 | * Uses the a trous reconstruction, keeping only the highest two scales, |
---|
| 118 | * to reconstruct the baseline. |
---|
| 119 | * To avoid contamination by very strong signals, the input spectrum is |
---|
| 120 | * trimmed at 8*MADFM above the median before reconstruction. This |
---|
| 121 | * reduces the strong dips created by the presence of very strong |
---|
| 122 | * signals. |
---|
[74] | 123 | * The baseline array is returned -- no change is made to the input array. |
---|
[3] | 124 | */ |
---|
| 125 | |
---|
[74] | 126 | extern Filter reconFilter; |
---|
| 127 | Param par; |
---|
| 128 | par.setMinScale(reconFilter.getNumScales(size)); |
---|
| 129 | par.setAtrousCut(1); |
---|
| 130 | par.setVerbosity(false); |
---|
[43] | 131 | |
---|
[74] | 132 | float *spec = new float[size]; |
---|
| 133 | float med,sig; |
---|
| 134 | findMedianStats(input,size,med,sig); |
---|
| 135 | float threshold = 8. * sig; |
---|
| 136 | for(int i=0;i<size;i++) { |
---|
[125] | 137 | if(fabs(input[i]-med)>threshold){ |
---|
[74] | 138 | if(input[i]>med) spec[i] = med + threshold; |
---|
| 139 | else spec[i] = med - threshold; |
---|
[43] | 140 | } |
---|
[74] | 141 | else spec[i] = input[i]; |
---|
| 142 | } |
---|
[43] | 143 | |
---|
[74] | 144 | atrous1DReconstruct(size, spec, baseline, par); |
---|
[43] | 145 | |
---|
[74] | 146 | delete [] spec; |
---|
[43] | 147 | |
---|
[3] | 148 | } |
---|
| 149 | |
---|