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

SNR_dB=[-3 0 3 6 9];

for i=1:length(SNR_dB)
    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
        if abs(s_hat-s)>0
for i=1:5

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


,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?

  • $\begingroup$ Because that's how the noise power is adjusted to give the desired SNR. $\endgroup$ – Marcus Müller Apr 21 '19 at 9:40
  • $\begingroup$ @MarcusMüller Is there any formula can prove this? $\endgroup$ – shineele Apr 21 '19 at 9:40
  • $\begingroup$ 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... $\endgroup$ – Marcus Müller Apr 21 '19 at 9:43
  • $\begingroup$ 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)? $\endgroup$ – shineele Apr 21 '19 at 9:48
  • 1
    $\begingroup$ 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. $\endgroup$ – Marcus Müller Apr 21 '19 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$$

| improve this answer | |
  • $\begingroup$ 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? $\endgroup$ – shineele Apr 22 '19 at 2:10
  • $\begingroup$ yes that's exactly what I did ;-) $\endgroup$ – Fat32 Apr 22 '19 at 10:51
  • $\begingroup$ How would you adjust this for a signal represented in complex form? $\endgroup$ – BigBrownBear00 Jul 23 at 12:19
  • $\begingroup$ Discrete-Random Signals and Statistical Signal Processing_THERRIEN gives quite detailed answers to complex random processing. $\endgroup$ – Fat32 Jul 24 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.