function [x, h, rec] = ISIdemo(sequence, T, id)
% [X, REC, H] = ISIDEMO(SEQUENCE, T, ID) demonstrates the Inter-Symbol Interference 
% (ISI) Problem in communication channels. ISIDEMO returns the transmitted
% signal, the channel signal, and the received signal provided the sequence
% of bits in a character array, and the length of the pulse. A flag is
% provided for ideal filtering.
%
% Input:
% ------
%    sequence - [1xN] : sequence of 1s and 0s as string array (separated by
%                       commas or space)
%    T        - [1x1] : duration of pulses in seconds
%    id       - [1x1] : set to 1 for h(t) = delta(t) (ideal channel)
%                       set to 0 for h(t) = (1/RC)exp(-t/RC)u(t) (RC
%                       circuit)
%
% Output:
% -------
%    x        - [1xL]     : transmitted signal
%    h        - [1xK]     : impulse response of the channel 
%    rec      - [1x(L+K)] : received signal
% 
% Example:  seq = '1 0 0 0 1 1 0'; % Careful, put a space or a comma between
%           T = 2;
%           [x,h,r] = ISIdemo(seq, T, 0);
% 
% @ CSD, UoC, G. P. Kafentzis, 2013
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ts = 0.01;               % Time step size
seq = str2num(sequence); % Decode string array into an array of numbers

if id == 0
    th = 0:ts:10*T;                  % Long time interval for h(t)
    RC = 1/T;                        % RC value
    h = (1/RC)*exp(-th/RC);          % h(t) - t is defined as positive, so no u(t) is needed
elseif id == 1
    th = 0:ts:10*T;                  % Time interval for ideal filter
    h = [1 zeros(1,length(th)-1)];   % Ideal filter h(t) = delta(t)
else
    error('ID should be either 0 or 1'); 
end

L = length(seq);               % How many digits we have?
tx = 0:ts:L*T;                 % Time vector for x(t)
x = zeros(1, length(tx));      % Memory allocation (makes code faster)
for i = 1:L                    % For each bit...
    if seq(i) == 0;            % ... if it is 0, then...
        x = x - rectpuls(tx-(2*i-1)*T/2,T);  % ...place a "-1" pulse
    else
        x = x + rectpuls(tx-(2*i-1)*T/2,T);  % ...otherwise, place a "+1"
    end
end

figure; subplot(211); plot(tx,x, 'LineWidth', 2);    % Figure
xlabel('Time (s)');                                  % Pretty plot :-)
ylabel('Amplitude');                                 % Pretty plot :-)
title('x(t)'); axis([0 L*T -2 2]);                   % Pretty plot :-)

subplot(212); plot(th, h, 'LineWidth', 2);           % Figure
xlabel('Time (s)');                                  % Pretty plot :-)
ylabel('Amplitude');                                 % Pretty plot :-)
title('h(t)'); axis([0 L*T 0 1.5*max(h)]);           % Pretty plot :-)

rec = ts*conv(x, h);                   % Convolution
Ly = tx(end) + th(end);                % Length of convolution
ty = 0:ts:Ly;                          % Convolution results in [0, Tx+Th];
figure; subplot(211); plot(ty, rec, 'LineWidth', 2);   % Figure
xlabel('Time (s)');                    % Pretty plot :-)
ylabel('Amplitude');                   % Pretty plot :-)
title('Convolution result'); axis([0 Ly 1.5*min(rec) 1.5*max(rec)]); % Pretty plot :-)
subplot(212); plot(tx, x, 'LineWidth', 2);  % Figure
xlabel('Time (s)');                         % Pretty plot :-)
ylabel('Amplitude');                        % Pretty plot :-)
title('Input Pulse x(t)'); axis([0 Ly -2 2]); % Pretty plot :-)

