# how to generate gaussian noise with certain covariance and zero mean ?

329 views (last 30 days)
Tejas Appaji on 25 Sep 2017
Commented: Brendan Nichols on 26 Nov 2020
i have a signal and i want to add gaussian noise to it with zero mean and 0.1 covariance.
variance = 0.1
W = sqrt(variance).*randn(1,size(Xmodt,2)); %Gaussian white noise W
mysignal = mysignal + W; %Add the noise
this code lets me define variance. but i need an algorithm or code to generate gaussian noise with specific covariance and zero mean.

David Ding on 27 Sep 2017
Hi Tejas,
If I understand your question correctly, you wish to generate AWGN with certain co-variance. In this case, you would have a vector of zero-mean Gaussian noises that are statistically dependent. In order to model this in MATLAB, your workflow would be to generate an n x 1 noise vector and then pre-multiply that by the co-variance matrix.
For example:
% Generate a 2 x 1 Gaussian noise vector with covariance
noiseVec = randn(2, 1);
var1 = 0.1;
var2 = 0.2;
covar = 0.05;
cMatrix = [var1, covar; covar, var2];
noiseVec = cMatrix * noiseVec;

Show 1 older comment
JUNHO KWEON on 30 Apr 2019
Hi, thank you for your kind answer. By the way, as I know, randn has variance of 1. In this case, if covar =0, var1 = 4, var2 = 9, why don't we put 'sqrt' such as
% Generate a 2 x 1 Gaussian noise vector with each variance. (orthogonal)
noiseVec = rand(2,1);
var1 = 4;
var2 = 9;
cMatrix = [sqrt(4) 0; 0 sqrt(9)];
noiseVec = cMatrix*noiseVec;
Here, var1 = E[x1*x1.']; var2 = E[x2*x2.']; where E is expectation. x1 and x2 are zero-mean noise with each variance values.
Or did you mean var1 and var2 are standard deviation values?
Shah Mahdi Hasan on 14 Aug 2020
I agree with JUNHO. There should have been sqrt(). Think about the scalar analogy. If you have a standard normal random variable x~N(0,1) and want to have a certain variance sigma, then you would multiply the following:
y ~ N(0,sigma^2) = sigma*x
Brendan Nichols on 26 Nov 2020
I agree with Junho as well. Test out a variation of David's answer:
noiseVec = randn(2, 1e6);
var1 = 0.1;
var2 = 0.2;
covar = 0.05;
cMatrix = [var1, covar; covar, var2];
noiseVec = cMatrix * noiseVec;
cov(noiseVec(1, :), noiseVec(2, :))
Note the covariance is not equal to cMatrix. Try the following instead:
noiseVec = randn(2, 1e6);
var1 = 0.1;
var2 = 0.2;
covar = 0.05;
cMatrix = [var1, covar; covar, var2];
noiseVec = chol(cMatrix, 'lower') * noiseVec;
cov(noiseVec(1, :), noiseVec(2, :))
Note the addition of the Cholesky decomposition to get the covariance of the noise to match.