source: tags/release-1.0.2/src/ATrous/baselineSubtract.cc @ 167

Last change on this file since 167 was 126, checked in by Matthew Whiting, 18 years ago

Changed all instances of fabsf to fabs so that compilation on venice works.
(Mirror commit of rev.125)

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