Filter.txt
Warning: if you clip this code into your program, look out for possible html code slipping in.
REM Filter.txt REM REM Author: Steven R. Wheeler REM REM Copyright 1998 Vesta Technology, Inc. REM All rights reserved. REM REM This is a filter function which can be used to add a portion of a new reading REM to an "old" value. It works with integer parameters. A similar but simpler REM filter can be implemented on the SBC2000-074 using floating-point operations. REM The parameters are as follows: REM REM old This is the previous filtered value. REM new This is the value to be added to the filtered value. REM retain This is the portion of the old value which will be retained. REM This can be numerically anywhere from 0 to scale. REM scale This is the scaling value to be used in the filter. It can be REM up to 181. Larger values will cause numeric overflow. REM REM The calculation started with the equation: REM REM (old * retain) + new * (scale - retain) REM filtered_value = --------------------------------------- REM scale REM REM This was simplified to: REM REM (old - new) * retain REM filtered_value = -------------------- + new REM scale REM REM The calculations are done with absolute values for simplicity. A persistent REM remainder value is kept for better accuracy. If more than one signal is to be REM filtered in an application, you will either need to have separate copies of REM the filter function with unique names for each signal, or you will have to REM make "remainder" a global variable, and save and restore the value each time REM you change to filtering a different signal. FUNCTION Filter(old AS INTEGER, new AS INTEGER, retain AS INTEGER, scale AS INTEGER) AS INTEGER STATIC remainder AS INTEGER Filter = abs(old - new) remainder = (Filter \ scale) * retain + remainder Filter = (Filter / scale) * retain Filter = Filter + remainder / scale if (old - new) < 0 Filter = -Filter endif Filter = Filter + new remainder = remainder \ scale END rem This is the main body of the application. rem GLOBAL z AS INTEGER rem z = 0 rem DO WHILE 1 rem z = Filter(z, 31416, 99, 100) rem PRINT "\013\010", z rem LOOP