% Example 5.2
t = -3:6/800:3;
w0 = pi;
n=1:50;
%  first plot the amplitude spectrum for x and for y
cnx = sin(n*pi/2)./n/pi;
cnx = [cnx(length(cnx):-1:1), .5, cnx];
n = -50:50;
clg
subplot(211),stem(n*pi,abs(cnx));
axis([-80 80 0 .5])
xlabel('Frequency (rad/sec)')
ylabel('|cnx|')
RC = input('Input value of RC: ');
H = (1/RC)./(j*n*w0 + 1/RC);
cny = cnx.*H;
subplot(212),stem(n*pi,abs(cny));
xlabel('Frequency (rad/sec)')
ylabel('|cny|')
axis([-80 80 0 .5])
pause
clg
%
% compute y(t)
c0 = .5; H0 = 1; N = 50;
y = c0*H0*ones(1,length(t));
counter = 0;
for n=1:2:N,
  if counter == 0,
     cn = 1/n/pi;          
     counter = 1;
  else
     cn = -1/n/pi;
     counter = 0;
  end
  H = (1/RC)/(j*n*w0 + 1/RC);
  y = y + cn*H*exp(j*n*w0*t) + cn*conj(H)*exp(-j*n*w0*t);  % note cn is real
end
clg
subplot(211),plot(t,y)
title(['Example 5.2 RC = ' num2str(RC)])
xlabel('Time (sec)')
ylabel('y(t)')
axis([-3 3 -.5 1.5])
subplot(111)
