Last active
February 27, 2023 13:13
-
-
Save 3846masa/5628f711e86fd62bea56b18e32177c60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LowPassFilter { | |
constructor(alpha) { | |
this.setAlpha(alpha); | |
this.y = null; | |
this.s = null; | |
} | |
setAlpha(alpha) { | |
if (alpha <= 0 || alpha > 1.0) { | |
throw new Error(); | |
} | |
this.alpha = alpha; | |
} | |
filter(value, timestamp, alpha) { | |
if (alpha) { | |
this.setAlpha(alpha); | |
} | |
let s; | |
if (!this.y) { | |
s = value; | |
} else { | |
s = this.alpha * value + ( 1.0 - this.alpha ) * this.s; | |
} | |
this.y = value; | |
this.s = s; | |
return s; | |
} | |
lastValue() { | |
return this.y; | |
} | |
} | |
class OneEuroFilter { | |
constructor(freq, minCutOff=1.0, beta=0.0, dCutOff=1.0) { | |
if (freq <= 0 || minCutOff <= 0 || dCutOff <= 0) { | |
throw new Error(); | |
} | |
this.freq = freq; | |
this.minCutOff = minCutOff; | |
this.beta = beta; | |
this.dCutOff = dCutOff; | |
this.x = new LowPassFilter(this.alpha(this.minCutOff)); | |
this.dx = new LowPassFilter(this.alpha(this.dCutOff)); | |
this.lasttime = null; | |
} | |
alpha(cutOff) { | |
const te = 1.0 / this.freq; | |
const tau = 1.0 / ( 2 * Math.PI * cutOff ); | |
return 1.0 / ( 1.0 + tau / te ); | |
} | |
filter(x, timestamp=null) { | |
if (this.lasttime && timestamp) { | |
this.freq = 1.0 / ( timestamp - this.lasttime ); | |
} | |
this.lasttime = timestamp; | |
const prevX = this.x.lastValue(); | |
const dx = (!prevX) ? 0.0 : ( x - prevX ) * this.freq; | |
const edx = this.dx.filter(dx, timestamp, this.alpha(this.dCutOff)); | |
const cutOff = this.minCutOff + this.beta * Math.abs(edx); | |
return this.x.filter(x, timestamp, this.alpha(cutOff)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typescript version: https://gist.github.com/ThorstenBux/323183bb0bc2ccb92ff23ebdf3de6408
thanks @3846masa