Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function x = generate_nums_ber(n,p,m)
- % GENERATE_BER - generate Ber(p) outcomes m times
- %
- %Create vector of size m, go through loop m times
- x=zeros(1,m); % 1 x m vector of zeros for storing outcomes
- for j=1:m
- successes = 0;
- for i = 1:n
- u = rand;
- if(u>= 1-p)
- successes= successes+1;
- end
- end
- x(j)= successes;
- end
- % alternative to loop above: x=(rand(1,m)>=1-p);
- % plot normalised histogram of outcomes versus theoretical pmf
- close all; % close all previous plots
- alphabet_x=[0,1]; % 1 x 2 vector with alphabet of r.v.
- hx=histc(x,alphabet_x); % compute histogram of outcomes x on alphabet (i.e. how many 0s and 1s)
- stem(alphabet_x,hx/m); % plot normalised histogram on alphabet
- hold on; % keep this figure when plotting next
- px=[1-p,p]; % theoretical pmf of Ber(p)
- stem(alphabet_x,px,'rs'); % plot pmf
- axis([-1 2 0 1]); % widen axis of figure
- legend('empirical','theoretical'); % add legend to figure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement