%% 442.104 Fundamentals of Digital Communications - Problem Classes
% 
% Problem: 5.1 
% 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

P_A1 = 0.3;
P_A2 = 0.7;
P_B2A1 = 0.3;
P_B2A2 = 0.2;

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

%% *) Compute Conditional Probability Matrix

P_B3A1 = 0;
P_B1A1 = 1 - (P_B2A1 + P_B3A1);
P_B1A2 = 0;
P_B3A2 = 1 - (P_B1A2 + P_B2A2);

%% a)

x = rand(1,N); 
A = cell(1,N);
A(x <= P_A1) = {'A1'};
A(x > P_A1) = {'A2'};

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

x = rand(1,N);
y = rand(1,N);
B = cell(1,N);
B(strcmp(A,'A1') & x <= P_B1A1) = {'B1'};
B(strcmp(A,'A1') & x > P_B1A1) = {'B2'};
B(strcmp(A,'A2') & y <= P_B2A2) = {'B2'};
B(strcmp(A,'A2') & y > P_B2A2) = {'B3'};

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

% EOF