# Averaging data from 2 sensors

Suppose we take a reading of a single rotating axis using 2 identical sensors. Intuitively, I think that averaging the outputs of both sensors will yield a less noisy result. But what if sensor A has a defect that makes it much noisier than sensor B. Will it still be wise to use the averaged data? Or will it be better to use only the data from sensor B?

If sensor A has a defect, the clear answer is to only use sensor B.

A preferred solution to minimize noise would be to do a weighted average based on the quality of each sensor, when that can be actively characterized. This can be easily done for the case the OP has presented of taking a reading from a single rotating axis.

The optimum combining for the general case of independent signals in the presence of white noise is to do a weighted average of each signal using the signal to noise ratio, which is referred to as maximal ratio combining. In practical application we determine the extent that we can use these assumptions, and our ability to extract an SNR metric depends also on how stationary the signals are. With consideration to this we arrive at a reasonable approach for the OP specifically.

A standard average of the result of two sensors given as x and y would be done as:

$$r_{avg} = \frac{x + y}{2} \tag{1} \label{1}$$

Alternatively, with maximal ratio combining, the two sensors could be combined as follows:

$$r_{opt} = \frac{\sigma_y^2 x + \sigma_x^2 y}{\sigma^2_x + \sigma^2_y } \tag{2} \label{2}$$

Where: $$\sigma^2_x$$, $$\sigma^2_y$$: variance of each sensor's measurement

It is intuitively clear From \ref{2} how this would be superior, since we are now doing a weighted average with favoritism toward the less noisy sensor in the average of the two. [Addressing FAT32's good comment to an earlier version of this post; in addition to the variance estimate $$\sigma^2$$, a moving average estimating the mean $$\mu$$ of each sensor can be tracked for further robustness against erroneous measurements or a completely faulty sensor.]

Notice that when we have no further information as to the quality of each sensor, \ref{2} becomes \ref{1} when we otherwise then set $$\sigma^2_x = \sigma^2_y$$. To the extent that the measurement results are stationary, we are able to easily extract the first and second order statistics (mean and standard deviation as $$\mu$$ and $$\sigma^2$$) directly from the measurements of each sensor to then be able to apply maximal ratio combining. In many cases stationarity can be reasonably assumed over a finite time interval, allowing in the OP's case to easily estimate the mean and standard deviation from each sensor over the maximum time duration allowed by the dynamics of the experiment, bandwidth of sensors etc, and then combining as in \ref{2}.

For a very robust (but detailed) approach, see How to interpret Allan Deviation plot for gyroscope? for further details on using the two-sample variance to dynamically determine the best averaging time for the mean and variance estimates. Thus it can be envisioned where the measurement system is continuously monitoring a moving windowed mean, variance and two-sample variance of the sensors to provide the maximal ratio solution, where the window duration is updated based on minimizing the two sample variance. That level of complexity is likely unnecessary, as it may be sufficient to choose a moving window that is known to be a stationary interval and simply compute the mean and variance over that duration to get the result defined in \ref{2}; but shows an interesting robust approach of what else could be done.

In summary, Maximal Ratio Combining is superior solution over simple averaging. A simpler, suboptimum but effective strategy is to select the sensor with the highest SNR and ignore the other, or doing the simple average and disabling a sensor upon a threshold noise condition (which should be included regardless, such as a stuck sensor or excessive offset condition, to Fat32's points in the comments). Worst case would be to arbitrarily just average both for the very reason the OP brings up when it is entirely feasible that one of the sensors could become defective. A simple average would only make sense when there is high confidence both sensors are without defect and we believe to have similar performance.

This is similar to the strategies used in diversity receivers where two independent channels are optimally combined:

https://en.wikipedia.org/wiki/Diversity_combining

Also to include from the comments the link Fat32 found which refers to this approach as the "Fraser-Potter fixed-interval smoother", from the sensor community while I was exposed to equivalent "Maximal Ratio Combining" from the wireless communications community.

https://en.wikipedia.org/wiki/Sensor_fusion

As a demonstration per my friend Fat's request, below shows the results from a simulation of two sensor readings with a background trend, offset, and slope differential, where the statistics of the signals are not known apriori (everything is extracted from the signals themselves).

The top plot represents the raw measurements from the two sensors, with one sensor significantly noisy. The bottom plot shows the comparative results of simply averaging the reading from each sensor to produce an average result after each update (standard average) and the maximal ratio combining average where each of the two samples being averaged is first weighted by a variance estimate derived from the data from each sensor. The estimators used a moving window to measure the variance of each sensor over the past 500 samples.

An error signal was generated comparing the final results to the avg of the underlying sensor signals without the added noise and is plotted below showing the clear performance improvement in overall noise of the final result.

I'll also include the results of the Allan Deviation (root of the 2-sample variance) showing the significance behind it. I chose to use 500 samples from a review of the ADEV confirming for us in this particular case that statistical computations up to 500 samples will improve the overall estimates, anything beyond that or less than that will be degraded (this includes our estimates of the variance of each sensor for the maximal ratio combining to get each result as well as how long we can average those results over time for the final sensor result). This isn't a necessary step but something I do personally check in any statistical analysis and here demonstrates its clear utility. Basically this is saying that even though there is a background slope, given the noise level itself we can assume stationarity in this case over a 500 sample time interval. This means we would improve our noise estimates by using up to the past 500 samples for each measurement to determine the statistics for the weighting. (And using as much as 2000 samples would provide as good of an answer as 100 samples!). Also most significantly, note the clear difference between the standard average and the maximal ratio combining, in that the latter is better than the best sensor alone!

The horizontal axis for the plot below is in log samples, showing the minimum of the more dominant noise at a 500 sample averaging time.

The 30 second primer in how to read the ADEV plot: The horizontal axis indicates an averaging duration (here in samples) while the vertical axis is an indication of the standard deviation of the mean of a random variable after being averaged over that duration. This relates to our confidence in any estimator that may be used on the underlying data. For example, when determining the average value over $$N$$ samples, if the samples are random white noise and stationary, the standard deviation will go down by the rate of $$1/\sqrt{N}$$ which is exactly what we see in the plot for all curves out to 100 samples (and out to 500 samples for the blue curve of the noisiest signal). After that it bottoms out and turns up due to the underlying drift in the measurement. This tells us for all practical purposes we can assume the noise on the data is white and stationary out to the duration in time where the ADEV plot is still going down by the rate of $$1/\sqrt{N}$$, and that we would minimize the uncertainty (noise) of any estimators that include a mean as part of their computation (rms, variance, etc) by using data over that duration: very simply it tells us the true extent as to how long in time we can average for and keep improving a result; average over a longer duration and results actually get worst! (In this case we could simply remove the linear drift to them take advantage of an even longer averaging time - but the minimum would typically occur due to the signal statistics being no longer stationary due to more random effects that cannot be predictably removed.)

TO DO: The Kalman Filter similarly determines optimum weights for the averaging between the two samples. Show how in this case the Kalman would result in the same weighting solution (it is the optimum combining for this case) and why, and show under what conditions specifically the use of the Kalman would be warranted...

• Hi! That $\mu$ and $\sigma$ ? I'm not sure but cannot see if they're signal or sensor parameters? (mean of sensor measurement implies signal parameter, but if mean of sensor measurement error is used it will be a sensor parameter?). Assume we have an input signal with $\mu = 5$ and $\sigma = 3$ and sensor-A has reading $\mu = 0.5 ~,~ \sigma= 3/10$, and sensor-B has $\mu = 5 ~, ~\sigma = 3$ then Eq.3 reduces to Eq.2 as equally weighted sum (x+y)/2 which is clearly wrong given that sensor-B perfectly matches the signal and sensor-A is quite ignorable, at least shouldn't be weighted equally. Oct 24 '20 at 14:46
• @Fat32 see my rewrite based on your comment --- make more sense to you now? I couldn't really reconcile $\mu$ to be a metric of signal strength since $\mu$ can equally be 0 or 100. But measuring $\mu$ to compare between the two or even for excluding an outlier measurement has utility. Outlier exclusion on a sample by sample basis is best when there is impulse noise conditions, not sure that would apply here, but doesn't hurt to include. Oct 24 '20 at 16:58
• In fact I really have doubts about this maximal ratio combiner technique all together. Because in this question the fundamental problem is about sensor fusion (en.wikipedia.org/wiki/Sensor_fusion) and not about a telecom problem (though they can be related). And my confusion remains exactly the same. May be if you bother to include an example matlab/octave/python application of this method, it will be naturally resolved :-)) Oct 24 '20 at 19:26
• @Fat32 ah nice link! I see the "Fraser-Potter fixed-interval smoother" is what relates the two, and in the telecom maximal ratio combiner it is indeed the variance not the standard deviation as I initially listed. It makes sense they should be related given the fundamental problem is measurement and estimation Oct 24 '20 at 19:34
• @Fat32 Indeed, the real fusion is sensor and telecom technologies! Thanks for getting everything right (variance not standard deviation, meaningless mean, etc). What I find most interesting is the broader application of ADEV; I was introduced to it through my (realtively) newer introduction to working with atomic clocks to assess clock stability (which is non-stationary) and haven't seen it applied elsewhere. Here shows the utility! If I had time I would show what would happen if we averaged for 10,000 samples instead of 300 and how bad that comparative result would be. Oct 24 '20 at 23:22

It's always better to have one more sensor, even if it's a lot noiser or distorted etc., provided that you can sufficiently model its characteristics, and the added circuit complexity is of no concern.

If you cannot mathematically characterise how bad the added sensor is, and instead treat it as if it was good one, then you will degrade your performance, compared to a single better sensor with known characteristics.

In practice, it can be harder to sufficiently and precisely model a low quality sensor than to avoid it.

Here are two papers that deal with generic sensor fusion under unknown statistics: