-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingAverage.h
More file actions
98 lines (82 loc) · 2.3 KB
/
Copy pathMovingAverage.h
File metadata and controls
98 lines (82 loc) · 2.3 KB
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
/*
* MovingAverage.h
*
* Implements a moving average filter with a circular buffer.
* As new samples are added, the oldest sample is automatically
* removed, creating a "marching" or "rolling" average.
*/
#ifndef MOVING_AVERAGE_H
#define MOVING_AVERAGE_H
class MovingAverage {
private:
float* buffer; // Circular buffer for samples
int bufferSize; // Size of the buffer
int currentIndex; // Current position in buffer
int sampleCount; // Number of samples added so far
float sum; // Running sum of samples
public:
// Constructor
MovingAverage(int size) {
bufferSize = size;
buffer = new float[bufferSize];
currentIndex = 0;
sampleCount = 0;
sum = 0.0;
// Initialize buffer
for (int i = 0; i < bufferSize; i++) {
buffer[i] = 0.0;
}
}
// Destructor
~MovingAverage() {
delete[] buffer;
}
// Add a new sample to the moving average
void addSample(float value) {
// Subtract the value being replaced from the sum
if (sampleCount >= bufferSize) {
sum -= buffer[currentIndex];
}
// Add new value to buffer and sum
buffer[currentIndex] = value;
sum += value;
// Update index (wrap around using modulo)
currentIndex = (currentIndex + 1) % bufferSize;
// Track number of samples (up to buffer size)
if (sampleCount < bufferSize) {
sampleCount++;
}
}
// Get the current average
float getAverage() {
if (sampleCount == 0) {
return 0.0;
}
return sum / sampleCount;
}
// Check if buffer is full
bool isFull() {
return sampleCount >= bufferSize;
}
// Get the number of samples currently in the buffer
int getSampleCount() {
return sampleCount;
}
// Reset the moving average
void reset() {
currentIndex = 0;
sampleCount = 0;
sum = 0.0;
for (int i = 0; i < bufferSize; i++) {
buffer[i] = 0.0;
}
}
// Get a specific sample from the buffer (for debugging)
float getSample(int index) {
if (index < 0 || index >= sampleCount) {
return 0.0;
}
return buffer[index];
}
};
#endif // MOVING_AVERAGE_H