% File: E2_158.M

clear;

% Generate a square-wave function, w(t), that will be sampled.
t = 0:0.1: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(211);
plot(t,w);
xlabel('t');
ylabel('w(t)');
title('Square-wave function');

subplot(212)
plot(t,wa);
xlabel('t');
ylabel('w(t)');
title(['Sampling Theorem Waveform with fs = ' int2str(fs) ]);
subplot(111);




