Go Down

Topic: RunningAverage Class on playground (updated) (Read 20535 times) previous topic - next topic

odometer

There are tricks you can use to improve accuracy when adding up a lot of floats.
For example, you can use a long to hold the "whole" part of the sum, and a float to hold the fractional part.

robtillaart

#46
Feb 09, 2016, 09:52 pm Last Edit: Feb 09, 2016, 09:55 pm by robtillaart
Good point, it's a variation of - https://en.wikipedia.org/wiki/Kahan_summation_algorithm

that said accuracy tricks often come at a price of footprint, performance or sometimes the working range of the code. The more you know about the data to process the easier/better you can tune an algorithm for accuracy/performance etc.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jano

hi!

First: Thanks for publishing your code, great work!

I'm using it to filter the input of opto sensors for reading the speed of two dc motors, but since the speed of the motors is variable the cutoff of the filter should too, exist a way to change the amount of samples in the running average on the fly?

lucy

Can I run 3 copies of RunningAverage.h at the same time? I am attempting to find relative accelerometer motion in three axis simultaneously to produce a z score. this is a test using example furnished with the library. Can I interleave or multiplex. Sample size will probably need tobe altered on thefly as code progresses. Thanks a lot.


//
//    FILE: runningAverageTest.pde
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.01
//    DATE: 2012-12-30
//
// PUPROSE: show working of runningAverage
//

#include "RunningAverage.h"

RunningAverage myRA(10);
int samples = 0;

void setup(void)
{
  Serial.begin(115200);
  Serial.println("Demo RunningAverage lib");
  Serial.print("Version: ");
  Serial.println(RUNNINGAVERAGE_LIB_VERSION);
  myRA.clear(); // explicitly start clean
}

void loop(void)
{ int x=analogRead(A0);
  int u=myRA.getAverage();
  int s=myRA.GetStandardDeviation();
 
  int z=(x-u)/s;
  //long rn = random(0, 1000);
  myRA.addValue(x);
 // samples++;
 // Serial.print(samples);
 // Serial.print("\t Running Average: ");
  Serial.println(z);
 
 /* if (samples == 300)
  {
    samples = 0;
    myRA.clear();
    Serial.println();
  }*/
  delay(10);
}

robtillaart

#49
Jul 24, 2017, 02:19 pm Last Edit: Jul 24, 2017, 02:20 pm by robtillaart
Yes you can by allocating 3 running Average objects.

Add the X values to the RA_X object, the Y values to the RA_Y object etc

see small sketch below

Code: (not tested) [Select]
//
//    FILE: runningAverage.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.0.1
//    DATE: 2017-07-24
// PURPOSE: demo
//
// HISTORY:
// 0.0.1 - 2017-07-24 initial version

#include "RunningAverage.h"

RunningAverage RA_X(10);
RunningAverage RA_Y(10);
RunningAverage RA_Z(10);

void setup(void)
{
  Serial.begin(115200);
  Serial.println("Demo RunningAverage lib");
  Serial.print("Version: ");
  Serial.println(RUNNINGAVERAGE_LIB_VERSION);

  RA_X.clear();
  RA_Y.clear();
  RA_Z.clear();
}

void loop(void)
{
  int x = analogRead(A0);
  int y = analogRead(A1);
  int z = analogRead(A2);

  RA_X.addValue(x);
  RA_Y.addValue(y);
  RA_Z.addValue(z);

  Serial.print("RA_X: \t");
  Serial.println(RA_X.getAverage());
  Serial.println(RA_X.GetStandardDeviation());

  // idem for RA_Y and RA_Z

  int u = RA_X.getAverage();
  int s = RA_X.GetStandardDeviation();
  int zzz = (x - u) / s;

  Serial.print("ZZZ:\t");
  Serial.println(zzz);

  delay(10);
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Mikusban

Is it possible to only clear Min/Max values?

Go Up