Loading...
Searching...
No Matches
ImpulseAvgOp.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25 */
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31#ifndef ImpulseAvgOp_H
32#define ImpulseAvgOp_H
33
34#include <drain/UniTuple.h>
35
36
37//#include "drain/image/SegmentProber.h"
38
39#include "ImpulseResponseOp.h"
40
41namespace drain
42{
43namespace image
44{
45
46template <class T>
47class Decay2 : public UniTuple<T,2> {
48
49public:
50
51 double & forward;
52 double & backward;
53
54 Decay2(double decay = 1.0) : forward(this->next()), backward(this->next()) {
55 this->fill(decay);
56 }
57
58 // Reference
59 template <size_t N>
60 Decay2(drain::UniTuple<T,N> & tuple, size_t i) : drain::UniTuple<T,2>(tuple, i), forward(this->next()), backward(this->next()){
61 };
62};
63
64template <class T>
65struct Decay4 : public drain::UniTuple<T,4> {
66
67 Decay2<T> horz;
68 Decay2<T> vert;
69
70 Decay4(T decay=0.5) : horz(this->tuple(), 0), vert(this->tuple(), 2) {
71 this->fill(decay);
72 }
73
74
75 Decay4(const Decay4 & r) : horz(this->tuple(), 0), vert(this->tuple(), 2){
76 this->assign(r);
77 };
78
79 Decay4 & operator=(const Decay4<T> & decay){
80 this->set(decay.tuple());
81 return *this;
82 }
83
84};
85
86
87// IMPLEMENTATIONS
88
89
90struct ImpulseAvgConf : public BeanLike {
91
92
93 inline
94 ImpulseAvgConf() : BeanLike(__FUNCTION__, "Infinite-impulse response type spreading"), decays(0.75){
95 // this->parameters.link("decayHorz", decayHorz = 0.9);
96 // this->parameters.link("decayVert", decayVert = 0.9);
97 this->parameters.link("decay", decays.tuple()); //.fillArray = true;
98 //this->parameters.link("decay", decay = 0.9);
99 // this->parameters.link("decayVert", decayVert = 0.9);
100 };
101
102 inline
103 ImpulseAvgConf(const ImpulseAvgConf & conf) :
104 BeanLike(__FUNCTION__, "Infinite-impulse response type spreading"), decays(0.75){
105 this->parameters.link("decay", decays.tuple()); //.fillArray = true;
106 // this->parameters.link("decayHorz", decayHorz = conf.decayHorz);
107 // this->parameters.link("decayVert", decayVert = conf.decayHorz);
108 };
109
110 Decay4<double> decays;
111 //double decay;
112 //std::vector<double> decays;
113 //double decayHorz;
114 //double decayVert;
115
116};
117
118
120
135class ImpulseAvg : public ImpulseBucket<ImpulseAvgConf> {
136
137public:
138
139
140 inline
141 ImpulseAvg(){
142
143 };
144
145 inline
146 ImpulseAvg(const ImpulseAvg & avg){
147 decays = avg.decays;
148 }
149
150 inline
151 ImpulseAvg(const ImpulseAvgConf & conf){
152 decays = conf.decays;
153 }
154
155 virtual
156 void init(const Channel & src, bool horizontal);
157
158 virtual
159 void reset();
160
162 virtual
163 void addLeft(int i, double value, double weight);
164
166 virtual
167 void addRight(int i, double value, double weight);
168
170 virtual
171 void addDown(int i, double value, double weight);
172
174 virtual
175 void addUp(int i, double value, double weight);
176
177
178 virtual
179 double get(int i);
180
181 virtual
182 double getWeight(int i);
183
184
185protected:
186
187private:
188
189 drain::ValueScaling scaling;
190
192 struct entry {
193
194 double x;
195 double weight;
196
197 inline void set(double value, double weight){
198 this->x = value;
199 this->weight = weight;
200 }
201
202 };
203
204 /*
205 * \param xNew - value to be added
206 * \param wNew - weight of xNew
207 */
208 inline
209 void mix(entry & prev, const entry & e, double decay){
210
211 double w1 = decay*e.weight;
212 double w2 = (1.0-decay);
213
214 if (decay < 1.0)
215 prev.x =(w1*e.x + w2*prev.x) / (w1 + w2);
216 else // decay==1.0
217 prev.x = e.x;
218
219 prev.weight = w1 + w2*prev.weight;
220
221 }
222
223 typedef std::pair<entry,entry> entryPair;
224 typedef std::vector<entryPair> container;
225
226 container data;
227
228 entry e;
229 entryPair latest; // utility
230
231
232};
233
234
235
236} // image::
237
238} // drain::
239
240
241#endif /* ImpulseResponse_H_ */
Something which has a name, a description and possibly some parameters of varying type.
Definition BeanLike.h:58
void fill(T i)
Set all the elements to i.
Definition TupleBase.h:315
Tuple of N elements of type T.
Definition UniTuple.h:65
Linear scaling and physical range for image intensities.
Definition ValueScaling.h:63
Image with static geometry.
Definition ImageChannel.h:58
Definition ImpulseAvgOp.h:47
Averaging operator. A simple example implementation of ImpulseBucket.
Definition ImpulseAvgOp.h:135
virtual void addUp(int i, double value, double weight)
Accumulate encoded value.
Definition ImpulseAvgOp.cpp:86
virtual void init(const Channel &src, bool horizontal)
Adapt to input geometry, type, and scaling.
Definition ImpulseAvgOp.cpp:41
virtual double getWeight(int i)
Return weight at position i.
Definition ImpulseAvgOp.cpp:93
virtual void addRight(int i, double value, double weight)
Accumulate encoded value.
Definition ImpulseAvgOp.cpp:74
virtual double get(int i)
Return natural (not encoded) value at position i.
Definition ImpulseAvgOp.cpp:98
virtual void addLeft(int i, double value, double weight)
Accumulate encoded value.
Definition ImpulseAvgOp.cpp:68
virtual void addDown(int i, double value, double weight)
Accumulate encoded value.
Definition ImpulseAvgOp.cpp:80
virtual void reset()
Clear statistics before traversing each row or column.
Definition ImpulseAvgOp.cpp:55
Definition ImpulseResponseOp.h:54
Definition DataSelector.cpp:1277
Definition ImpulseAvgOp.h:65
Definition ImpulseAvgOp.h:90