00001 //# MedianSlider.h: Optimized sliding-median computator 00002 //# Copyright (C) 2000,2001 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id$ 00027 00028 #ifndef SCIMATH_MEDIANSLIDER_H 00029 #define SCIMATH_MEDIANSLIDER_H 00030 00031 //#! Includes go here 00032 00033 #include <casacore/casa/aips.h> 00034 #include <casacore/casa/Arrays/Vector.h> 00035 00036 namespace casacore { //# NAMESPACE CASACORE - BEGIN 00037 00038 //# Forward Declarations 00039 00040 // <summary> 00041 // Class to compute sliding median 00042 // </summary> 00043 00044 // <use visibility=export> 00045 00046 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos=""> 00047 // </reviewed> 00048 00049 // <synopsis> 00050 // MedianSlider is a class for efficient computing of sliding medians. 00051 // </synopsis> 00052 // 00053 // <example> 00054 // </example> 00055 // 00056 // <motivation> 00057 // Flagging Agents make extended use of sliding medians. 00058 // </motivation> 00059 // 00060 // <todo asof="yyyy/mm/dd"> 00061 // <li> think about a 2D sliding median 00062 // </todo> 00063 00064 class MedianSlider 00065 { 00066 public: 00067 00068 MedianSlider (); 00069 MedianSlider ( int halfwin ); 00070 MedianSlider ( const MedianSlider &other ); 00071 ~MedianSlider (); 00072 MedianSlider & operator = ( const MedianSlider &other ); 00073 00074 void cleanup (); 00075 00076 // Adds a datum to the slider. Once the window is full, newer values will 00077 // push out older values. Returns the new median value. 00078 // If flag is set to true, adds a "flagged" datum, one which takes 00079 // up space in the window but is skipped during median computations. 00080 Float add ( Float d,Bool flag=False ); 00081 // Adds a flagged datum 00082 Float add () { return add(0,True); } 00083 // Adds N flagged datums 00084 Float next ( uInt n=1 ); 00085 // Adds several datums at once (with corresponding flags) 00086 Float add ( const Vector<Float> &d,const Vector<Bool> &flag ); 00087 // Adds several non-flagged datums at once 00088 Float add ( const Vector<Float> &d ); 00089 00090 // Returns the number of values currently in the window. This is less 00091 // than the window width initially. 00092 // Int size (); 00093 00094 // Returns the number of non-flagged values in window 00095 Int nval (); 00096 00097 // Returns the current median value 00098 Float median (); 00099 00100 // Returns a previous value (from n steps ago) from the sliding window 00101 Float prevVal ( uInt n,Bool &flag ); 00102 00103 // Returns value from midpoint (center) of window, possibly with flag 00104 Float midpoint ( Bool &flag ); 00105 Float midpoint () 00106 { Bool dum; return midpoint(dum); } 00107 00108 // Returns the difference between the current median and the value 00109 // at window center. Optionally, also returns flag of median center 00110 Float diff ( Bool &flag ) { return midpoint(flag) - median(); } 00111 Float diff () 00112 { Bool dum; return diff(dum); } 00113 00114 // returns total memory usage (in bytes) for a given halfwin size 00115 static size_t objsize ( int halfwin ) 00116 { return sizeof(MedianSlider)+(sizeof(Float)+sizeof(uInt)+sizeof(Bool))*(halfwin*2+1); } 00117 00118 // For testing purposes only: verifies current value of median. 00119 // Throws an exception if it fails. 00120 Bool assure (); 00121 00122 private: 00123 00124 uInt halfwin,fullwin; 00125 Float *buf; 00126 uInt *index; 00127 Bool *valid; 00128 uInt ibuf,nind; 00129 00130 }; 00131 00132 00133 inline Int MedianSlider::nval () 00134 { 00135 return nind; 00136 } 00137 00138 inline Float MedianSlider::median () 00139 { 00140 if( !nind ) 00141 return 0; 00142 return nind%2 ? buf[ index[nind/2] ] 00143 : ( buf[ index[nind/2-1] ] + buf[ index[nind/2] ] )/2; 00144 // return nind%2 ? buf[ index[nind/2] ] 00145 // : buf[ index[nind/2-1] ]; 00146 } 00147 00148 inline Float MedianSlider::midpoint ( Bool &flag ) 00149 { 00150 return prevVal(halfwin+1,flag); 00151 } 00152 00153 00154 00155 } //# NAMESPACE CASACORE - END 00156 00157 #endif