rmatlabsignalsprobability-theory

Pls convert this R code of bit error probability into matlab


solution of the bit error probability problem

prob=function(E,m)        #--- prob is the estimated error probabity for given values of signal to 
                          #---noise ratio E and sample size m
{
   stopifnot(E>=0 & m>0)  #--- this says that the function won't accept negative values of E and 
                          #---m shoulde be at least 1
   n=rnorm(m)             #--- this says that n is a random sample of size m from N(0,1) 
                          #---distribution
   m=mean(n< -sqrt(E))    #--- this says that m is the proportion of values in n which are less  
                          #---than the negative root of E
   return(m)              #--- this gives us the value of m, which is the estimated error          
                          #---probability
}


E=seq(0,2,by=0.001)
sam=1000
y=sapply(E,prob,m=sam)
p=10*log10(E)

plot(p, log(y),
     main="Graph For The Error Probabilities",
     xlab=expression(10*log[10](E)),
     ylab="log(Error Probability)",
     type="l")

Solution

  • You can try the MATLAB code like below

    clc;
    clear;
    close all;
    
    function y = prob(E,m)
      assert(E>=0 & m>0);
      n = randn(1,m);
      y = mean(n+sqrt(E)<0);
    end
    
    E = 0:0.001:2;
    sam = 1000;
    y = arrayfun(@(x) prob(x,sam),E);
    p = 10*log10(E);
    
    plot(p,log(y));
    title("Graph For The Error Probabilities");
    xlabel('10\log_{10}(E)');
    ylabel("log(Error Probability)");
    

    OUTPUT (MATLAB) enter image description here OUTPUT (R) enter image description here