In embedded programming one sometimes need to check when an analog signal is stable. For example, consider an adjustable pressure regulator where the resulting pressure is read by an Analog- to-Digital Converter (ADC). A user could change the regulator and over time the pressure will reach a new equilibrium.

In the graph above we have a simulation of the pressure set point on the dial changing, and the actual measured pressure changing as a result. Clearly while the dial is moving the pressure reading will be changing. There will also be a period after the dial stops moving the pressure will continue to change until a new equilibrium is reached. How can we say the new equilibrium has been obtained?

Let’s start with a simplified setup by assuming you know the intended value to be obtained. The most basic check would be simply to wait until the ADC reads the precise value desired. Hitting an exact value with an ADC is difficult because there is always noise in the ADC reading. To deal with this a range of values can be used—a center point plus/minus a tolerance band.

This graph shows a band for which to consider the new pressure value having been obtained. Checking for a band works to see a value has been reached, but doesn’t say that value is staying in that band. One option is to require the ADC value stay within this band for some period of time. Another option is similar. When the average of the last *n* samples is within a band. The average essentially builds in time. In either case, time is now a parameter. A signal is stable when it is within some tolerance of nominal for some period of time.

Now what happens if you do not know target value? In our pressure example we might just want to know the target pressure is stable or not, but have no knowledge what the target pressure should be. There are various makeshift options but there is a statistical approach that makes this fairly easy: standard deviation.

In a nutshell, standard deviation is a measure of how much change a set of data has from the mean average. If this set of data is samples taken at regular time intervals, then standard deviation is a direct measurement of the stability over time of that sample set. If a signal is changing over time, the standard deviation will be higher than if it is not, and the faster the change, the higher the standard deviation. So to use standard deviation, one simply needs to pick a standard deviation under which the signal is considered stable. Why this is particularly useful here is that while standard deviation is a measure of deviation from the average, the value of the actual average is irrelevant. Thus, this method works even when the target is unknown.

In the graph above we have added a plot of standard deviation with the scale exaggerated to make the effects easier to see. There are two transition lines, one at the left side denoting the pressure has become unstable, and on the right when the pressure has again stabilized. The green stabilization line represents the standard deviation under which the signal is considered stable.

The standard deviation works just like the windowed average used above. Some number of samples are kept in a rotating buffer from which the average and standard deviation can be computed. When a new sample arrives, the oldest sample in the set is discarded and the new sample added in it’s place. Stability is now just defined as whenever the standard deviation of some number of previous samples is below a threshold.

What’s nice about standard deviation is we can quantify what it means to be stable. For our pressure example, let’s say the pressure can range from 0 to 100 PSI. It might be said that the pressure is stable if the pressure over 1 second deviates less than 1 PSI. By definition, a standard deviation on this 1 second data set of 1 means that between 0% and 68% of the samples were within ±1 PSI of the average (depending on how close to Gaussian the distribution is). One could tighten the tolerance to two standard deviations, which would mean that between 75%- 95% of samples with ±1 PSI. This just requires dividing the target standard deviation in half. In fact, any tolerance band can be defined the number of standard deviations. Two standard deviation is pretty good though since it says that 95% of all data is within the tolerance band.

Above is the equation for stability checking using standard deviation. Standard deviation is typically represented with the Greek lowercase letter sigma, σ. We check this against our desired deviation value represented with Δ. *S*_{devs} is the number of standard deviations to use. *N* is the number of samples in the windowed average, *x* the array of data points and *x* the average of *x*.

One consideration for using standard deviation for stability measurement is to make sure the window size is large enough. If the window is too small and the signal is noisy the standard deviation can jump around too much to be useful.

In the graph above, the window is too small and the noise causes the standard deviation to jump above a fairly conservative stability threshold. The solution is to increase the window size to include more samples. This slows down the response of stability detection and requires more memory. Another option is to filter the noise.

Here is simple low-pass filter using a windowed average is use (showing in cyan), and the stability check run on that.

This system can works for detecting instability as well. However, a gradual signal change will have low standard deviation and thus would not be detected. Of course, that is the difference between stability and change. Stability can allow for gradual change and as long as that change isn’t too great the system can be considered stable. So as with the selection of any algorithm, it is important to understand the needs of your project and to make sure you understand the algorithms you want to use to handle these needs.