A faster covariance function for 1 or 2 monodimensional vectors

September 12, 2009 by Luigi Rosa 
Filed under: Signal Processing 
Leave a Comment
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 10.0/10 (1 vote cast)

The zip file includes:

  • readme.txt this file
  • cov_1d.c  the mex file
  • prova.m an example of use

Instructions:

cov_1d compute 1-dimensional covariance

Copy cov_1d.c in matlab current directory
mex compile this file:

mex cov_1d.c

To call this function:

  • cov_1d(a) returns the variance of the vector elements normalizes by (N-1) where N is the number of observations
  • cov_1d(a,0) returns the variance of the vector elements normalizes by (N-1) where N is the number of observations
  • cov_1d(a,1) returns the variance of the vector elements  normalizes by (N) where N is the number of observations
  • cov_1d(a,b) a and b are monodimensional vectors with the same size N. It returns E[(a-aa)(b-ba)] where aa=E[a] and ba=E[b]  E is the mathematical expectation normalizes by (N-1) where N is the number of observations
  • cov_1d(a,b,0) a and b are monodimensional vectors with the same  size N. It returns E[(a-aa)(b-ba)] where aa=E[a] and ba=E[b]  E is the mathematical expectation normalizes by (N-1) where N is the number of observations
  • cov_1d(a,b,1) a and b are monodimensional vectors with the same size N. It returns E[(a-aa)(b-ba)] where aa=E[a] and ba=E[b]  E is the mathematical expectationnormalizes by (N) where N is the number of observations

NOTE: a and b must be REAL vectors (i.e. monodimensional)

From these relationships it follows that:

  • cov_1d(a,a)=cov_1d(a)=cov_1d(a,a,0)
  • cov_1d(a,a,1)=cov_1d(a,1)
  • sqrt(std(a))   is the standard deviation of vector a (normalized to N-1)
  • sqrt(std(a,1)) is the standard deviation of vector a (normalizaed to N)

cov_1d is faster than matlab cov function and can be also used to compute std of 1-dimensional vector.

Example:

a=rand(1,30000000);
tic;v1=cov(a);toc
elapsed_time =
2.10900000000000
tic;v2=cov_1d(a);toc
elapsed_time =
0.75000000000000

Download Now

VN:F [1.8.8_1072]
Rating: 10.0/10 (1 vote cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
A faster covariance function for 1 or 2 monodimensional vectors10.0101

Popularity: 3% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS

Related Posts

  • No Related Post

Comments

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!


Include MATLAB code in your comment by doing the following:

<pre lang="MATLAB">

%insert code here

</pre>