-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algo.h
61 lines (52 loc) · 1.41 KB
/
Algo.h
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
#ifndef AUDIO_ALGO_H
#define AUDIO_ALGO_H
#include <cassert>
#include <cstdlib>
namespace audio {
template<typename Container>
Container smooth(
Container container,
size_t windowRadius)
{
const auto windowSize = windowRadius * 2 + 1;
Container smoothed = container;
if(windowSize < container.size()) {
struct Window
{
size_t front;
size_t mid;
size_t back;
size_t count;
float sum;
} window = {};
// preload
for(; window.back < windowRadius; ++window.back) {
window.sum += container[window.back];
++window.count;
}
// front half window
for(; window.back < windowSize; ++window.mid, ++window.back) {
window.sum += container[window.back];
++window.count;
smoothed[window.mid] = window.sum / window.count;
}
// whole window
for(; window.back < container.size(); ++window.front, ++window.mid, ++window.back) {
window.sum -= container[window.front];
window.sum += container[window.back];
smoothed[window.mid] = window.sum / window.count;
}
// back half window
for(; window.mid < container.size(); ++window.front, ++window.mid) {
window.sum -= container[window.front];
--window.count;
smoothed[window.mid] = window.sum / window.count;
}
// unload (omitted)
} else {
assert(false); // TODO
}
return smoothed;
}
} // namespace audio
#endif // AUDIO_ALGO_H