-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@uppy/status-bar: Filtered ETA (#4458)
- Loading branch information
Showing
4 changed files
with
104 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Low-pass filter using Exponential Moving Averages (aka exponential smoothing) | ||
* Filters a sequence of values by updating the mixing the previous output value | ||
* with the new input using the exponential window function | ||
* | ||
* @param {*} newValue the n-th value of the sequence | ||
* @param {*} previousSmoothedValue the exponential average of the first n-1 values | ||
* @param {*} halfLife value of `dt` to move the smoothed value halfway between `previousFilteredValue` and `newValue` | ||
* @param {*} dt time elapsed between adding the (n-1)th and the n-th values | ||
* @returns the exponential average of the first n values | ||
*/ | ||
export default function emaFilter (newValue, previousSmoothedValue, halfLife, dt) { | ||
if (halfLife === 0 || newValue === previousSmoothedValue) return newValue | ||
if (dt === 0) return previousSmoothedValue | ||
|
||
return newValue + (previousSmoothedValue - newValue) * (2 ** (-dt / halfLife)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { describe, expect, it } from '@jest/globals' | ||
import emaFilter from './emaFilter.js' | ||
|
||
describe('emaFilter', () => { | ||
it('should calculate the exponential average', () => { | ||
expect(emaFilter(1, 0, 0, 1)).toBe(1) | ||
|
||
expect(emaFilter(1, 0, 2, 0)).toBe(0) | ||
expect(emaFilter(1, 0, 2, 2)).toBeCloseTo(0.5) | ||
expect(emaFilter(1, 0, 2, 4)).toBeCloseTo(0.75) | ||
expect(emaFilter(1, 0, 2, 6)).toBeCloseTo(0.875) | ||
|
||
expect(emaFilter(0, 1, 2, 2)).toBeCloseTo(0.5) | ||
expect(emaFilter(0, 1, 2, 4)).toBeCloseTo(0.25) | ||
expect(emaFilter(0, 1, 2, 6)).toBeCloseTo(0.125) | ||
|
||
expect(emaFilter(0.5, 1, 2, 4)).toBeCloseTo(0.625) | ||
expect(emaFilter(1, 0.5, 2, 4)).toBeCloseTo(0.875) | ||
}) | ||
it('should behave like exponential moving average', () => { | ||
const firstValue = 1 | ||
const newValue = 10 | ||
const step = 0.618033989 | ||
const halfLife = 2 | ||
let lastFilteredValue = firstValue | ||
for (let i = 0; i < 10; ++i) { | ||
lastFilteredValue = emaFilter(newValue, lastFilteredValue, halfLife, step) | ||
expect(lastFilteredValue).toBeCloseTo(emaFilter(newValue, firstValue, halfLife, step * (i + 1))) | ||
} | ||
}) | ||
}) |