Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ripred committed Jun 18, 2023
1 parent 3517abf commit 9ceecd1
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/Smooth/Smooth.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "Smooth.h"

#define SMOOTHED_SAMPLE_SIZE 10

// Smoothing average object
Smooth average(SMOOTHED_SAMPLE_SIZE);

// Simulated moving sample
int sample = 0;

void setup() {
Serial.begin(115200);
}

void loop() {
// get a random -10, 0, or +10
int const updown = 10 * (random(0, 3) - 1);

sample += updown;

average += sample;

char scratch[64] = "";
snprintf(scratch, sizeof(scratch), "count: %4d, sample: %3d, average: %3d\n",
average.get_count(),
updown,
(int) average());

Serial.print(scratch);
}
24 changes: 24 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
########################################################
# Syntax Coloring Map for Arduino Smooth Library
########################################################
# https://github.com/ripred/Smooth
########################################################
# Datatypes (KEYWORD1)
########################################################
Smooth KEYWORD1

########################################################
# Methods, Functions, and Globals (KEYWORD2)
########################################################
Smooth KEYWORD2
add KEYWORD2
get_avg KEYWORD2
get_count KEYWORD2
get_window KEYWORD2
set_window KEYWORD2
reset KEYWORD2

########################################################
# Constants (LITERAL1)
########################################################

10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=Smooth
version=1.0.0
author=Trent M. Wyatt
maintainer=Trent M. Wyatt <robotics@trentwyatt.com>
sentence=Keep smooth running averages without using arrays! The sample size is adjustable at runtime.
paragraph=Additionally the average is available and accurate even before N samples have been received.
category=Data Processing
url=https://github.com/ripred/Smooth
architectures=*
includes=Smooth.h
74 changes: 74 additions & 0 deletions src/Smooth.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Smooth.h
*
* implementation file for Smooth averaging class
*
* version 1.0 - June, 2023 ++trent m. wyatt
*
*/
#include "Smooth.h"

Smooth::Smooth(int const window, int const c, double const a) :
set_size(window),
count(c),
avg(a)
{
}

// get the current running average
double Smooth::get_avg() const
{
return avg;
}

// get the total sample count
int Smooth::get_count() const
{
return count;
}

// get the current window size (num samples)
int Smooth::get_window() const
{
return set_size;
}

// set the current window size (num samples)
void Smooth::set_window(int const size)
{
set_size = size;
}

// reset the smoothing object
void Smooth::reset(int const window)
{
set_size = window;
count = 0;
avg = 0.0;
}

// add a sample to the set and return the running average
double Smooth::add(double const val)
{
int num = ++count;
if (num > set_size) {
num = set_size;
}

double run_coef = double(num - 1) / double(num);
double val_coef = 1.0 / double(num);

avg = avg * run_coef + val * val_coef;

return avg;
}

// operator overload for +=
double Smooth::operator += (double const term) {
return add(term);
}

// operator overload for ()
double Smooth::operator () () {
return avg;
}
49 changes: 49 additions & 0 deletions src/Smooth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Smooth.h
*
* header file for Smooth averaging class
*
* version 1.0 - June, 2023 ++trent m. wyatt
*
*/
#ifndef SMOOTH_H_INCL
#define SMOOTH_H_INCL

#include <inttypes.h>

class Smooth {
private:
int set_size;
int count;
double avg;

public:
Smooth(int const window = 1, int const c = 0, double const a = 0.0);

// get the current running average
double get_avg() const;

// get the total sample count
int get_count() const;

// get the current window size (num samples)
int get_window() const;

// set the current window size (num samples)
void set_window(int const size);

// reset the smoothing object
void reset(int const window);

// add a sample to the set and return the running average
double add(double const val);

// operator overload for +=
double operator += (double const term);

// operator overload for ()
double operator () ();

}; // class Smooth

#endif // #ifndef SMOOTH_H_INCL

0 comments on commit 9ceecd1

Please sign in to comment.