-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmovext.hh
75 lines (64 loc) · 1.27 KB
/
movext.hh
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
/*
Moving Extrema
Copyright 2020 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
#include "delay.hh"
#include "stack.hh"
namespace DSP {
template <typename TYPE, typename EQUAL, typename COMP, int NUM>
class MovExt
{
Delay<TYPE, NUM> window;
Stack<TYPE, NUM> dispenser, refill;
EQUAL equal;
COMP comp;
public:
MovExt(TYPE init) : window(init)
{
dispenser.push(init);
}
TYPE operator () (TYPE input)
{
if (window(input) == dispenser.top())
dispenser.pop();
while (!refill.empty() && comp(input, refill.top()))
refill.pop();
refill.push(input);
if (dispenser.empty()) {
while (!refill.empty()) {
dispenser.push(refill.top());
refill.pop();
}
return dispenser.top();
}
return comp(dispenser.top(), refill.first()) ? dispenser.top() : refill.first();
}
};
template <typename TYPE, int NUM>
class MovMin
{
MovExt<TYPE, std::equal_to<TYPE>, std::less<TYPE>, NUM> movmin;
public:
MovMin() : movmin(std::numeric_limits<TYPE>::max())
{
}
TYPE operator () (TYPE input)
{
return movmin(input);
}
};
template <typename TYPE, int NUM>
class MovMax
{
MovExt<TYPE, std::equal_to<TYPE>, std::greater<TYPE>, NUM> movmax;
public:
MovMax() : movmax(std::numeric_limits<TYPE>::min())
{
}
TYPE operator () (TYPE input)
{
return movmax(input);
}
};
}