casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MedianSlider.h
Go to the documentation of this file.
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 
00027 #ifndef SCIMATH_MEDIANSLIDER_H
00028 #define SCIMATH_MEDIANSLIDER_H
00029 
00030 //#! Includes go here
00031 
00032 #include <casa/Arrays/Vector.h>
00033 
00034 namespace casa { //# NAMESPACE CASA - BEGIN
00035 
00036 //# Forward Declarations
00037 
00038 // <summary>
00039 // Class to compute sliding median
00040 // </summary>
00041 
00042 // <use visibility=export>
00043 
00044 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00045 // </reviewed>
00046 
00047 // <synopsis>
00048 // MedianSlider is a class for efficient computing of sliding medians.
00049 // </synopsis>
00050 //
00051 // <example>
00052 // </example>
00053 //
00054 // <motivation>
00055 // Flagging Agents make extended use of sliding medians.
00056 // </motivation>
00057 //
00058 // <todo asof="yyyy/mm/dd">
00059 //   <li> think about a 2D sliding median
00060 // </todo>
00061 
00062 class MedianSlider
00063 {
00064 public:
00065     
00066   MedianSlider  ();
00067   MedianSlider  ( int halfwin );
00068   MedianSlider  ( const MedianSlider &other );
00069   ~MedianSlider ();
00070   MedianSlider & operator = ( const MedianSlider &other );
00071   
00072   void cleanup ();
00073 
00074 // Adds a datum to the slider. Once the window is full, newer values will 
00075 // push out older values. Returns the new median value.
00076 // If flag is set to true, adds a "flagged" datum, one which takes
00077 // up space in the window but is skipped during median computations.
00078   Float add      ( Float d,Bool flag=False );
00079 // Adds a flagged datum
00080   Float add      ()                          { return add(0,True); }
00081 // Adds N flagged datums
00082   Float next     ( uInt n=1 );
00083 // Adds several datums at once (with corresponding flags)
00084   Float add      ( const Vector<Float> &d,const Vector<Bool> &flag );
00085 // Adds several non-flagged datums at once
00086   Float add      ( const Vector<Float> &d );
00087   
00088 // Returns the number of values currently in the window. This is less
00089 // than the window width initially.
00090 //  Int  size    ();    
00091 
00092 // Returns the number of non-flagged values in window
00093   Int  nval    ();
00094   
00095 // Returns the current median value  
00096   Float median ();
00097 
00098 // Returns a previous value (from n steps ago) from the sliding window
00099   Float prevVal  ( uInt n,Bool &flag );
00100 
00101 // Returns value from midpoint (center) of window, possibly with flag
00102   Float midpoint ( Bool &flag );
00103   Float midpoint ()               
00104           { Bool dum; return midpoint(dum); }
00105 
00106 // Returns the difference between the current median and the value
00107 // at window center. Optionally, also returns flag of median center
00108   Float diff ( Bool &flag )       { return midpoint(flag) - median(); }
00109   Float diff ()                   
00110            { Bool dum; return diff(dum); }
00111 
00112 // returns total memory usage (in bytes) for a given halfwin size 
00113   static size_t objsize ( int halfwin )
00114   { return sizeof(MedianSlider)+(sizeof(Float)+sizeof(uInt)+sizeof(Bool))*(halfwin*2+1); }
00115   
00116 // For testing purposes only: verifies current value of median.
00117 // Throws an exception if it fails.
00118   Bool  assure ();
00119 
00120 private:
00121 
00122   uInt   halfwin,fullwin;
00123   Float *buf;
00124   uInt  *index;
00125   Bool  *valid;
00126   uInt   ibuf,nind;
00127   
00128 };
00129 
00130 
00131 inline Int MedianSlider::nval ()
00132 {
00133   return nind;
00134 }
00135 
00136 inline Float MedianSlider::median () 
00137 {
00138   if( !nind )
00139     return 0;
00140   return nind%2 ? buf[ index[nind/2] ] 
00141       : ( buf[ index[nind/2-1] ] + buf[ index[nind/2] ] )/2;
00142 //  return nind%2 ? buf[ index[nind/2] ] 
00143 //      : buf[ index[nind/2-1] ];
00144 }
00145 
00146 inline Float MedianSlider::midpoint ( Bool &flag ) 
00147 {
00148   return prevVal(halfwin+1,flag);
00149 }
00150 
00151 
00152 
00153 } //# NAMESPACE CASA - END
00154 
00155 #endif