casacore
scimath
Mathematics
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: aips2-request@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
//# $Id$
27
28
#ifndef SCIMATH_MEDIANSLIDER_H
29
#define SCIMATH_MEDIANSLIDER_H
30
31
//#! Includes go here
32
33
#include <casacore/casa/aips.h>
34
#include <casacore/casa/Arrays/Vector.h>
35
36
namespace
casacore
{
//# NAMESPACE CASACORE - BEGIN
37
38
//# Forward Declarations
39
40
// <summary>
41
// Class to compute sliding median
42
// </summary>
43
44
// <use visibility=export>
45
46
// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
47
// </reviewed>
48
49
// <synopsis>
50
// MedianSlider is a class for efficient computing of sliding medians.
51
// </synopsis>
52
//
53
// <example>
54
// </example>
55
//
56
// <motivation>
57
// Flagging Agents make extended use of sliding medians.
58
// </motivation>
59
//
60
// <todo asof="yyyy/mm/dd">
61
// <li> think about a 2D sliding median
62
// </todo>
63
64
class
MedianSlider
65
{
66
public
:
67
68
MedianSlider
();
69
MedianSlider
(
int
halfwin
);
70
MedianSlider
(
const
MedianSlider
&other );
71
~MedianSlider
();
72
MedianSlider
&
operator =
(
const
MedianSlider
&other );
73
74
void
cleanup
();
75
76
// Adds a datum to the slider. Once the window is full, newer values will
77
// push out older values. Returns the new median value.
78
// If flag is set to true, adds a "flagged" datum, one which takes
79
// up space in the window but is skipped during median computations.
80
Float
add
(
Float
d,
Bool
flag=
False
);
81
// Adds a flagged datum
82
Float
add
() {
return
add
(0,
True
); }
83
// Adds N flagged datums
84
Float
next
(
uInt
n=1 );
85
// Adds several datums at once (with corresponding flags)
86
Float
add
(
const
Vector<Float>
&d,
const
Vector<Bool>
&flag );
87
// Adds several non-flagged datums at once
88
Float
add
(
const
Vector<Float>
&d );
89
90
// Returns the number of values currently in the window. This is less
91
// than the window width initially.
92
// Int size ();
93
94
// Returns the number of non-flagged values in window
95
Int
nval
();
96
97
// Returns the current median value
98
Float
median
();
99
100
// Returns a previous value (from n steps ago) from the sliding window
101
Float
prevVal
(
uInt
n,
Bool
&flag );
102
103
// Returns value from midpoint (center) of window, possibly with flag
104
Float
midpoint
(
Bool
&flag );
105
Float
midpoint
()
106
{
Bool
dum;
return
midpoint
(dum); }
107
108
// Returns the difference between the current median and the value
109
// at window center. Optionally, also returns flag of median center
110
Float
diff
(
Bool
&flag ) {
return
midpoint
(flag) -
median
(); }
111
Float
diff
()
112
{
Bool
dum;
return
diff
(dum); }
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
// For testing purposes only: verifies current value of median.
119
// Throws an exception if it fails.
120
Bool
assure
();
121
122
private
:
123
124
uInt
halfwin
,
fullwin
;
125
Float
*
buf
;
126
uInt
*
index
;
127
Bool
*
valid
;
128
uInt
ibuf
,
nind
;
129
130
};
131
132
133
inline
Int
MedianSlider::nval
()
134
{
135
return
nind
;
136
}
137
138
inline
Float
MedianSlider::median
()
139
{
140
if
( !
nind
)
141
return
0;
142
return
nind
%2 ?
buf
[
index
[
nind
/2] ]
143
: (
buf
[
index
[
nind
/2-1] ] +
buf
[
index
[
nind
/2] ] )/2;
144
// return nind%2 ? buf[ index[nind/2] ]
145
// : buf[ index[nind/2-1] ];
146
}
147
148
inline
Float
MedianSlider::midpoint
(
Bool
&flag )
149
{
150
return
prevVal
(
halfwin
+1,flag);
151
}
152
153
154
155
}
//# NAMESPACE CASACORE - END
156
157
#endif
casacore::MedianSlider
Definition:
MedianSlider.h:65
casacore::MedianSlider::add
Float add()
Adds a flagged datum.
Definition:
MedianSlider.h:82
casacore::MedianSlider::nind
uInt nind
Definition:
MedianSlider.h:128
casacore::MedianSlider::next
Float next(uInt n=1)
Adds N flagged datums.
casacore::MedianSlider::halfwin
uInt halfwin
Definition:
MedianSlider.h:124
casacore::MedianSlider::buf
Float * buf
Definition:
MedianSlider.h:125
casacore::MedianSlider::~MedianSlider
~MedianSlider()
casacore::MedianSlider::cleanup
void cleanup()
casacore::MedianSlider::add
Float add(Float d, Bool flag=False)
Adds a datum to the slider.
casacore::MedianSlider::median
Float median()
Returns the current median value
Definition:
MedianSlider.h:138
casacore::MedianSlider::MedianSlider
MedianSlider(const MedianSlider &other)
casacore::MedianSlider::assure
Bool assure()
For testing purposes only: verifies current value of median.
casacore::MedianSlider::add
Float add(const Vector< Float > &d, const Vector< Bool > &flag)
Adds several datums at once (with corresponding flags)
casacore::MedianSlider::fullwin
uInt fullwin
Definition:
MedianSlider.h:124
casacore::MedianSlider::nval
Int nval()
Returns the number of values currently in the window.
Definition:
MedianSlider.h:133
casacore::MedianSlider::prevVal
Float prevVal(uInt n, Bool &flag)
Returns a previous value (from n steps ago) from the sliding window.
casacore::MedianSlider::MedianSlider
MedianSlider()
casacore::MedianSlider::ibuf
uInt ibuf
Definition:
MedianSlider.h:128
casacore::MedianSlider::diff
Float diff(Bool &flag)
Returns the difference between the current median and the value at window center.
Definition:
MedianSlider.h:110
casacore::MedianSlider::objsize
static size_t objsize(int halfwin)
returns total memory usage (in bytes) for a given halfwin size
Definition:
MedianSlider.h:115
casacore::MedianSlider::add
Float add(const Vector< Float > &d)
Adds several non-flagged datums at once.
casacore::MedianSlider::index
uInt * index
Definition:
MedianSlider.h:126
casacore::MedianSlider::MedianSlider
MedianSlider(int halfwin)
casacore::MedianSlider::valid
Bool * valid
Definition:
MedianSlider.h:127
casacore::MedianSlider::diff
Float diff()
Definition:
MedianSlider.h:111
casacore::MedianSlider::operator=
MedianSlider & operator=(const MedianSlider &other)
casacore::MedianSlider::midpoint
Float midpoint()
Definition:
MedianSlider.h:105
casacore::Vector< Float >
casacore
this file contains all the compiler specific defines
Definition:
mainpage.dox:28
casacore::False
const Bool False
Definition:
aipstype.h:44
casacore::uInt
unsigned int uInt
Definition:
aipstype.h:51
casacore::Float
float Float
Definition:
aipstype.h:54
casacore::Int
int Int
Definition:
aipstype.h:50
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition:
aipstype.h:42
casacore::True
const Bool True
Definition:
aipstype.h:43
Generated by
1.9.4