Skip to content

Instantly share code, notes, and snippets.

@alexliesenfeld
Last active January 27, 2019 14:58
Show Gist options
  • Save alexliesenfeld/d59b5454d5516b8d18479a707b6336d6 to your computer and use it in GitHub Desktop.
Save alexliesenfeld/d59b5454d5516b8d18479a707b6336d6 to your computer and use it in GitHub Desktop.
A C++ class to smooth out DSP parameters
#ifndef SMOOTHER_H
#define SMOOTHER_H
#include <math.h>
class Smoother {
private:
double mSampleRate;
double mLastOutput{};
double mA{};
double mB{};
public:
Smoother(double pInitialSmoothingTimeInMs, double pSampleRate) : mSampleRate(pSampleRate) {
setSmoothingTime(pInitialSmoothingTimeInMs);
}
double process(double pInput) {
mLastOutput = (pInput * mB) + (mLastOutput * mA);
return mLastOutput;
}
void setSmoothingTime(double pSmoothingTimeInMs) {
mA = exp(-(M_PI * 2) / (pSmoothingTimeInMs * 0.001 * mSampleRate));
mB = 1.0 - mA;
}
};
#endif // SMOOTHER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment