1
$\begingroup$

I have 6.4 sec recording of a discrete signal on 40Hz frequency (256 values).

How can I compute the variance of the signal? Please, share an algorithm in pseudo-code or in an actual programming language.

$\endgroup$

3 Answers 3

3
$\begingroup$

You want the sample variance:

$$ s^2 = \frac{1}{N-1} \sum_{n=0}^{N-1} (x_n - \mu_x)^2 $$

where the mean, $\mu_x$, is given by

$$ \mu_x = \frac{1}{N} \sum_{n=0}^{N-1} x_n. $$

The $\frac{1}{N-1}$ factor should be used to account for the bias that is otherwise there if $\frac{1}{N}$ is used. One way to think about this is the sample variance is computed from $N-1$ differences between samples, rather than $N$ samples.

$\endgroup$
6
  • $\begingroup$ I am measuring the magnitude of the acceleration vector of triaxial accelerometer. Every value of my set is the magnitude of the [x,y,z] vector. Do you still think, I should be getting the sample variance? $\endgroup$ Commented Sep 4, 2013 at 19:56
  • 1
    $\begingroup$ Yes, it shouldn't matter much how the data is generated. If you're estimating a variance from (effectively) an infinite, or at least large, population, then it's the sample variance you want. $\endgroup$ Commented Sep 4, 2013 at 19:58
  • $\begingroup$ Thank you very much Peter. I will mark your post as an answer as soon as I have the "reputation" to do it. :) $\endgroup$ Commented Sep 4, 2013 at 20:00
  • $\begingroup$ Thanks! Odd. It's your question.. you should be able to mark the answer. $\endgroup$ Commented Sep 4, 2013 at 20:02
  • $\begingroup$ Oops, sorry. I can actually mark it as an answer, but can't upvote it yet. Marked! Thank you! $\endgroup$ Commented Sep 4, 2013 at 20:04
2
$\begingroup$

The Sample Variance $s^2$ of a signal $\mathbf{y}$ with sample average $\mu$ may be computed as: $$ \begin{align*} s^2 &= \frac{1}{N-1} \sum_{i=1}^N (y_i - \mu)^2 \\ &= \frac{1}{N-1} \sum_{i=1}^N \left( \mu^2 - 2 \mu y_i + y_i^2 \right) \\ &= \frac{1}{N-1} \left[ N \mu^2 - 2 \mu \sum_{i=1}^N y_i + \sum_{i=1}^N y_i^2 \right] \\ &= \frac{1}{N-1} \left[ N \left(\frac{1}{N} \sum_{i=1}^N y_i \right)^2 - 2 \left(\frac{1}{N} \sum_{i=1}^N y_i \right) \sum_{i=1}^N y_i + \sum_{i=1}^N y_i^2 \right] &\left( \mu = \frac{1}{N} \sum_{i=1}^N y_i \right) \\ &= \frac{1}{N-1} \left[ N \left(\frac{1}{N} \sum_{i=1}^N y_i \right)^2 - 2 N \left(\frac{1}{N} \sum_{i=1}^N y_i \right)^2 + \sum_{i=1}^N y_i^2 \right] \\ &= \frac{1}{N-1} \left[ -N \left(\frac{1}{N} \sum_{i=1}^N y_i \right)^2 + \sum_{i=1}^N y_i^2 \right] \\ &= \frac{1}{N-1} \left[ -\frac{1}{N} \left(\sum_{i=1}^N y_i \right)^2 + \sum_{i=1}^N y_i^2 \right] \end{align*} $$

Below is an example in C++ code that implements the above equation. It may not be completely optimized, but it should be fairly efficient:

double sample_variance(double *signal, unsigned int signal_length) { double sum = 0.0; double sum2 = 0.0; for (unsigned int i=0; i<signal_length; i++) { sum += signal[i]; sum2 += signal[i]*signal[i]; } double N = (double)signal_length; return (sum2 - sum*sum/N)/(N-1.0); } 
$\endgroup$
7
  • 1
    $\begingroup$ I cooked up this little example, and came back to find Peter K. had already given a great answer. I decided to add this anyway, since the question ask for an algorithm or code. $\endgroup$ Commented Sep 4, 2013 at 20:28
  • $\begingroup$ Are you sure "(sum2 - sum*sum/N)/(N-1.0)" is correct?... Seems like there is a $\sum \mu^2$ missing... $\endgroup$ Commented Sep 4, 2013 at 20:42
  • $\begingroup$ @TheGrapeBeyond: I did a sanity check to make sure that the code was giving me the correct variance. I will edit my answer to add my justification. Let me know if it is wrong. $\endgroup$ Commented Sep 4, 2013 at 20:50
  • 1
    $\begingroup$ The comment wont let me put a big formula in here, so here is it worked out. I think you had it right the first time. $\endgroup$ Commented Sep 4, 2013 at 22:02
  • 1
    $\begingroup$ Its quite clever! Have an upvote for this. ;-) $\endgroup$ Commented Sep 4, 2013 at 22:08
0
$\begingroup$

Here is an article on what is Variance and how to calculate it. If you are using Matlab then you can form a column matrix consisting of the 256 values and send it to the var function in Matlab.

$\endgroup$
2
  • $\begingroup$ Thanks for the link, although this was the first thing I checked out when I started researching. I suppose I need the Population Variance: en.wikipedia.org/wiki/Variance#Population_variance Is this correct? $\endgroup$ Commented Sep 4, 2013 at 19:04
  • 1
    $\begingroup$ No, the population variance assumes that you have measured every single one of the possible points to measure. It sounds like you could sample another 256 points at a later point... so the original 256 points do not constitute the whole population. $\endgroup$ Commented Sep 4, 2013 at 19:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.