source: trunk/src/ATrous/baselineSubtract.cc @ 208

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

Large suite of edits, mostly due to testing with valgrind, which clear up bad memory allocations and so on.
Improved the printing of progress bars in some routines by introducing a new ProgressBar? class, with associated functions to do the printing (now much cleaner).

File size: 4.6 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <math.h>
4#include <duchamp.hh>
5#include <param.hh>
6#include <ATrous/atrous.hh>
7#include <Utils/utils.hh>
8
9void baselineSubtract(long numSpec, long specLength, float *originalCube,
10                      float *baselineValues, Param &par)
11{
12  /**
13   *  baselineSubtract(long numSpec, long specLength, float *originalCube,
14   *                   float *baselineValues, Param &par)
15   *   
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.
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
34  ProgressBar bar;
35  if(flagVerb) bar.init(numSpec);
36  for(int pix=0; pix<numSpec; pix++){ // for each spatial pixel...
37
38    if(flagVerb) bar.update(pix+1);
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]))
48        originalCube[z*numSpec+pix] = originalCube[z*numSpec+pix] -
49          baselineValues[z*numSpec+pix];
50    }
51
52  }   
53  if(flagVerb) bar.rewind();
54
55  par.setMinScale(minscale);
56  par.setAtrousCut(atrouscut);
57  par.setVerbosity(flagVerb);
58 
59  delete [] spec;
60  delete [] thisBaseline;
61   
62}
63
64void 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.
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.
75   *    The baseline array is returned -- no change is made to the input array.
76   */
77
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++) {
91    if(fabs(input[i]-med)>threshold){
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
110void getBaseline(long size, float *input, float *baseline)
111{
112  /**
113   *  getBaseline(long size, float *input, float *baseline)
114   *    A function to find the baseline of an input (1-D) spectrum.
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.
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.