source: trunk/src/Utils/medianBaseline.cc @ 1259

Last change on this file since 1259 was 1259, checked in by MatthewWhiting, 11 years ago

Ticket #107 - Implementing the median baseline algorithm, with a few tweaks to make it work. The interface is now similar to the atrous baseline algorithm (which has been renamed).

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
48void findMedianBaseline(int dim, float *inputArray, unsigned int boxWidth, float *baselineValues)
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
55    int halfWidth = boxWidth/2;
56
57    if(halfWidth > dim) {
58        std::cerr << "medianBaseline: Box half-width bigger than array dimension. Returning output array unchanged.\n";
59    }
60
61    std::map<float,int> pixMap;
62 
63//  float *baselineValues = 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    }
70    baselineValues[curpos] = medVal(pixMap);
71    // for(std::map<float,int>::iterator it=pixMap.begin();it!=pixMap.end();it++) std::cerr << it->first <<" -> " << it->second <<"\n";
72    // std::cerr << baselineValues[curpos] <<"\n";
73
74    // Do it for the rest of the points
75    curpos++;
76    for(;curpos<dim;curpos++){
77   
78        int newpix = curpos+halfWidth;
79        if(newpix<dim) pixMap.insert(std::pair<float,int>(inputArray[newpix],newpix));
80
81        if(pixMap.size()>boxWidth){
82            if(curpos>halfWidth) pixMap.erase(inputArray[curpos-halfWidth]);
83        }
84
85        baselineValues[curpos] = medVal(pixMap);
86
87    }
88
89}
Note: See TracBrowser for help on using the repository browser.