Permalink
Browse files

edge detection code

  • Loading branch information...
mlang committed Jun 5, 2018
1 parent 0d2b772 commit c0b60646541ad340cfabcb80b02a7c86e171f2da
Showing with 50 additions and 0 deletions.
  1. +25 −0 EWMA.h
  2. +24 −0 EdgeDetect.h
  3. +1 −0 render.cpp
25 EWMA.h
@@ -0,0 +1,25 @@
#if !defined(EWMA_H_DEFINED)
#define EWMA_H_DEFINED
// https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
template<typename T>
class ExponentiallyWeightedMovingAverage {
T Average;
T const Weight, OneMinusWeight;
public:
explicit ExponentiallyWeightedMovingAverage(T Weight, T Initial = T(0))
: Average(Initial), Weight(Weight), OneMinusWeight(T(1) - Weight) {}
ExponentiallyWeightedMovingAverage &operator()(T Value) {
Average = Value * Weight + Average * OneMinusWeight;
return *this;
}
operator T() const noexcept { return Average; }
};
template<typename T> using EWMA = ExponentiallyWeightedMovingAverage<T>;
#endif // EWMA_H_DEFINED
@@ -0,0 +1,24 @@
#ifndef EDGEDETECT_H
#define EDGEDETECT_H
#include "EWMA.h"
template<typename T>
class EdgeDetect {
T previousDifference = T(0);
T threshold;
EWMA<T> fastAverage, slowAverage;
public:
EdgeDetect(T threshold = 0.2)
: threshold(threshold)
, fastAverage(0.25), slowAverage(0.0625)
{}
bool operator()(T value) {
auto const difference = fastAverage(value) - slowAverage(value);
auto const rising = previousDifference < threshold && difference > threshold;
previousDifference = difference;
return rising;
}
};
#endif
@@ -1,4 +1,5 @@
#include "AuxTask.h"
#include "EdgeDetect.h"
#include "RTPipe.h"
#include "RTQueue.h"
#include <Bela.h>

0 comments on commit c0b6064

Please sign in to comment.