digital filters


Header: digif.h

#ifndef _DIGIF_
#define _DIGIF_


typedef struct {
    float    y;
    float    minus_c0;
    float    amp_korr;
    float     rez_amp_korr;
    }DigiF;


void digif_init(DigiF *filter,float minus_c0);
int digif_cycle(DigiF *filter,float x);


#endif


Implementation: digif.c

typedef struct {
    float    y;
    float    minus_c0;
    float    amp_korr;
    float     rez_amp_korr;
    }DigiF;


void digif_init(DigiF *filter,float minus_c0)
{
    filter->y=0;
    filter->minus_c0=minus_c0;
    filter->amp_korr=1.-minus_c0;
    filter->rez_amp_korr=1./filter->amp_korr;
}

int digif_cycle(DigiF *filter,float x)
{
    filter->y=((float)x)+(filter->minus_c0*filter->y);
    return (int)(filter->y*filter->amp_korr);
}   


example code (to give you some idea how to use):


#include "digif.h"   
...
void main(void)
{
    DigiF    filter;
    int      value;
    int      filtered_value;

    digif_init(&filter,0.90);                    //initialise filter and set filter
                                                 //coefficient to 0.90

    filter.y=value*filter.rez_amp_korr;          //set current filter-value to value
                                                 //otherwise it will start with 0,
                                                 //do this every time you want set the filter
                                                 //without filtering
    ....
    //you got a new value and want to filter it

    filtered_value=digif_cycle(&filter,value);          
   
    //now do something with filtered_value :-))

    ....

}


another example: new1.gif (111 bytes)

void main(void)
{
    DigiF    filter1;
    DigiF    filter2;
    DigiF    filter3;
    int    value;
    int    filtered_value1;
    int    filtered_value2;
    int    filtered_value3;
    int    i;

    digif_init(&filter1,0.60);     //initialise filter and set filter coefficient
    digif_init(&filter2,0.75);
    digif_init(&filter3,0.90);

    value=100;
    for (i=0;i<30;i++)
    {
        if (i>=15) value=0;
        filtered_value1=digif_cycle(&filter1,value);
        filtered_value2=digif_cycle(&filter2,value);
        filtered_value3=digif_cycle(&filter3,value);
        printf("%d;%d;%d;%d\n",value,filtered_value1,filtered_value2,filtered_value3);
    }
}

gives you following output:

graph.tif (22522 bytes)

 

Download Source:

digif.h
digif.c

Andreas Schrattenecker