-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsma.hh
99 lines (89 loc) · 1.57 KB
/
sma.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Simple moving average
Copyright 2019 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
#include "kahan.hh"
#include "delay.hh"
#include "swa.hh"
namespace DSP {
template <typename TYPE, typename VALUE, int NUM>
class SMA1
{
TYPE hist_inp[NUM];
TYPE hist_avg;
int hist_pos;
public:
SMA1() : hist_avg(0), hist_pos(0)
{
for (int i = 0; i < NUM; ++i)
hist_inp[i] = 0;
}
VALUE abs_dev()
{
VALUE sum(abs(hist_inp[0] - hist_avg));
for (int i = 1; i < NUM; ++i)
sum += abs(hist_inp[i] - hist_avg);
return sum / VALUE(NUM);
}
TYPE operator () (TYPE input)
{
hist_inp[hist_pos] = input;
if (++hist_pos >= NUM)
hist_pos = 0;
TYPE hist_sum(hist_inp[0]);
for (int i = 1; i < NUM; ++i)
hist_sum += hist_inp[i];
return hist_avg = hist_sum / VALUE(NUM);
}
};
template <typename TYPE, typename VALUE, int NUM, bool NORM = true>
class SMA2
{
Delay<TYPE, NUM> delay;
TYPE sum;
public:
SMA2() : sum(0)
{
}
TYPE operator () (TYPE input)
{
sum += input - delay(input);
if (NORM)
return sum / VALUE(NUM);
return sum;
}
};
template <typename TYPE, typename VALUE, int NUM, bool NORM = true>
class SMA3
{
Delay<TYPE, NUM> delay;
Kahan<TYPE> sum;
public:
SMA3() : sum(0)
{
}
TYPE operator () (TYPE input)
{
sum(-delay(input));
if (NORM)
return sum(input) / VALUE(NUM);
return sum(input);
}
};
template <typename TYPE, typename VALUE, int NUM, bool NORM = true>
class SMA4
{
SWA<TYPE, std::plus<TYPE>, NUM> swa;
public:
SMA4() : swa(0)
{
}
TYPE operator () (TYPE input)
{
if (NORM)
return swa(input) / VALUE(NUM);
return swa(input);
}
};
}