Computing A Signal Variance in Real Time

The standard way to compute the variance of a data set is using the sqaure of the standard deviation. When we have scalar values x1,x2,x3,,xN, the mean μN standard deviation σN for all the N values is computed as follows:

μN=k=1NxkN...(1) σN=k=1N(xkμN)N...(1)

I already described in my previous blog post how to compute the mean in real time using the following algorithm at runtime:

μ0=0; fork=1:1:N μk=(k1k)μk1+xkk; endfor

This saves memory and is more elegent.

Similarly, we can compute the variance of a data set using another recursive algorithm:

μ0=0; S0=0; fork=1:1:N μk=(k1k)μk1+xkk; Sk=Sk1+((xkμk)(xkμk1)); σk=Skk; Vark=σk2=Skk; endfor

Note that, to compute the variance, we also require to compute the mean.

References:

Link