mirror of
https://github.com/okalachev/flix.git
synced 2026-02-17 15:41:32 +00:00
Begin with zero instead of the initializing value, as the latter doesn't make much sense in practice, but complicates the code much.
28 lines
636 B
C++
28 lines
636 B
C++
// Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com>
|
|
// Repository: https://github.com/okalachev/flix
|
|
|
|
// Low pass filter implementation
|
|
|
|
#pragma once
|
|
|
|
template <typename T> // Using template to make the filter usable for scalar and vector values
|
|
class LowPassFilter {
|
|
public:
|
|
float alpha; // smoothing constant, 1 means filter disabled
|
|
T output;
|
|
|
|
LowPassFilter(float alpha): alpha(alpha) {};
|
|
|
|
T update(const T input) {
|
|
return output += alpha * (input - output);
|
|
}
|
|
|
|
void setCutOffFrequency(float cutOffFreq, float dt) {
|
|
alpha = 1 - exp(-2 * PI * cutOffFreq * dt);
|
|
}
|
|
|
|
void reset() {
|
|
output = T(); // set to zero
|
|
}
|
|
};
|