source: tags/release-1.1/src/ATrous/baselineSubtract.cc @ 1391

Last change on this file since 1391 was 299, checked in by Matthew Whiting, 17 years ago

Adding distribution text at the start of each file...

File size: 6.0 KB
Line 
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 <param.hh>
32#include <ATrous/filter.hh>
33#include <ATrous/atrous.hh>
34#include <Utils/utils.hh>
35#include <Utils/feedback.hh>
36
37void baselineSubtract(long numSpec, long specLength, float *originalCube,
38                      float *baselineValues, Param &par)
39{
40  /**
41   *  A routine to find the baseline of each spectrum in a cube (the spectral
42   *    direction is assumed to be the third dimension) and subtract it off
43   *    the original.
44   * \param numSpec Number of spatial pixels in original cube.
45   * \param specLength Size of spectral dimension of original cube.
46   * \param originalCube The cube that is to have its baseline removed.
47   * \param baselineValues The cube of baseline values -- the same
48   *                       size as the original.
49   * \param par The Param set: information on BLANK values and on how
50   *            the subtraction is done.
51   */
52
53  float *spec     = new float[specLength];
54  float *thisBaseline = new float[specLength];
55  int minscale = par.getMinScale();
56  par.setMinScale(par.filter().getNumScales(specLength));
57  float atrouscut = par.getAtrousCut();
58  par.setAtrousCut(1);
59  bool flagVerb = par.isVerbose();
60  par.setVerbosity(false);
61
62  ProgressBar bar;
63  if(flagVerb) bar.init(numSpec);
64  for(int pix=0; pix<numSpec; pix++){ // for each spatial pixel...
65
66    if(flagVerb) bar.update(pix+1);
67
68    for(int z=0; z<specLength; z++)
69      spec[z] = originalCube[z*numSpec + pix];
70
71    atrous1DReconstruct(specLength,spec,thisBaseline,par);
72
73    for(int z=0; z<specLength; z++) {
74      baselineValues[z*numSpec+pix] = thisBaseline[z];
75      if(!par.isBlank(originalCube[z*numSpec+pix]))
76        originalCube[z*numSpec+pix] = originalCube[z*numSpec+pix] -
77          baselineValues[z*numSpec+pix];
78    }
79
80  }   
81  if(flagVerb) bar.rewind();
82
83  par.setMinScale(minscale);
84  par.setAtrousCut(atrouscut);
85  par.setVerbosity(flagVerb);
86 
87  delete [] spec;
88  delete [] thisBaseline;
89   
90}
91
92void getBaseline(long size, float *input, float *baseline, Param &par)
93{
94  /**
95   *   Uses the a trous reconstruction, keeping only the highest two scales,
96   *     to reconstruct the baseline.
97   *    To avoid contamination by very strong signals, the input spectrum is
98   *     trimmed at 5*MADFM above the median before reconstruction.
99   *     This reduces the strong dips created by the presence of very strong
100   *     signals.
101   *  \param size Length of the spectrum.
102   *  \param input The input array : this is not affected.
103   *  \param baseline The returned baseline array. This needs to be allocated
104   *   before the function is called.
105   *  \param par The Param set, needed for the atrous reconstruction.
106   */
107
108  int minscale = par.getMinScale();
109  par.setMinScale(par.filter().getNumScales(size));
110  float atrouscut = par.getAtrousCut();
111  par.setAtrousCut(1);
112  bool flagVerb = par.isVerbose();
113  par.setVerbosity(false);
114
115  float *spec = new float[size];
116  float med,sig;
117  findMedianStats(input,size,med,sig);
118  float threshold = 5. * sig;
119  for(int i=0;i<size;i++) {
120    if(fabs(input[i]-med)>threshold){
121      if(input[i]>med) spec[i] = med + threshold;
122      else spec[i] = med - threshold;
123    }
124    else spec[i] = input[i];
125  }
126
127  //    atrous1DReconstruct(size, input, baseline, par);
128  atrous1DReconstruct(size, spec, baseline, par);
129
130  par.setMinScale(minscale);
131  par.setAtrousCut(atrouscut);
132  par.setVerbosity(flagVerb);
133
134  delete [] spec;
135
136}
137
138
139void getBaseline(long size, float *input, float *baseline)
140{
141  /**
142   *  This version is designed for programs not using Param classes -- it
143   *   keeps that side of things hidden from the user.
144   *  Uses the a trous reconstruction, keeping only the highest two scales,
145   *   to reconstruct the baseline.
146   *  To avoid contamination by very strong signals, the input spectrum is
147   *   trimmed at 8*MADFM above the median before reconstruction. This
148   *   reduces the strong dips created by the presence of very strong
149   *   signals.
150   *  \param size Length of the spectrum.
151   *  \param input The input array : this is not affected.
152   *  \param baseline The returned baseline array. This needs to be allocated
153   *   before the function is called.
154   */
155
156  Param par;
157  par.setMinScale(par.filter().getNumScales(size));
158  par.setAtrousCut(1);
159  par.setVerbosity(false);
160
161  float *spec = new float[size];
162  float med,sig;
163  findMedianStats(input,size,med,sig);
164  float threshold = 8. * sig;
165  for(int i=0;i<size;i++) {
166    if(fabs(input[i]-med)>threshold){
167      if(input[i]>med) spec[i] = med + threshold;
168      else spec[i] = med - threshold;
169    }
170    else spec[i] = input[i];
171  }
172
173  atrous1DReconstruct(size, spec, baseline, par);
174
175  delete [] spec;
176
177}
178
Note: See TracBrowser for help on using the repository browser.