# Complex Normal Samples In R

**Get Your Data On**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

#### Normal Samples

If we want 10 samples from a Gaussian or normal random process with variance 4 can use rnorm(10,sd=2). Remember the standard deviation (sd) is the square root of the variance.

set.seed(2) x <- rnorm(10,sd=2) x ## [1] -1.7938291 0.3696984 3.1756907 -2.2607513 -0.1605035 0.2648406 ## [7] 1.4159095 -0.4793960 3.9689479 -0.2775740 var(x) ## [1] 3.880803

The var() function produces an estimate of the variance, if we want a better estimate we need more samples.

var(rnorm(1000,sd=2)) ## [1] 4.105966

#### Complex Normal Samples

If we are using base R and want complex normal (CN) samples, we need to write our own function. When the signal processing literature refers to CN they are usually referring to circularly-symmetric CN. Circularly-symmetric means the samples are independent and their mean is 0.

The function produces N CN samples with variance v. The real and imaginary parts are independent, because they are produced by different calls to rnorm(). Let x,y be independent. The var(ax) = a^2var(x) and var(x+y)=var(x)+var(y). So, if we want a variance of 1 would have to start a variance of sqrt(1/2).

makeCN <- function(N,v=1) {(sqrt(v/2))*rnorm(N) + (sqrt(v/2))*1i*rnorm(N)} makeCN(10) ## [1] 0.0023376-0.2079938i 0.7613032+0.6053620i 0.3946671-0.4049715i ## [4] 0.4892950-0.1207824i 0.4651165-0.2871364i -0.2312504+0.9408834i ## [7] -0.2153405-0.9648887i -1.0994866+1.0119199i 1.0396552+0.7824796i ## [10] 0.1147878+0.9059002i

If we want to check the variance, we can't use var() directly.

var(makeCN(10)) ## Warning in var(makeCN(10)): imaginary parts discarded in coercion ## [1] 0.6039204

But the real and imaginary parts are independent, so we can calculate the variance separately.

z <- makeCN(10) var(Re(z)) + var(Im(z)) ## [1] 0.5623849

To make this easier, we can create a function to find the variance.

varComplex <- function(z) var(Re(z)) + var(Im(z))

To get a good estimate we need a-lot of samples.

varComplex(makeCN(1000)) ## [1] 1.016615

Let's set the variance to 2 and then estimate the variance of the samples.

varComplex(makeCN(1000,v=2)) ## [1] 1.925119

Success!

**leave a comment**for the author, please follow the link and comment on their blog:

**Get Your Data On**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.