%% 442.104 Fundamentals of Digital Communications - Problem Classes
% 
% Problem: 6.1 
% Term: WS2014
% Intructor: Erik Leitinger <erik.leitinger@tugraz.at>
%
% Signal Processing and Speech Communication Laboratory
% Graz University of Technology
% Inffeldgasse 16c
% 8010 Graz
% Austria

clear;
close all;
clc;

Rs = 1;     % sample rate [Hz]
Ts = 1/Rs;  % sample period [s]
N = 1E7;    % # of samples
N0_2 = 1;   % power spectral density of white noise process


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


%% *) ACF & PSD of input noise (remark: we are dealing here with a sampled process!!!)

n = sqrt(N0_2)*randn(N,1);
Rn = xcorr(n,n,500,'unbiased'); % ACF
Sn = fft(Rn);                   % PSD

figure(1);
subplot(211);
plot(-(numel(Rn)-1)/2:+(numel(Rn)-1)/2,Rn,'LineWidth',2);
axis([-300 300 -0.1 1]);
ylabel('R_n','FontSize',12);
xlabel('delay [s]','FontSize',12);
title('Input Noise','FontSize',12);
grid on;
subplot(212);
plot(linspace(0,Rs/2,(numel(Sn)-1)/2),abs(Sn(1:(end-1)/2)),'LineWidth',2);
axis([0 Rs/2 0 N0_2+0.1]);
ylabel('S_n [W/Hz]','FontSize',12);
xlabel('frequency [Hz]','FontSize',12);
grid on;

%% a) ACF & PSD of output noise (remark: we are dealing here with a sampled process!!!)

[b, a] = butter(3,2*pi*0.25,'s');
[bz,az] = impinvar(b,a,Rs);
x = filter(bz,az,n);
Rx = xcorr(x,x,500,'unbiased'); % ACF
Sx = fft(Rx);                   % PSD

figure(2);
subplot(211);
plot(-(numel(Rx)-1)/2:+(numel(Rx)-1)/2,Rx,'LineWidth',2);
axis([-300 300 -0.1 1]);
ylabel('R_x','FontSize',12);
xlabel('delay [s]','FontSize',12);
title('Output Noise','FontSize',12);
grid on;
subplot(212);
plot(linspace(0,Rs/2,(numel(Sx)-1)/2),abs(Sx(1:(end-1)/2)),'LineWidth',2);
axis([0 Rs/2 0 N0_2+0.1]);
ylabel('S_x [W/Hz]','FontSize',12);
xlabel('frequency [Hz]','FontSize',12);
grid on;

% EOF