mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
testing
This commit is contained in:
parent
eb297bc993
commit
64c247fee4
@ -1,57 +0,0 @@
|
||||
//
|
||||
// FILE: RunningAverage.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.01
|
||||
// PURPOSE: RunningAverage library for Arduino
|
||||
//
|
||||
// The library does store the last N individual values, to
|
||||
// calculate the running average.
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.00 - 2011-01-30 initial version
|
||||
// 0.1.01 - 2011-02-28 fixed missing destructor in .h
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include "RunningAverage.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
RunningAverage::RunningAverage(int n)
|
||||
{
|
||||
_size = n;
|
||||
_ar = (float*) malloc(_size * sizeof(float));
|
||||
clr();
|
||||
}
|
||||
|
||||
RunningAverage::~RunningAverage()
|
||||
{
|
||||
free(_ar);
|
||||
}
|
||||
|
||||
// resets all counters
|
||||
void RunningAverage::clr()
|
||||
{
|
||||
_cnt = 0;
|
||||
_idx = 0;
|
||||
_sum = 0.0;
|
||||
for (int i=0; i< _size; i++) _ar[i] = 0.0;
|
||||
}
|
||||
|
||||
// adds a new value to the data-set
|
||||
void RunningAverage::add(float f)
|
||||
{
|
||||
_sum -= _ar[_idx];
|
||||
_ar[_idx] = f;
|
||||
_sum += _ar[_idx];
|
||||
_idx = (_idx + 1) % _size;
|
||||
if (_cnt < _size) _cnt++;
|
||||
}
|
||||
|
||||
// returns the average of the data-set added sofar
|
||||
float RunningAverage::avg()
|
||||
{
|
||||
return _sum / _cnt;
|
||||
}
|
||||
|
||||
// END OF FILE
|
@ -1,33 +0,0 @@
|
||||
#ifndef RunningAverage_h
|
||||
#define RunningAverage_h
|
||||
//
|
||||
// FILE: RunningAverage.h
|
||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||
// PURPOSE: RunningAverage library for Arduino
|
||||
// VERSION: 0.1.01
|
||||
// URL: http://arduino.cc/playground/Main/RunningAverage
|
||||
// HISTORY: See RunningAverage.cpp
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
class RunningAverage
|
||||
{
|
||||
public:
|
||||
RunningAverage(void);
|
||||
RunningAverage(int);
|
||||
~RunningAverage();
|
||||
void clr();
|
||||
void add(float);
|
||||
float avg();
|
||||
|
||||
protected:
|
||||
int _size;
|
||||
int _cnt;
|
||||
int _idx;
|
||||
float _sum;
|
||||
float * _ar;
|
||||
};
|
||||
|
||||
#endif
|
||||
// END OF FILE
|
@ -1,28 +0,0 @@
|
||||
#include "RunningAverage.h"
|
||||
|
||||
RunningAverage raMinute(60);
|
||||
RunningAverage raHour(60);
|
||||
|
||||
int samples = 0;
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("Demo RunningAverage lib - average per minute & hour");
|
||||
raHour.clr();
|
||||
raMinute.clr();
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
long rn = random(0, 100);
|
||||
raMinute.add(rn);
|
||||
samples++;
|
||||
|
||||
if (samples % 60 == 0) raHour.add(raMinute.avg());
|
||||
|
||||
Serial.print(" raMinute: ");
|
||||
Serial.print(raMinute.avg(), 4);
|
||||
Serial.print(" raHour: ");
|
||||
Serial.println(raHour.avg(), 4);
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
#include "RunningAverage.h"
|
||||
|
||||
RunningAverage myRA(10); // use default size
|
||||
int samples = 0;
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("Demo RunningAverage lib");
|
||||
myRA.clr(); // explicitly start clean
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
long rn = random(0, 100);
|
||||
myRA.add(rn/100.0);
|
||||
samples++;
|
||||
Serial.print("Running Average: ");
|
||||
Serial.println(myRA.avg(), 4);
|
||||
|
||||
if (samples == 300)
|
||||
{
|
||||
samples = 0;
|
||||
myRA.clr();
|
||||
}
|
||||
delay(100);
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
//
|
||||
// FILE: RunningMedian.cpp
|
||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||
// VERSION: 0.1
|
||||
// PURPOSE: RunningMedian library for Arduino
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.00 - 2011-02-16 initial version
|
||||
// 0.1.01 - 2011-02-22 added remarks from CodingBadly
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include "RunningMedian.h"
|
||||
|
||||
RunningMedian::RunningMedian()
|
||||
{
|
||||
// size hardcoded
|
||||
clear();
|
||||
}
|
||||
|
||||
// resets all counters
|
||||
void RunningMedian::clear()
|
||||
{
|
||||
_cnt = 0;
|
||||
_idx = 0;
|
||||
}
|
||||
|
||||
// adds a new value to the data-set
|
||||
// or overwrites the oldest if full.
|
||||
void RunningMedian::add(long value)
|
||||
{
|
||||
_ar[_idx++] = value;
|
||||
if ( _idx >= MEDIAN_SIZE ) _idx = 0; // wrap around
|
||||
if (_cnt < MEDIAN_SIZE) _cnt++;
|
||||
return;
|
||||
}
|
||||
|
||||
long RunningMedian::getMedian()
|
||||
{
|
||||
sort();
|
||||
return _as[_cnt/2];
|
||||
}
|
||||
|
||||
|
||||
void RunningMedian::sort()
|
||||
{
|
||||
// copy
|
||||
for (uint8_t i=0; i< _cnt; i++) _as[i] = _ar[i];
|
||||
|
||||
// sort all
|
||||
for (uint8_t i=0; i< _cnt-1; i++)
|
||||
{
|
||||
uint8_t m = i;
|
||||
for (uint8_t j=i+1; j< _cnt; j++)
|
||||
{
|
||||
if (_as[j] < _as[m]) m = j;
|
||||
}
|
||||
if (m != i)
|
||||
{
|
||||
long t = _as[m];
|
||||
_as[m] = _as[i];
|
||||
_as[i] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// END OF FILE
|
@ -1,37 +0,0 @@
|
||||
#ifndef RunningMedian_h
|
||||
#define RunningMedian_h
|
||||
//
|
||||
// FILE: RunningMedian.h
|
||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||
// PURPOSE: RunningMedian library for Arduino
|
||||
// VERSION: 0.1.01
|
||||
// URL: http://arduino.cc/playground/Main/RunningMedian
|
||||
// HISTORY: See RunningMedian.cpp
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#define RUNNINGMEDIANVERSION "1.0"
|
||||
// should at least be 5 and odd.
|
||||
#define MEDIAN_SIZE 5
|
||||
|
||||
class RunningMedian
|
||||
{
|
||||
public:
|
||||
RunningMedian();
|
||||
void clear();
|
||||
void add(long);
|
||||
long getMedian();
|
||||
|
||||
protected:
|
||||
uint8_t _cnt;
|
||||
uint8_t _idx;
|
||||
long _ar[MEDIAN_SIZE];
|
||||
long _as[MEDIAN_SIZE];
|
||||
void sort();
|
||||
};
|
||||
|
||||
#endif
|
||||
// END OF FILE
|
Loading…
Reference in New Issue
Block a user