-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_running_average_filter.ks
More file actions
49 lines (37 loc) · 1.57 KB
/
lib_running_average_filter.ks
File metadata and controls
49 lines (37 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Running Average Filter
//lib_running_average_filter.ks
//This file is distributed under the terms of the MIT license, (c) the KSLib team
//Authored by space_is_hard
//These functions will set up and implement a running
//average filter that will help filter out noise in a
//continuously-updating set of data. It's useful for
//removing noise from a dataset that is expected to remain
//relatively stable. It will lag behind any large or
//continuous changes in the dataset
@LAZYGLOBAL OFF.
//Builds the list that will be passed on to the filter
FUNCTION running_average_filter_init {
PARAMETER
maxLength, //How many previous values should be stored and averaged
initValue. //What starting values to use to build the list of [maxLength] units long
LOCAL inputList TO LIST().
FROM {LOCAL i TO 0.} UNTIL i >= maxLength STEP {SET i TO i + 1.} DO {
inputList:ADD(initValue).
}.
RETURN inputList.
}.
//Takes in a new value and spits out an average of it and the previous values
FUNCTION running_average_filter {
PARAMETER
inputList, //The list we built with PA_filter_init
newValue. //The next value to tack onto the list
inputList:REMOVE(0). //Removes the first item and bumps the rest down
inputList:ADD(newValue). //Adds the new value onto the end
//Adds all of the values in the list together
LOCAL sum TO 0.
FOR item IN inputList {
SET sum TO sum + item.
}.
//Returns the average of all of the values in the list
RETURN sum / inputList:LENGTH.
}.