filtering low pass, naive implementation
compared to up-binary-scaled
This commit is contained in:
parent
ff5af0f8e8
commit
f0397a117f
69
embedded_c_book/fo_filter/fo_filter.c
Normal file
69
embedded_c_book/fo_filter/fo_filter.c
Normal file
@ -0,0 +1,69 @@
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* compile with: gcc fo_filter.c -o fo_filter
|
||||
* run as : $ ./fo_filter
|
||||
*
|
||||
* shows how the standard lp first order filter, give a steady signal
|
||||
* truncates it and gives a low result
|
||||
*
|
||||
*/
|
||||
short int threeup_filter, pa2_an2;
|
||||
short int naive_filter;
|
||||
double fp;
|
||||
|
||||
#define INT16 short int
|
||||
INT16 filter16(INT16 var, int depth);
|
||||
#define ADRES 0x1FC
|
||||
|
||||
main()
|
||||
{
|
||||
|
||||
int i;
|
||||
INT16 var,fvar;
|
||||
|
||||
var = ADRES;
|
||||
|
||||
for (i = 0; i < 150; i++) {
|
||||
|
||||
fp = (fp * 7.0 + (double) ADRES) / 8.0;
|
||||
|
||||
naive_filter = (naive_filter * 7 + ADRES) / 8; // old filter
|
||||
|
||||
threeup_filter = (((threeup_filter << 3) - threeup_filter) >> 3) + ADRES;
|
||||
|
||||
fvar = filter16(var,3);
|
||||
|
||||
printf(" iteration: %d naive_filter = %x threeup_filter = %x fvar = %x fp = %lf\n", i, naive_filter, threeup_filter, fvar, fp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define BINARY_FILTER_SCALE 8
|
||||
/* with BINARY_FILTER_SCALE set to 8, depth valid between 2 and 7 */
|
||||
/* with a depth of 2 what we are saying is
|
||||
* * result = (old_result * 3.0 + new_val ) / 4.0)
|
||||
* * with a depth of 3
|
||||
* * result = (old_result * 7.0 + new_val ) / 8.0)
|
||||
* * */
|
||||
INT16 filter16(INT16 var, int depth)
|
||||
{
|
||||
|
||||
long v = var, v2;
|
||||
static long oldv;
|
||||
|
||||
printf(" v = %lx oldv = %lx: ", v, oldv);
|
||||
v <<= BINARY_FILTER_SCALE; // scale up
|
||||
printf(" v = %lx : ", v);
|
||||
v2 = (oldv << depth) - oldv + v;
|
||||
v2 >>= depth;
|
||||
oldv = v2;
|
||||
printf(" v = %lx oldv = %lx ", v, oldv);
|
||||
|
||||
return (v2 >> BINARY_FILTER_SCALE);
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user