# Why do we need to write the noise as randn(2,1)./sqrt(2*SNR) instead of randn(1,1)/sqrt(2)?

I saw this code, about the error probability of on/off signal ,on the internet

Ts=10^4;
SNR_dB=[-3 0 3 6 9];
Ne=zeros(1,length(SNR_dB));

for i=1:length(SNR_dB)
SNR=10^(SNR_dB(i)/10);
for t=1:Ts
s=((sign(rand()-0.5))+1)/2; %create the "on"signal or "off" signal ."on"signal=1,"off" signal=0
r=randn(2,1)./sqrt(2.*SNR); %it seems that it is noise
if (s+r)>0.5
s_hat=1;
else
s_hat=0;
end
if abs(s_hat-s)>0
Ne(i)=Ne(i)+1;
end
y=Ne./Ts;
end
end
Pe=zeros(1,5);
for i=1:5
SNR=10^(SNR_dB(i)/10);
Pe(i)=erfc(sqrt(SNR/2)/(2^0.5))/2;
end
semilogy(SNR_dB,y,'-r',SNR_dB,Pe,'-k')


I don't understand why can the noise be written as

r=randn(2,1)./sqrt(2.*SNR)

,why do we need to write as randn(2,1)./sqrt(2*SNR) instead of randn(1,1)/sqrt(2)?Is anyone know about this?

• Because that's how the noise power is adjusted to give the desired SNR. – Marcus Müller Apr 21 at 9:40
• @MarcusMüller Is there any formula can prove this? – shineele Apr 21 at 9:40
• It's exactly the formula in that line of code: Power goes with the square of amplitude, and thus, you divide by the square root of the desired power factor. I honestly don't know what there is to explain about that... – Marcus Müller Apr 21 at 9:43
• i mean,SNR=signal power/noise power.so r=randn(2,1)./sqrt(2.*SNR)=randn(2,1)./sqrt(2.*(signal power/noise power)).why?why doesn't the noise like SNR* randn(2,1)./sqrt(2)? – shineele Apr 21 at 9:48
• I don't understand your criticism: you know that SNR is the ratio of signal power to noise power. Signal power is fixed in your code. You hence have to adjust the noise power. The higher the SNR gets, the lower the noise power hence must be. To scale the power linearly by a factor, you need to scale the amplitude by the square root of the factor. That's all there is! I recommend you don't get defensive towards people trying to help you. – Marcus Müller Apr 21 at 10:18

Not sure if this will help, but let me show how to generate a noisy signal of a desired SNR, assuming additive white Gaussian noise (AWGN) model with fixed signal power.

First the basic definition of SNR (signal to noise ratio) is : $$\text{SNR} = \frac{\sigma_x^2}{\sigma_v^2}$$

where $${\sigma_x^2}$$ is the signal power based on a WSS random signal model, and $${\sigma_v^2}$$ is therefore the noise power (which I take here as white Gaussian noise).

SNR can also be defined in decibells: $$\text{SNR}_{dB} = 10 \log_{10} \left( \frac{\sigma_x^2}{\sigma_v^2} \right) = 10 \log_{10}( \text{SNR})$$

Assuming your signal power $$\sigma_x^2$$ is fixed, then you can adjust the noise power $$\sigma_v^2$$ to achieve a desired SNR with your AWGN signal model. The necessary noise power is found from required SNR as:

$$\sigma_v^2 = \frac{ \sigma_x^2 }{ SNR }$$

Now, Matlab's built in Gaussian function randn() generates standard normal with zero mean and unit variance. Then to get a desired variance (hence noise power) you should scale it with the square root of the target power as shown:

sigx2  = 5.1;          % signal variance (power)
SNR    = 23 ;          % SNR = sigx^2 / sigv^2
sigv2  = sigx2 / SNR;  % noise variance (power)
N      = 1024;         % number of noise samples
v = sqrt( sigv2 ) * randn(N,1); % White Gaussian noise


The reason why you multiply with the square root of the power (or variance) is based on the following property of the variance operator:

$$\text{Var}(X) = \sigma_x^2 \implies \text{Var}(aX) = a^2 \sigma_x^2$$

• because the randn() will produce the unit variance,that is ,$\sigma^2_{n}=1$,and now i know the SNR and the signal power,$\sigma^2_{x}$, is fixed.So actually,in this case,we have to let the $1$ become $\sigma^2_{n}$,and $\sigma^2_{n}= \frac{\sigma^2_{x}}{SNR}$ ,so we have to $1 \times \sqrt{\frac{\sigma^2_{x}}{SNR}}$,that's why we will write sqrt( sigv2 ) * randn(N,1) ,is my thinking right? – shineele Apr 22 at 2:10
• yes that's exactly what I did ;-) – Fat32 Apr 22 at 10:51