Assume that the data is read from the 8-bit AD (if it is a higher AD, the data type can be defined as int), and the subroutine is get_ad (); 1. subfilter /* The A value can be adjusted according to the actual situation. Value is a valid value, and new_value is the current sample value. The filter returns valid actual values */ # Define A 10 Char value; Char filter () { Char new_value; New_value = get_ad (); If (new_value-value> A) | (value-new_value>) Return value; Return new_value;
} 2. Central value filtering method /* The N value can be adjusted according to the actual situation. Sort by Bubble Method */ # Define N 11 Char filter () { Char value_buf [N]; Char count, I, j, temp; For (count = 0; count <N; count ++) { Value_buf [count] = get_ad (); Delay (); } For (j = 0; j <N-1; j ++) { For (I = 0; I <N-j; I ++) { If (value_buf [I]> value_buf [I + 1]) { Temp = value_buf [I]; Value_buf [I] = value_buf [I + 1]; Value_buf [I + 1] = temp; } } } Return value_buf [(N-1)/2]; } 3. arithmetic mean Filtering /* */ # Define N 12 Char filter () { Int sum = 0; For (COUNT = 0; count <n; count ++) { Sum + = get_ad (); Delay (); } Return (char) (sum/N ); } 4. Recursive mean filtering (also known as moving average filtering) /* */ # Define N 12 Char value_buf [N]; Char I = 0; Char filter () { Char count; Int sum = 0; Value_buf [I ++] = get_ad (); If (I = N) I = 0; For (count = 0; count <N, count ++) Sum = value_buf [count]; Return (char) (sum/N ); } 5. Median average filtering method (also known as the average filtering method for anti-pulse interference) /* */ # Define N 12 Char filter () { Char count, I, j; Char value_buf [N]; Int sum = 0; For (COUNT = 0; count <n; count ++) { Value_buf [count] = get_ad (); Delay (); } For (j = 0; j <N-1; j ++) { For (I = 0; I <n-J; I ++) { If (value_buf [I]> value_buf [I + 1]) { Temp = value_buf [I]; Value_buf [I] = value_buf [I + 1]; Value_buf [I + 1] = temp; } } } For (COUNT = 1; count <N-1; count ++) Sum + = value [count]; Return (char) (sum/(N-2 )); } 6. amplitude limiting average filtering method /* */ For details, refer to subprograms 1 and 3. 7. First-order lagging Filtering Method /* To accelerate the processing speed of the program, assume that the base number is 100, a = 0 ~ 100 */ # Define a 50 Char value; Char filter () { Char new_value; New_value = get_ad (); Return (100-a) * value + A * new_value; } 8. Weighted recursive average filtering method /* The CoE array is a weighting coefficient table, which exists in the program storage area. */ # Define N 12 Char code COE [N] = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; Char code sum_coe = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12; Char filter () { Char count; Char value_buf [N]; Int sum = 0; For (COUNT = 0, count <n; count ++) { Value_buf [count] = get_ad (); Delay (); } For (COUNT = 0, count <n; count ++) Sum + = value_buf [count] * coe [count]; Return (char) (sum/sum_coe ); } 9. deshake Filtering Method # Define N 12 Char filter () { Char count = 0; Char new_value; New_value = get_ad (); While (value! = New_value ); { Count ++; If (count> = N) return new_value; Delay (); New_value = get_ad (); } Return value; } 10. amplitude limiting and jitter Filtering /* */ For details, refer to subprograms 1 and 9. |