%ECOMMS Spring 99
%S. Mandayam, Rowan University
%Adapted from L.W. Couch, Digital and Analog
%Communication Systems, File: E2_158.M

resetx;

close;

% Generate a rectangular pulse function, w(t), that will be sampled.
t = 0:0.01:5;
w = pulse(zeros(length(t),1),t,2,3);

% Generate a square-wave waveform using the Sampling Theorem.  Change
% the value of fs to see the effect of choosing a higher or lower
% sampling frequency.  fs must be a positive integer.

fs = input('Enter the value of fs: ');
fprintf('\n\nCalculating....please wait\n\n');
fs = floor(fs);
if (fs <= 0)
  fs = 1;
end;

n = 1:(5*fs);
ts = n/fs - 1/(2*fs);
a = pulse(zeros(length(ts),1),ts,2,3); 

%wa = Waveform generated using Sampling Theorem

for (i = 1:length(t))
  for (j = 1:length(n))
    temp(j) = pi*fs*(t(i)-j/fs);
  end;
  wa(i) = a'*sa(temp);
end;

subplot(311);
plot(t,w);
axis([0 5 -1 2]);
ylabel('w(t)');
title('Rectangular pulse function');

subplot(312);
stem(ts,a);
axis([0 5 -1 2]);
ylabel('ws(t)');
title(['Sampled Rectangular pulse function with fs = ', int2str(fs), 'Hz' ]);



subplot(313)
plot(t,wa);
xlabel('t in seconds');
ylabel('wr(t)');
title(['Reconstructed Waveform with fs = ' int2str(fs) 'Hz' ]);


