% Question (b) in HW2-Ex5
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T = 1;                   % Pulse length
RC = 1/T;                % RC value
ts = 0.01;               % Time step size

th = 0:ts:5*T;            % Long time interval for h(t)
h = (1/RC)*exp(-th/RC);   % h(t) - t is defined as positive, so no u(t) is needed

figure; plot(th, h, 'LineWidth', 2);      % Figure
xlabel('Time (s)');                       % Make it pretty :-)
ylabel('Amplitude');                      % Make it pretty :-)
title('h(t)');                            % Make it pretty :-) 

tx = 0:ts:T;                        % Time interval for x(t)
x = double(rectpuls(tx-T/2, T));    % Unitary pulse at [0,T]
figure; plot(tx,x, 'LineWidth', 2); % Figure
xlabel('Time (s)');                 % Make it pretty :-)
ylabel('Amplitude');                % Make it pretty :-)
title('x(t)');                      % Make it pretty :-)

rec = ts*conv(x, h);              % Convolution
trec = 0:ts:(tx(end)+th(end));    % Convolution results in [0, Tx+Th];
subplot(211); plot(trec, rec, 'LineWidth', 2);    % Figure
xlabel('Time (s)');               % Make it pretty :-)
ylabel('Amplitude');              % Make it pretty :-)
title('Convolution result'); axis([0 5*T 0 max(rec)]);  % Make it pretty :-)
subplot(212); plot(tx, x, 'LineWidth', 2);        % Figure
xlabel('Time (s)');               % Make it pretty :-)
ylabel('Amplitude');              % Make it pretty :-)
title('Pulse x(t)'); axis([0 5*T 0 2]);  % Make it pretty :-)