Skip to content

Instantly share code, notes, and snippets.

@n8allan
Created September 14, 2023 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8allan/b38de1073b297ef1651c3a040b124058 to your computer and use it in GitHub Desktop.
Save n8allan/b38de1073b297ef1651c3a040b124058 to your computer and use it in GitHub Desktop.
Exponential filter in C++
int getSmooth6() {
// ASSUMPTION: history has at least 6 entries
auto a = (getOffset(0) + getOffset(-1)) / 2;
auto b = (getOffset(0) + getOffset(-2)) / 2;
auto c = (getOffset(0) + getOffset(-3)) / 2;
auto d = (getOffset(-1) + getOffset(-2)) / 2;
auto e = (getOffset(-1) + getOffset(-3)) / 2;
auto f = (getOffset(-2) + getOffset(-3)) / 2;
return (((((a + b * 2) / 3 + c * 2) / 3 + d * 2) / 3 + e * 2) / 3 + f * 2) / 3;
}
// Exponential filter
int getSmooth3(int offset) {
// ASSUMPTION: capacity is >= 3
auto a = (getOffset(offset) + getOffset(offset - 1)) / 2;
auto b = (getOffset(offset) + getOffset(offset - 2)) / 2;
auto c = (getOffset(offset - 1) + getOffset(offset - 2)) / 2;
return ((a + b * 2) / 3 + c * 2) / 3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment