/
Hysteresis.hpp
110 lines (80 loc) · 2.08 KB
/
Hysteresis.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef MOBILINKD__HYSTERESIS_H_
#define MOBILINKD__HYSTERESIS_H_
#include <cstddef>
namespace mobilinkd { namespace libafsk {
template <typename T>
struct BaseHysteresis {
typedef T float_type;
float_type min_;
float_type max_;
int low_;
int high_;
float_type last_;
BaseHysteresis(float_type minimum, float_type maximum, int low = 0, int high = 1)
: min_(minimum), max_(maximum), low_(low), high_(high), last_(0.0)
{}
int operator()(float_type value) {
if (value <= min_) {
last_ = low_;
} else if (value >= max_) {
last_ = high_;
}
return last_;
}
};
typedef BaseHysteresis<double> Hysteresis;
typedef BaseHysteresis<float> FastHysteresis;
template <typename T, size_t N>
struct BlockHysteresis {
typedef T* result_type;
float min_;
float max_;
T low_;
T high_;
T last_;
T buffer_[N];
BlockHysteresis(float minimum, float maximum, T low = 0, T high = 1)
: min_(minimum), max_(maximum), low_(low), high_(high), last_(low)
, buffer_()
{}
result_type operator()(float* value) {
for (size_t i = 0; i != N; ++i) {
if (value[i] <= min_) {
last_ = low_;
} else if (value[i] >= max_) {
last_ = high_;
}
buffer_[i] = last_;
}
return buffer_;
}
};
/*
template <bool, size_t N>
struct BlockHysteresis {
typedef uint8_t* result_type;
float min_;
float max_;
bool low_;
bool high_;
bool last_;
uint8_t buffer_[(N + 7) / 8];
BlockHysteresis(float minimum, float maximum, bool low = false, bool high = true)
: min_(minimum), max_(maximum), low_(low), high_(high), last_(low)
, buffer_()
{}
result_type operator()(float* value) {
for (size_t i = 0; i != N; ++i) {
if (value[i] <= min_) {
last_ = low_;
} else if (value[i] >= max_) {
last_ = high_;
}
buffer_[i >> 3] = (last_ << (i & 7));
}
return buffer_;
}
};
*/
}} // mobilinkd::libafsk
#endif // MOBILINKD__HYSTERESIS_H_