diff --git a/typescript/Readme.md b/typescript/Readme.md index 451f101..61db2f8 100644 --- a/typescript/Readme.md +++ b/typescript/Readme.md @@ -28,6 +28,107 @@ let filtered = f.filter(noisyvalue, timestamp); ``` + +## Doc + +### constructor + +• **new OneEuroFilter**(`freq`, `mincutoff`, `beta`, `dcutoff`) + +Constructs a 1 euro filter. + +#### Parameters + +| Name | Type | Default value | Description | +| :------ | :------ | :------ | :------ | +| `freq` | `number` | `undefined` | An estimate of the frequency in Hz of the signal (> 0), if timestamps are not available. | +| `mincutoff` | `number` | `1.0` | Min cutoff frequency in Hz (> 0). Lower values allow to remove more jitter. | +| `beta` | `number` | `0.0` | Parameter to reduce latency (> 0). | +| `dcutoff` | `number` | `1.0` | Used to filter the derivates. 1 Hz by default. Change this parameter if you know what you are doing. | + + +### filter + +▸ **filter**(`value`, `timestamp`): `number` + +Returns the filtered value. + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `value` | `number` | Noisy value to filter | +| `timestamp` | `number` | (optional) timestamp in seconds | + +### reset + +▸ **reset**(): `void` + +Resets the internal state of the filter. + +### setBeta + +▸ **setBeta**(`beta`): `void` + +Sets the Beta parameter + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `beta` | `number` | Parameter to reduce latency (> 0). | + +### setDerivateCutoff + +▸ **setDerivateCutoff**(`dcutoff`): `void` + +Sets the dcutoff parameter + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `dcutoff` | `number` | Used to filter the derivates. 1 Hz by default. Change this parameter if you know what you are doing. | + +### setFrequency + +▸ **setFrequency**(`freq`): `void` + +Sets the frequency of the signal + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `freq` | `number` | An estimate of the frequency in Hz of the signal (> 0), if timestamps are not available. | + +### setMinCutoff + +▸ **setMinCutoff**(`mincutoff`): `void` + +Sets the filter min cutoff frequency + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `mincutoff` | `number` | Min cutoff frequency in Hz (> 0). Lower values allow to remove more jitter. | + +### setParameters + +▸ **setParameters**(`freq`, `mincutoff`, `beta`): `void` + +Sets the parameters of the 1 euro filter. + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `freq` | `number` | An estimate of the frequency in Hz of the signal (> 0), if timestamps are not available. | +| `mincutoff` | `number` | Min cutoff frequency in Hz (> 0). Lower values allow to remove more jitter. | +| `beta` | `number` | Parameter to reduce latency (> 0). | + + ## Related publication [![DOI](https://img.shields.io/badge/doi-10.1145%2F2207676.2208639-blue)](https://doi.org/10.1145/2207676.2208639) diff --git a/typescript/package.json b/typescript/package.json index b52bf45..dc9faa4 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -1,6 +1,6 @@ { "name": "1eurofilter", - "version": "1.1.1", + "version": "1.2.0", "description": "Algorithm to filter noisy signals for high precision and responsiveness.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/typescript/src/OneEuroFilter.ts b/typescript/src/OneEuroFilter.ts index 80ef8f8..dc792b7 100755 --- a/typescript/src/OneEuroFilter.ts +++ b/typescript/src/OneEuroFilter.ts @@ -99,25 +99,71 @@ export class OneEuroFilter { return 1.0 / (1.0 + tau/te); } - public setFrequency(f : number) { - if (f<=0) console.log("freq should be >0") ; - this.freq = f; + + /** + * Sets the frequency of the signal + * + * @param freq An estimate of the frequency in Hz of the signal (> 0), if timestamps are not available. + */ + + public setFrequency(freq : number) { + if (freq<=0) console.log("freq should be >0") ; + this.freq = freq; } - public setMinCutoff(mc : number) { - if (mc<=0) console.log("mincutoff should be >0"); - this.mincutoff = mc; + /** + * Sets the filter min cutoff frequency + * + * @param mincutoff Min cutoff frequency in Hz (> 0). Lower values allow to remove more jitter. + */ + public setMinCutoff(mincutoff : number) { + if (mincutoff<=0) console.log("mincutoff should be >0"); + this.mincutoff = mincutoff; } - public setBeta(b : number) { - this.beta = b; + /** + * Sets the Beta parameter + * + * @param beta Parameter to reduce latency (> 0). + */ + public setBeta(beta : number) { + this.beta = beta; } - public setDerivateCutoff(dc : number) { - if (dc<=0) console.log("dcutoff should be >0") ; - this.dcutoff = dc; + /** + * Sets the dcutoff parameter + * + * @param dcutoff Used to filter the derivates. 1 Hz by default. Change this parameter if you know what you are doing. + */ + public setDerivateCutoff(dcutoff : number) { + if (dcutoff<=0) console.log("dcutoff should be >0") ; + this.dcutoff = dcutoff; + } + +/** + * Sets the parameters of the 1 euro filter. + * + * @param freq - An estimate of the frequency in Hz of the signal (> 0), if timestamps are not available. + * @param mincutoff - Min cutoff frequency in Hz (> 0). Lower values allow to remove more jitter. + * @param beta - Parameter to reduce latency (> 0). + * + */ + public setParameters(freq : number, mincutoff : number, beta : number) { + this.setFrequency(freq); + this.setMinCutoff(mincutoff); + this.setBeta(beta); } +/** + * Constructs a 1 euro filter. + * + * @param freq - An estimate of the frequency in Hz of the signal (> 0), if timestamps are not available. + * @param mincutoff - Min cutoff frequency in Hz (> 0). Lower values allow to remove more jitter. + * @param beta - Parameter to reduce latency (> 0). + * @param dcutoff - Used to filter the derivates. 1 Hz by default. Change this parameter if you know what you are doing. + * + */ + constructor(freq : number, mincutoff : number = 1.0, beta : number = 0.0, dcutoff : number = 1.0) { this.setFrequency(freq) ; this.setMinCutoff(mincutoff) ; @@ -128,12 +174,25 @@ export class OneEuroFilter { this.lasttime = undefined ; } + /** + * Resets the internal state of the filter. + */ + public reset() { this.x.reset(); this.dx.reset(); this.lasttime = undefined; } + /** + * Returns the filtered value. + * + * @param value - Noisy value to filter + * @param timestamp - (optional) timestamp in seconds + * @returns The filtered value + * + */ + public filter(value : number, timestamp : number | undefined) : number { // update the sampling frequency based on timestamps if (this.lasttime!=undefined && timestamp!=undefined)