Smoother

Smooths the incoming signal by removing fast variations and noise (high frequencies).

_images/Plaquette-Smooth.png

Example

Smooth a sensor over time.

#include <Plaquette.h>

AnalogIn sensor(A0);

// Smooths over time window of 10 seconds.
Smoother smoother(10.0);

StreamOut serialOut(Serial);

void begin() {}

void step() {
  // Smooth value and send it to serial output.
  sensor >> smoother >> serialOut;
}

Note

The filter uses an exponential moving average which corresponds to a form of low-pass filter.

Reference

class Smoother : public Unit, public MovingAverage

Simple moving average transform filter.

Public Functions

Smoother(Engine &engine = Engine::primary())

Constructor with default smoothing.

Parameters

engine – the engine running this unit

Smoother(float smoothingWindow, Engine &engine = Engine::primary())

Constructor with smoothing window.

Parameters
  • smoothingWindow – the time window over which the smoothing applies (in seconds)

  • engine – the engine running this unit

virtual float put(float value)

Pushes value into the unit.

Parameters

value – the value sent to the unit

Returns

the new value of the unit

inline virtual float get()

Returns smoothed value.

inline float seconds() const

Returns engine time in seconds.

inline uint32_t milliSeconds() const

Returns engine time in milliseconds.

inline uint64_t microSeconds() const

Returns engine time in microseconds.

inline unsigned long nSteps() const

Returns number of engine steps.

inline float sampleRate() const

Returns engine sample rate.

inline float samplePeriod() const

Returns enginesample period.

inline operator float()

Object can be used directly to access its value.

inline virtual float mapTo(float toLow, float toHigh)

Maps value to new range.

This function guarantees that the value is within [toLow, toHigh]. If the unit’s values are unbounded, returns get() constrained to [toLow, toHigh].

inline explicit operator bool()

Operator that allows usage in conditional expressions.

void infiniteTimeWindow()

Sets to “infinite” smoothing window.

void timeWindow(float seconds)

Changes the smoothing window (expressed in seconds).

inline float timeWindow() const

Returns the smoothing window (expressed in seconds).

bool timeWindowIsInfinite() const

Returns true if time window is infinite.

void cutoff(float hz)

Changes the smoothing window cutoff frequency (expressed in Hz).

float cutoff() const

Returns the smoothing window cutoff frequency (expressed in Hz).

float alpha(float sampleRate) const

Returns the alpha value computed from given sample rate.

void reset()

Resets the moving average.

virtual float update(float v, float sampleRate = 1, bool forceAlpha = false)

Updates the moving average with new value v# (also returns the current value).

inline unsigned int nSamples() const

Returns the number of samples processed since reset().

void amendUpdate(float previousValue, float newValue, float sampleRate = 1, bool forceAlpha = false)

Performs an amendment of latest update (needs to be called with same sampleRate# and forceAlpha# parameters).

Public Static Functions

static inline bool analogToDigital(float f)

Converts analog (float) value to digital (bool) value.

static inline float digitalToAnalog(bool b)

Converts digital (bool) value to analog (float) value.

static float alpha(float sampleRate, float timeWindow, unsigned int nSamples = UINT_MAX)

Returns the alpha value computed from given sample rate, time window, and number of samples.

static void applyUpdate(float &runningValue, float newValue, float alpha)

Applies a single update on runningValue# with new sample newValue# and mixing factor alpha#.

static void applyAmendUpdate(float &runningValue, float previousValue, float newValue, float alpha)

Performs an amendment of latest update (needs to be called with same alpha# parameter).

static float computeUpdate(float runningValue, float newValue, float alpha)

Computes a single update on runningValue# with new sample newValue# and mixing factor alpha#.

See Also