Method
Take the samples for n times in a row (n get the odd), arrange the sample values by the size, and set the intermediate values as the valid values.
Pro:
It can effectively overcome wave interference due to causal factors, and the filtering effect of the measured parameters about the slow changes of temperature, liquid level are good.
Con:
For rapidly changing parameters such as flow, speed, it would not be appropriate.
Test code
int Filter_Value;
void setup () {
Serial.begin (9600); / / initialize the serial communication
randomSeed (analogRead (0)); / / random seed
}
void loop () {
Filter_Value = Filter (); / / get the filter output value
Serial.println (Filter_Value); / / serial output
delay (50);
}
/ / Used to randomly generate a current value of around 300
int Get_AD () {
return random (295, 305);
}
/ / Median filtering method
# define FILTER_N 101
int Filter () {
int filter_buf [FILTER_N];
int i, j;
int filter_temp;
for (i = 0; i <FILTER_N; i + +) {
filter_buf [i] = Get_AD ();
delay (1);
}
/ / Sample values in ascending order (Bubble Act)
for (j = 0; j <FILTER_N - 1; j + +) {
for (i = 0; i <FILTER_N - 1 - j; i + +) {
if (filter_buf [i]> filter_buf [i + 1]) {
filter_temp = filter_buf [i];
filter_buf [i] = filter_buf [i + 1];
filter_buf [i + 1] = filter_temp;
}
}
}
return filter_buf [(FILTER_N - 1) / 2];

Leave a Reply
You must be logged in to post a comment.