casacore
Loading...
Searching...
No Matches
MedianSlider.h
Go to the documentation of this file.
1// # MedianSlider.h: Optimized sliding-median computator
2// # Copyright (C) 2000,2001
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_MEDIANSLIDER_H
27#define SCIMATH_MEDIANSLIDER_H
28
29// #! Includes go here
30
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Arrays/Vector.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward Declarations
37
38// <summary>
39// Class to compute sliding median
40// </summary>
41
42// <use visibility=export>
43
44// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
45// </reviewed>
46
47// <synopsis>
48// MedianSlider is a class for efficient computing of sliding medians.
49// </synopsis>
50//
51// <example>
52// </example>
53//
54// <motivation>
55// Flagging Agents make extended use of sliding medians.
56// </motivation>
57//
58// <todo asof="yyyy/mm/dd">
59// <li> think about a 2D sliding median
60// </todo>
61
63 public:
69
70 void cleanup();
71
72 // Adds a datum to the slider. Once the window is full, newer values will
73 // push out older values. Returns the new median value.
74 // If flag is set to true, adds a "flagged" datum, one which takes
75 // up space in the window but is skipped during median computations.
76 Float add(Float d, Bool flag = False);
77 // Adds a flagged datum
78 Float add() { return add(0, True); }
79 // Adds N flagged datums
80 Float next(uInt n = 1);
81 // Adds several datums at once (with corresponding flags)
82 Float add(const Vector<Float> &d, const Vector<Bool> &flag);
83 // Adds several non-flagged datums at once
85
86 // Returns the number of values currently in the window. This is less
87 // than the window width initially.
88 // Int size ();
89
90 // Returns the number of non-flagged values in window
91 Int nval();
92
93 // Returns the current median value
94 Float median();
95
96 // Returns a previous value (from n steps ago) from the sliding window
98
99 // Returns value from midpoint (center) of window, possibly with flag
100 Float midpoint(Bool &flag);
102 Bool dum;
103 return midpoint(dum);
104 }
105
106 // Returns the difference between the current median and the value
107 // at window center. Optionally, also returns flag of median center
108 Float diff(Bool &flag) { return midpoint(flag) - median(); }
110 Bool dum;
111 return diff(dum);
112 }
113
114 // returns total memory usage (in bytes) for a given halfwin size
115 static size_t objsize(int halfwin) {
116 return sizeof(MedianSlider) + (sizeof(Float) + sizeof(uInt) + sizeof(Bool)) * (halfwin * 2 + 1);
117 }
118
119 // For testing purposes only: verifies current value of median.
120 // Throws an exception if it fails.
122
123 private:
129};
130
131inline Int MedianSlider::nval() { return nind; }
132
134 if (!nind) return 0;
135 return nind % 2 ? buf[index[nind / 2]] : (buf[index[nind / 2 - 1]] + buf[index[nind / 2]]) / 2;
136 // return nind%2 ? buf[ index[nind/2] ]
137 // : buf[ index[nind/2-1] ];
138}
139
140inline Float MedianSlider::midpoint(Bool &flag) { return prevVal(halfwin + 1, flag); }
141
142} // namespace casacore
143
144#endif
Float add()
Adds a flagged datum.
Float next(uInt n=1)
Adds N flagged datums.
Float add(Float d, Bool flag=False)
Adds a datum to the slider.
Float median()
Returns the current median value.
MedianSlider(const MedianSlider &other)
Bool assure()
For testing purposes only: verifies current value of median.
Float add(const Vector< Float > &d, const Vector< Bool > &flag)
Adds several datums at once (with corresponding flags).
Int nval()
Returns the number of values currently in the window.
Float prevVal(uInt n, Bool &flag)
Returns a previous value (from n steps ago) from the sliding window.
Float diff(Bool &flag)
Returns the difference between the current median and the value at window center.
static size_t objsize(int halfwin)
returns total memory usage (in bytes) for a given halfwin size
Float add(const Vector< Float > &d)
Adds several non-flagged datums at once.
MedianSlider(int halfwin)
MedianSlider & operator=(const MedianSlider &other)
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41