java - Using a low pass filter to calculate average? -
if want calculate average of 400 data points (noise values accelerometer sensor), can use low pass function such 1 that?
private float lowpass(float alpha, float input, float previousoutput) { return alpha * previousoutput + (1 - alpha) * input; }
i'm comparing storing 400 data points in list<float>
, summing them , dividing 400.
i'm getting quite different results high values alpha
. doing wrong? can use low pass filter calculate average, or better calculate "real" average?
edit
my low pass function took float[]
input , output, since data comes 3-axis accelerometer. changed float
, removed internal for
loop avoid confusion. means input/output passed primitive values, method returns float
instead of operating directly on output array.
if can afford compute arithmetic mean (which doesn't require storage if keep running sum) better option in cases reasons described bellow.
warning: maths ahead
for sake of comparing arithmetic average first-order recursive low-pass filter using, let's start signal of n
samples, each sample has value equal m
plus gaussian noise of variance v
. let's further assume noise independent sample sample.
the computation of arithmetic average on signal give random result mean m
, variance v/n
.
assuming first previousoutput
initialized zero, deriving mean , variance last output (output[n-1]
) of low-pass filter, mean m * (1 - alpha^n)
, variance v * (1-alpha)^2 * (1-alpha^(2*n)) / (1 - alpha^2)
. immediate problem can seen large m
, estimated mean m * (1 - alpha^n)
can quite far true value m
. problem unfortunately gets worse alpha
gets closer 1. occurs because filter not have time ramp it's steady state value.
to avoid issue, 1 may consider initializing first previousoutput
first input sample.
in case mean , variance of last output m
, v * ((1-alpha)^2*(1-alpha^(2*n-2))/(1-alpha^2) + alpha^(2*n-2))
respectively. time problem larger alpha
output variance largely dominated variance of first sample used initialization. particularly obvious in following comparative graph of output variance (normalized input variance):
so, either bias in estimated mean when initializing previousoutput
zero, or large residual variance when initializing first sample (much more arithmetic mean computation).
note in conclusion actual performance may vary specific data, depending on nature of observed variations.
Comments
Post a Comment