OpenWareLaboratory
SmoothValue.h
Go to the documentation of this file.
1 #ifndef __SmoothValue_h__
2 #define __SmoothValue_h__
3 
4 #include "SimpleValue.h"
5 
14 template<typename T, typename Value = SimpleValue<T>>
15 class SmoothValue : public Value {
16 public:
17  T lambda = Value::DEFAULT_LAMBDA;
19  SmoothValue(T value) : Value(value) {}
20  SmoothValue(T lambda, T init) : Value(init), lambda(lambda) {}
21  SmoothValue(const SmoothValue<T, Value>& other) : Value(other), lambda(other.lambda) {}
22  void update(T x){
23  Value::update(Value::get()*lambda + x*(1.0f - lambda));
24  }
26  update(x);
27  return *this;
28  }
30  update(Value::get() + other);
31  return *this;
32  }
34  update(Value::get() - other);
35  return *this;
36  }
38  update(Value::get() * other);
39  return *this;
40  }
42  update(Value::get() / other);
43  return *this;
44  }
45  operator T(){
46  return Value::get();
47  }
48  static T normal(float lambda, int blocksize);
49 };
50 
51 template class SmoothValue<int>;
52 template class SmoothValue<float>;
55 
60 template<typename T, typename Value = SimpleValue<T>>
61 class StiffValue : public Value {
62 public:
63  T delta = Value::DEFAULT_DELTA;
65  StiffValue(T value) : Value(value) {}
66  StiffValue(T delta, T init) : Value(init), delta(delta) {}
67  StiffValue(const StiffValue<T, Value>& other) : Value(other), delta(other.delta) {}
68  void update(T newValue){
69  if(abs(Value::get() - newValue) > delta)
70  Value::update(newValue);
71  }
72  StiffValue<T, Value>& operator=(const T& other){
73  update(other);
74  return *this;
75  }
76  StiffValue<T, Value>& operator+=(const T& other){
77  update(Value::get() + other);
78  return *this;
79  }
80  StiffValue<T, Value>& operator-=(const T& other){
81  update(Value::get() - other);
82  return *this;
83  }
84  StiffValue<T, Value>& operator*=(const T& other){
85  update(Value::get() * other);
86  return *this;
87  }
88  StiffValue<T, Value>& operator/=(const T& other){
89  update(Value::get() / other);
90  return *this;
91  }
92  operator T(){
93  return Value::get();
94  }
95  static T normal(float delta);
96 };
97 
98 template class StiffValue<int>;
99 template class StiffValue<float>;
104 
105 template<class T>
107 
112 template<typename T, typename Value = SimpleValue<T>>
113 class LinearValue : public Value {
114 protected:
115  T scale;
118 public:
119  LinearValue(T value = 0): Value(value), scale(1), adjust(0), offset(0) {}
121  Value(other), scale(other.scale), adjust(other.adjust), offset(other.offset) {}
122  LinearValue(T minimum, T maximum, T init): Value(init) {
123  setRange(minimum, maximum);
124  }
125  void setRange(T minimum, T maximum){
126  scale = maximum - minimum;
127  adjust = 0;
128  offset = minimum;
129  }
130  void setRange(T from_lo, T from_hi, T to_lo, T to_hi){
131  // return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
132  // * scale +
133  scale = (to_hi - to_lo) / (from_hi - from_lo);
134  offset = to_lo;
135  adjust = from_lo;
136  // todo: verify!
137  }
142  return (Value::get() - offset) / scale + adjust;
143  }
144  void update(T x){
145  Value::update((x - adjust) * scale + offset);
146  }
148  update(x);
149  return *this;
150  }
152  update(Value::get() + other);
153  return *this;
154  }
156  update(Value::get() - other);
157  return *this;
158  }
160  update(Value::get() * other);
161  return *this;
162  }
164  update(Value::get() / other);
165  return *this;
166  }
167  operator T(){
168  return Value::get();
169  }
170 };
171 
172 template class LinearValue<int>;
173 template class LinearValue<float>;
178 
186 template<typename T, typename Value = SimpleValue<T>>
187 class ExponentialValue : public Value {
188 protected:
189  T c = Value::DEFAULT_EXP_C;
190  T k = Value::DEFAULT_EXP_K;
191 public:
194  ExponentialValue(T value): Value(value) {}
195  ExponentialValue(const ExponentialValue<T>& other): Value(other), c(other.c), k(other.k) {}
196  ExponentialValue(T minimum, T maximum, T init): Value(init) {
197  setRange(minimum, maximum);
198  }
199  void setRange(T minimum, T maximum){
200  ASSERT(minimum > 0 && maximum > 0, "Exponential min/max must be greater than 0");
201  c = minimum;
202  k = logf(maximum/minimum);
203  }
208  return logf(Value::get()/c)/k;
209  }
210  void update(T x){
211  Value::update(c * expf(k * x));
212  }
213  operator T(){
214  return Value::get();
215  }
217  update(x);
218  return *this;
219  }
221  update(Value::get() + other);
222  return *this;
223  }
225  update(Value::get() - other);
226  return *this;
227  }
229  update(Value::get() * other);
230  return *this;
231  }
233  update(Value::get() / other);
234  return *this;
235  }
236 };
237 
238 template class ExponentialValue<int>;
239 template class ExponentialValue<float>;
244 
252 template<typename T, typename Value = SimpleValue<T>>
253 class LogarithmicValue : public Value {
254 protected:
255  T c = Value::DEFAULT_LOG_C;
256  T k = Value::DEFAULT_LOG_K;
257 public:
260  LogarithmicValue(T value): Value(value) {}
261  LogarithmicValue(const LogarithmicValue<T>& other): Value(other), c(other.c), k(other.k) {}
262  LogarithmicValue(T a, T b, T init): Value(init) {
263  setRange(a, b);
264  }
265  void setRange(T a, T b){
266  c = expf(a);
267  k = expf(b) - c;
268  }
269  void update(T x){
270  Value::update(logf(k * x + c));
271  }
273  return (expf(Value::get()) - c) / k;
274  }
275  /* value cast operator */
276  operator T(){
277  return Value::get();
278  }
279  /* assignment operator */
281  update(x);
282  return *this;
283  }
285  update(Value::get() + other);
286  return *this;
287  }
289  update(Value::get() - other);
290  return *this;
291  }
293  update(Value::get() * other);
294  return *this;
295  }
297  update(Value::get() / other);
298  return *this;
299  }
300 };
301 
302 template class LogarithmicValue<float>;
303 template class LogarithmicValue<int>;
306 
307 #endif /* __SmoothValue_h__ */
SmoothValue< float > SmoothFloat
Definition: SmoothValue.h:53
LinearValue< float > LinearFloat
Definition: SmoothValue.h:174
SmoothValue< float, LinearFloat > SmoothLinearFloat
Definition: SmoothValue.h:176
SmoothValue< int, StiffInt > SmoothStiffInt
Definition: SmoothValue.h:103
ExponentialValue< float > ExponentialFloat
Definition: SmoothValue.h:241
LogarithmicValue< float > LogarithmicFloat
Definition: SmoothValue.h:304
LinearValue< int > LinearInt
Definition: SmoothValue.h:175
SmoothValue< int, ExponentialInt > SmoothExponentialInt
Definition: SmoothValue.h:242
ExponentialValue< int > ExponentialInt
Definition: SmoothValue.h:240
SmoothValue< int > SmoothInt
Definition: SmoothValue.h:54
StiffValue< int > StiffInt
Definition: SmoothValue.h:101
LogarithmicValue< int > LogarithmicInt
Definition: SmoothValue.h:305
SmoothValue< float, StiffFloat > SmoothStiffFloat
Definition: SmoothValue.h:102
SmoothValue< float, ExponentialFloat > SmoothExponentialFloat
Definition: SmoothValue.h:243
SmoothValue< int, LinearInt > SmoothLinearInt
Definition: SmoothValue.h:177
StiffValue< float > StiffFloat
Definition: SmoothValue.h:100
#define abs(x)
Definition: basicmaths.h:44
Exponentially scaled value.
Definition: SmoothValue.h:187
ExponentialValue< T, Value > & operator-=(const T &other)
Definition: SmoothValue.h:224
void setRange(T minimum, T maximum)
Definition: SmoothValue.h:199
ExponentialValue< T, Value > & operator/=(const T &other)
Definition: SmoothValue.h:232
ExponentialValue(T value)
Definition: SmoothValue.h:194
T getControl()
Get the input value.
Definition: SmoothValue.h:207
ExponentialValue< T, Value > & operator+=(const T &other)
Definition: SmoothValue.h:220
ExponentialValue()
Default range scales [0, 1] to [0.5, 2].
Definition: SmoothValue.h:193
ExponentialValue< T, Value > & operator*=(const T &other)
Definition: SmoothValue.h:228
ExponentialValue(T minimum, T maximum, T init)
Definition: SmoothValue.h:196
void update(T x)
Definition: SmoothValue.h:210
ExponentialValue(const ExponentialValue< T > &other)
Definition: SmoothValue.h:195
ExponentialValue< T, Value > & operator=(const T &x)
Definition: SmoothValue.h:216
Linearly scaled value.
Definition: SmoothValue.h:113
T getControl()
Get the input value.
Definition: SmoothValue.h:141
LinearValue(T value=0)
Definition: SmoothValue.h:119
void setRange(T minimum, T maximum)
Definition: SmoothValue.h:125
LinearValue(T minimum, T maximum, T init)
Definition: SmoothValue.h:122
LinearValue< T, Value > & operator-=(const T &other)
Definition: SmoothValue.h:155
LinearValue< T, Value > & operator/=(const T &other)
Definition: SmoothValue.h:163
LinearValue< T, Value > & operator+=(const T &other)
Definition: SmoothValue.h:151
void setRange(T from_lo, T from_hi, T to_lo, T to_hi)
Definition: SmoothValue.h:130
LinearValue(const LinearValue< T, Value > &other)
Definition: SmoothValue.h:120
LinearValue< T, Value > & operator*=(const T &other)
Definition: SmoothValue.h:159
LinearValue< T, Value > & operator=(const T &x)
Definition: SmoothValue.h:147
void update(T x)
Definition: SmoothValue.h:144
Logarithmically scaled value.
Definition: SmoothValue.h:253
LogarithmicValue< T, Value > & operator*=(const T &other)
Definition: SmoothValue.h:292
LogarithmicValue< T, Value > & operator=(const T &x)
Definition: SmoothValue.h:280
LogarithmicValue< T, Value > & operator/=(const T &other)
Definition: SmoothValue.h:296
LogarithmicValue(T a, T b, T init)
Definition: SmoothValue.h:262
LogarithmicValue< T, Value > & operator+=(const T &other)
Definition: SmoothValue.h:284
LogarithmicValue(T value)
Definition: SmoothValue.h:260
LogarithmicValue< T, Value > & operator-=(const T &other)
Definition: SmoothValue.h:288
LogarithmicValue()
Default range scales [0, 1] to [0.5, 2].
Definition: SmoothValue.h:259
void setRange(T a, T b)
Definition: SmoothValue.h:265
LogarithmicValue(const LogarithmicValue< T > &other)
Definition: SmoothValue.h:261
void update(T x)
Definition: SmoothValue.h:269
Applies exponential smoothing to a scalar value.
Definition: SmoothValue.h:15
SmoothValue(T value)
Definition: SmoothValue.h:19
SmoothValue< T, Value > & operator/=(const T &other)
Definition: SmoothValue.h:41
SmoothValue< T, Value > & operator-=(const T &other)
Definition: SmoothValue.h:33
SmoothValue(const SmoothValue< T, Value > &other)
Definition: SmoothValue.h:21
static T normal(float lambda, int blocksize)
SmoothValue< T, Value > & operator=(const T &x)
Definition: SmoothValue.h:25
SmoothValue< T, Value > & operator+=(const T &other)
Definition: SmoothValue.h:29
void update(T x)
Definition: SmoothValue.h:22
SmoothValue< T, Value > & operator*=(const T &other)
Definition: SmoothValue.h:37
SmoothValue(T lambda, T init)
Definition: SmoothValue.h:20
Applies simple hysteresis to a scalar.
Definition: SmoothValue.h:61
StiffValue< T, Value > & operator*=(const T &other)
Definition: SmoothValue.h:84
StiffValue< T, Value > & operator+=(const T &other)
Definition: SmoothValue.h:76
StiffValue< T, Value > & operator/=(const T &other)
Definition: SmoothValue.h:88
StiffValue< T, Value > & operator-=(const T &other)
Definition: SmoothValue.h:80
StiffValue(const StiffValue< T, Value > &other)
Definition: SmoothValue.h:67
void update(T newValue)
Definition: SmoothValue.h:68
StiffValue< T, Value > & operator=(const T &other)
Definition: SmoothValue.h:72
StiffValue(T value)
Definition: SmoothValue.h:65
StiffValue(T delta, T init)
Definition: SmoothValue.h:66
static T normal(float delta)
#define ASSERT(cond, msg)
Definition: message.h:16