Bueno, no se si te servira, pero aqui dejo una rutina que efectua un filtro de peso tipo FIFO...espero te sirva, yo aun no lo utilize.
Codigo:
:=I have a program that measures wheatstone bridge loadcells with a 16 Bit A/D. I would like to average 10 to 20 readings to smooth out the result and have seen several references to median filters in the list. I have spent the last 40 minutes searching for any kind of code snippets to get me coding in the right direction, but to no avail. I have receintly switched from a basic compiler to PCWH and am new to the C language. Does anyone have a code snippet for the median filter or a link on how to implement such a filter. The reading that I am getting from the ADC is stored in a int32 variable called value. Thanks in advance....
In a recent application I needed to have several running averages of a number of pressure transducers. I created a stucture that holds the data in an array, the index into the data array, the running sum, and the average.
// declare the structure type
struct avg_pres
{
long pressure_reading[16];
int index;
long running_sum;
signed long int average;
};
// declare and initialize the 2 pressure structures
struct avg_pres pressure1 ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
struct avg_pres pressure2 ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
Then I use a function to update the data and the average:
void update_running_average (struct avg_pres *sptr, int external_adc_channel)
{
// subtract the oldest reading from the running sum
sptr->running_sum = sptr->running_sum - sptr->pressure_reading[sptr->index];
// read the adc channel for this transducer and store in array
// this is the latest reading
sptr->pressure_reading[sptr->index] = read_ext_adc(external_adc_channel);
// add the latest reading to the running sum
sptr->running_sum = sptr->running_sum + sptr->pressure_reading[sptr->index];
// calculate the average and cast to a signed long int
sptr->average = (signed long int)((sptr->running_sum)/16);
// update the index - points to the next element to update
if(++(sptr->index) > 15)
{
sptr->index = 0;
}
return;
}
The average is a signed long int because it is required in other calculations.
Then in the code I just call the function with the appropriate structure pointer:
update_running_average (&pressure1, PRESSURE1_ADC);
update_running_average (&pressure2, PRESSURE2_ADC);
Hope this helps.
This one is fast and small but not a median filter. It"s more like an RC filter but it"s great for noise rejection.
Current_reading = Read_ADC;
Filtered_reading = (Current_reading + (Filtered_reading * 15)) /16;
Lo pegue aqui porque no me deja subir el archivo...
espero salga bien...