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