%% 442.104 Fundamentals of Digital Communications - Problem Classes
% 
% Problem: 5.3
% 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
sigma = 10;     % standard deviation of input signal

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

%% a)

X = sigma*randn(1,N);
Y = round(X);

vx = linspace(-10*sigma,+10*sigma,sqrt(N));
dvx = vx(2)-vx(1);
vy = round(-10*sigma):round(+10*sigma);
dvy = vy(2)-vy(1);
ve = linspace(-1,+1,sqrt(N));
dve = ve(2)-ve(1);

PDFx = hist(X,vx)./(N*dvx);
PMFy = hist(Y,vy)./N;

figure(1);
subplot(211);
plot(vx,PDFx,'LineWidth',2);
ylabel('PDFx','FontSize',12)
xlabel('voltage [V]','FontSize',12);
grid on;
axis([-4*sigma +4*sigma 0 0.05]);
subplot(212);
stem(vy,PMFy,'LineWidth',2);
ylabel('PMFy','FontSize',12)
xlabel('voltage [V]','FontSize',12);
grid on;
axis([-4*sigma +4*sigma 0 0.05]);

figure(2);
plot([-5 +5],[-5 +5],'r','LineWidth',2);
axis([-5 +5 -5 +5]);
hold on;
plot(X,Y,'.');
xlabel('input X [V]','FontSize',12);
ylabel('output Y [V]','FontSize',12);
grid on;
hold off;

E = Y - X;
PDFe = hist(E,ve)./(dve*N);
CDFe = cumsum(PDFe).*dve;

figure(3);
subplot(211);
plot(ve,PDFe,'LineWidth',2);
ylabel('PDFe','FontSize',12)
xlabel('input X [V]','FontSize',12);
grid on;
axis([-1 +1 0 1.1]);
subplot(212);
plot(ve,CDFe,'LineWidth',2);
ylabel('CDFe','FontSize',12)
xlabel('input X [V]','FontSize',12);
grid on;
axis([-1 +1 0 1.1]);

% EOF