Permalink
Browse files

ExponentiallyWeightedMovingAverage

  • Loading branch information...
mlang committed Jul 25, 2017
1 parent ad943ba commit 1714a35f73945f78777e1e2999a07cdb0a4b09c3
Showing with 35 additions and 23 deletions.
  1. +2 −21 edge.cpp
  2. +27 −0 lib/dsp.hpp
  3. +6 −2 lib/jack.hpp
View
@@ -2,30 +2,12 @@
#include <thread>
#include <jack.hpp>
// https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
template<typename T>
class ExponentiallyWeightedMovingAverage {
T Average;
T const Weight;
public:
explicit ExponentiallyWeightedMovingAverage(T Weight, T Initial = T(0))
: Average(Initial), Weight(Weight) {}
ExponentiallyWeightedMovingAverage &operator()(T Value) {
Average = Weight * Value + (T(1) - Weight) * Average;
return *this;
}
operator T() const noexcept { return Average; }
};
#include <dsp.hpp>
class EdgeDetect : public JACK::Client {
JACK::AudioIn In;
decltype(In)::value_type PreviousDifference = 0;
ExponentiallyWeightedMovingAverage<decltype(In)::value_type>
BrlCV::ExponentiallyWeightedMovingAverage<decltype(In)::value_type>
FastAverage, SlowAverage;
std::size_t PulseWidth = 0;
@@ -66,4 +48,3 @@ int main() {
return EXIT_SUCCESS;
}
View
@@ -0,0 +1,27 @@
#if !defined(BrlCV_DSP_HPP)
#define BrlCV_DSP_HPP
namespace BrlCV {
// 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 = Weight * Value + OneMinusWeight * Average;
return *this;
}
operator T() const noexcept { return Average; }
};
} // namespace BrlCV
#endif // BrlCV_DSP_HPP
View
@@ -29,15 +29,19 @@ class AudioIn : public Port {
AudioIn(Client *, std::string Name);
public:
gsl::span<float> buffer(std::int32_t Frames);
using value_type = float;
gsl::span<value_type> buffer(std::int32_t Frames);
};
class AudioOut : public Port {
friend class Client;
AudioOut(Client *, std::string Name);
public:
gsl::span<float> buffer(std::int32_t Frames);
using value_type = float;
gsl::span<value_type> buffer(std::int32_t Frames);
};
class Client {

0 comments on commit 1714a35

Please sign in to comment.