source: tags/release-1.2.2/src/Utils/medianBaseline.cc

Last change on this file was 628, checked in by MatthewWhiting, 15 years ago

Cleaning up a bit, and making use of size_t

File size: 3.0 KB
Line 
1// -----------------------------------------------------------------------
2// medianBaseline.cc: Obtaining spectral baselines using a median filter.
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 <map>
30
31float medVal(std::map<float,int> &themap)
32{
33  std::map<float,int>::iterator it;
34  it = themap.begin();
35  if(themap.size()%2==0) {
36    for(size_t i=0;i<themap.size()/2-1;i++) it++;
37    float v1 = it->first;
38    it++;
39    float v2 = it->first;
40    return (v1+v2)/2.;
41  }
42  else{
43    for(size_t i=0;i<themap.size()/2;i++) it++;
44    return it->first;
45  }
46}
47
48float *medianBaseline(float *inputArray, int dim, unsigned int boxWidth)
49{
50
51  if(boxWidth%2==0) std::cerr << "medianBaseline: boxWidth needs to be odd. Changing from " << boxWidth << " to " << boxWidth+1 <<"\n";
52  boxWidth++;
53
54  int halfWidth = boxWidth/2;
55
56  if(halfWidth > dim) {
57    std::cerr << "Box half-width bigger than array dimension. Returning input array unchanged.\n";
58    return inputArray;
59  }
60
61  std::map<float,int> pixMap;
62 
63  float *medianArray = new float[dim];
64
65  // Initialise the map for the first point in the array
66  int curpos = 0;
67  for(int i=0; i<=halfWidth; i++) {
68    pixMap.insert(std::pair<float,int>(inputArray[i],i));
69    //    std::cerr << "Inserting " << i << " " << inputArray[i] << "\n";
70  }
71  medianArray[curpos] = medVal(pixMap);
72
73  // Do it for the rest of the points
74  curpos++;
75  for(;curpos<dim;curpos++){
76   
77    int newpix = curpos+halfWidth;
78    if(newpix<dim) pixMap.insert(std::pair<float,int>(inputArray[newpix],newpix));
79    //    std::cerr << "Inserting " << newpix << " " << inputArray[newpix] << "\n";
80
81    int oldpix = curpos-halfWidth-1;
82    //    if(oldpix>=0) pixMap.erase(oldpix);
83    if(oldpix>=0) pixMap.erase(inputArray[oldpix]);
84
85    medianArray[curpos] = medVal(pixMap);
86
87  }
88
89  return medianArray;
90}
Note: See TracBrowser for help on using the repository browser.