Small or embedded systems often require a delicate measurement in the presence of high noise or interference. Without fancy filtering hardware, high-speed processing, or digital signal processing, it can be difficult to extract a stable signal of useful precision and/or stability. This article presents a simple software filter with which inexpensive embedded control systems can dramatically improve the stability of precision measurements.
The following algorithm was originally developed to detect microampere changes in milliampere signals and has since been used in a variety of applications. Traditional measurement methods in simple systems involve multiple-sample averaging or continuous integration. The problem is that the scattering of data points may be too great to provide a repeatable answer, and certain types of interference (most particularly, switching-power-supply interference) will induce error in an integrated answer. Alternatively, true statistical analyses can overwhelm the resources of an embedded processor.
The algorithm assumes the ability to perform a number of sequential measurements (such as might be available from a successive-approximation analog-to-digital converter, or ADC) that would ordinarily be averaged to provide a final answer. A small amount of interleaved processing (while awaiting the next ADC result) and a small amount of post-processing provide an answer that's weighted toward most of the individual answers.
A memory array is allocated to retain each of the individual results for post-processing. The number of samples may be varied according to individual needs and resources. Following the last sample, the arithmetic mean of the samples is calculated. Each sample (array element) is compared to the mean. The direction (> or <) is accumulated, as well as the cumulative magnitude of the deviation in one direction (which, by definition of the mean, equals that of the other direction). The results are applied to the mean according to the following formula:
where A is the corrected answer, M is the arithmetic mean of the results (average), P is the number of samples greater than M, N is the number of samples less than M, TD is the total deviation from mean (absolute value and one direction only), and n is the number of samples.
For example, consider a simple system taking 10 samples and yielding results of 10, 10, 10, 10, 10, 20, 10, 10, 10, and 10. Ignoring the fact that a human will discard the obvious erroneous answer, process the following. The mean is 11. The total number of samples positive is 1, and the total number of samples negative is 9 (so P − N = −8). Total deviation in either direction from mean is 9 (1 times 9 samples low, or 9 times 1 sample high). The deviation factor is −72, which divided by n squared (100) provides a correction of −0.72, for a derived answer of 10.28.
The algorithm works well for random scattering, and reasonably well for spurious interference, such as in the previous example. Note that sinusoidal error will result in P − N of zero, or no deviation from average. An interesting homework assignment for the mathematician in the crowd is to calculate the rejection ratio for various types of interference at different values of n.
Go to www.elecdesign.com for an M68000 assembly-language subroutine sample implementation. It assumes a 12-bit ADC and 32-bit registers.