%% 442.104 Fundamentals of Digital Communications - Problem Classes
% 
% Problem: 5.2
% Term: WS2011
% Intructor: Andreas Pedross <andreas.pedross@tugraz.at>
%
% Signal Processing and Speech Communication Laboratory
% Graz University of Technology
% Inffeldgasse 16c
% 8010 Graz
% Austria

clear all;
close all;
clc;

N = 1E6;                % # of experiments
m = 1.5;                % mean voltage of "Good" batteries 
sigma = 0.1;            % standard deviation of "Good" batteries
P_good = sqrt(2*pi)/10; % probability of "Good" batteries
P_broken = 1-P_good;    % probability of "Broken" batteries

disp('Fundamentals of Digital Communications - Problem Classes')
disp('Problem 5.2');
disp(' ');

%% a)

x = rand(1,N);
B = cell(1,N);
B(x <= P_good) = {'Good'};
B(x > P_good) = {'Broken'};

figure(1);
bar([sum(strcmp(B,'Good'))/N sum(strcmp(B,'Broken'))/N]');
axis([0 3 0 1]);
set(gca,'XTick',[1,2]);
set(gca,'XTickLabel',{'Good';'Broken'},'FontSize',12);
xlabel('Symbols','FontSize',12);
ylabel('Probability','FontSize',12);
grid on;

y = m+sigma*randn(1,N);
v = linspace(-1,2,sqrt(N));
dv = v(2)-v(1);
A = zeros(1,N);
A(strcmp(B,'Good')) = y(strcmp(B,'Good'));
x = hist(A,v);
PDF = x/(N*dv);
CDF = cumsum(PDF).*dv;

figure(2);
subplot(211);
plot(v,PDF,'LineWidth',2);
grid on;
ylabel('PDF','FontSize',12)
xlabel('voltage [V]','FontSize',12);
axis([-1 2 0 1.5]);
subplot(212);
plot(v,CDF,'LineWidth',2);
grid on;
ylabel('CDF','FontSize',12)
xlabel('voltage [V]','FontSize',12);
axis([-1 2 0 1.5]);

% EOF