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