https://github.com/ripred/smooth
Keep smooth running averages without using arrays! Uses exponential moving averages and only 8-bytes no matter how many samples! No looping! No Arrays! Constant Compute Time! The sample size is adjustable at runtime. The average is available and accurate even before N samples have been received. Now with Callbacks!
https://github.com/ripred/smooth
Last synced: 10 months ago
JSON representation
Keep smooth running averages without using arrays! Uses exponential moving averages and only 8-bytes no matter how many samples! No looping! No Arrays! Constant Compute Time! The sample size is adjustable at runtime. The average is available and accurate even before N samples have been received. Now with Callbacks!
- Host: GitHub
- URL: https://github.com/ripred/smooth
- Owner: ripred
- License: mit
- Created: 2023-06-18T11:04:55.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-14T16:41:32.000Z (over 1 year ago)
- Last Synced: 2025-02-14T17:36:12.416Z (over 1 year ago)
- Language: C++
- Homepage: https://github.com/ripred/Smooth
- Size: 61.5 KB
- Stars: 59
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://github.com/marketplace/actions/arduino_ci)
[](https://github.com/ripred/Smooth/actions/workflows/arduino-lint.yml)
[](https://github.com/ripred/Smooth/actions/workflows/jsoncheck.yml)
[](https://github.com/ripred/Smooth/releases)
[](https://registry.platformio.org/libraries/ripred/Smooth)
[](https://github.com/ripred/Smooth/blob/master/LICENSE)

[](https://github.com/ripred/Smooth)
[](https://github.com/ripred/Smooth)
# Smooth Arduino Library
Keep smooth running averages without using arrays! No arrays. No Looping. Fast and Constant Time regardless of the sample window size!
The sample size is adjustable at runtime. The average is available and accurate even before N samples have been received.
The `Smooth` object uses 8 bytes of memory. That's it. No matter how large the window size. Perfect for smoothing and filtering
signals from noisy devices **like accelerometers** and **stable potentiometer readings**!
update: added support for change, upper and lower bounds callbacks!
### Example sketch:
```cpp
#include
#include
#define SMOOTHED_SAMPLE_SIZE 10
// Smoothing average object
Smooth average(SMOOTHED_SAMPLE_SIZE);
// Simulated wandering sample value
int sample = 0;
void setup() {
Serial.begin(115200);
randomSeed(analogRead(A0));
}
void loop() {
// get a random -1, 0, or +1 value
int const updown = random(0, 3) - 1;
// move our simulated wandering sample up or down
sample += updown;
// add it to the running average
average += sample; // or call average.add(sample)
// display the results:
char scratch[64] = "";
snprintf(scratch, sizeof(scratch), "count: %4d, sample: %3d, average: %3d\n",
average.get_count(),
updown,
(int) average()); // or call average.get_avg()
Serial.print(scratch);
}
```
example output:
```
count: 1, sample: 0, average: 0
count: 2, sample: 0, average: 0
count: 3, sample: 1, average: 0
count: 4, sample: 2, average: 0
count: 5, sample: 2, average: 1
count: 6, sample: 3, average: 1
count: 7, sample: 2, average: 1
count: 8, sample: 3, average: 1
count: 9, sample: 4, average: 1
count: 10, sample: 4, average: 2
count: 11, sample: 3, average: 2
count: 12, sample: 4, average: 2
count: 13, sample: 3, average: 2
count: 14, sample: 4, average: 2
count: 15, sample: 3, average: 2
count: 16, sample: 4, average: 2
count: 17, sample: 4, average: 2
count: 18, sample: 4, average: 3
count: 19, sample: 4, average: 3
count: 20, sample: 4, average: 3
count: 21, sample: 3, average: 3
count: 22, sample: 3, average: 3
count: 23, sample: 2, average: 3
count: 24, sample: 3, average: 3
count: 25, sample: 3, average: 3
count: 26, sample: 2, average: 2
count: 27, sample: 1, average: 2
count: 28, sample: 0, average: 2
count: 29, sample: 1, average: 2
count: 30, sample: 1, average: 2
count: 31, sample: 1, average: 2
count: 32, sample: 2, average: 2
count: 33, sample: 3, average: 2
count: 34, sample: 3, average: 2
count: 35, sample: 4, average: 2
count: 36, sample: 3, average: 2
count: 37, sample: 3, average: 2
count: 38, sample: 3, average: 2
count: 39, sample: 2, average: 2
```