From sweet@charcoal.eg.bucknell.edu Thu Sep 18 14:36 EDT 1997
Date: Thu, 18 Sep 1997 14:36:41 -0400
From: sweet@charcoal.eg.bucknell.edu (Sweet Eric Chandler '99)

%Eric Sweet
%Sept. 18, 1997
%Homework #8

% Sample MATLAB program for Homework 8:  Problem 1.34 in the Kamen/Heck text.
%
% ELEC 320, Fall 1997, Rich Kozick

% NOTE:  MATLAB does not allow an index of zero in vectors.
%        Thus the array index will be one greater than the time index.
%        In other words, the discrete-time signal value y[n] will be
%        stored at array location y[n+1].

% Parts a-d:  y[n] = 0.5*y[n-1] + x[n],  n = 0, 1, ..., 10
%             with initial condition y[-1] = 0.

yinit = 0;
yinite = -1;
n = 0:10;       % Vector of times at which we will evaluate system output.
                % This will be used for plotting.

% Part a: x[n] = u[n], output y1[n]

x = 1;
xb = 2*x;
xc = sin (pi/4)*x;
xd = 2*x + sin (pi/4)*x;
y1(1) = 0.5*yinit + x;  % Do time n=0 separately to handle initial condition.
y2(1) = 0.5*yinit + xb;
y3(1) = 0.5*yinit + xc;
y4(1) = 0.5*yinit + xd;
y5(1) = 0.5*yinite + x;
y6(1) = 0.5*yinite + xb;
y7(1) = 0.5*yinite + xc;
y8(1) = 0.5*yinite + xd;
y9(1) = 0.1*(yinit^2) + x;
y10(1) = 0.1*(yinit^2) + xb;
y11(1) = 0.1*(yinit^2) + xc;
y12(1) = 0.1*(yinit^2) + xd;

for i=2:11      % Compute system outputs for times 1, 2, ..., 10
  x = 1;
  xb = 2*x;
  xc = sin (pi*i/4)*x;
  xd = 2*x + sin (pi*i/4)*x;
  y1(i) = 0.5*y1(i-1) + x;
  y2(i) = 0.5*y2(i-1) + xb;
  y3(i) = 0.5*y3(i-1) + xc;
  y4(i) = 0.5*y4(i-1) + xd;
  y5(i) = 0.5*y5(i-1) + x;
  y6(i) = 0.5*y6(i-1) + xb;
  y7(i) = 0.5*y7(i-1) + xc;
  y8(i) = 0.5*y8(i-1) + xd;
  y9(i) = 0.1*y9(i-1)^2 + x;
  y10(i) = 0.1*y10(i-1)^2 + xb;
  y11(i) = 0.1*y11(i-1)^2 + xc;
  y12(i) = 0.1*y12(i-1)^2 + xd;
end

figure(1)       % Plot output signal y1[n]
stem(n, y1)
title('Problem 1.34 (a)')
xlabel('Time index (n)')
ylabel('y1[n]')

% Part b: x[n] = 2u[n], output y2[n]

figure(2)       % Plot output signal y2[n]
stem(n, y2)
title('Problem 1.34 (b)')
xlabel('Time index (n)')
ylabel('y2[n]')

% Part c: x[n] = sin(pi*n/4)u[n], output y3[n]

figure(3)       % Plot output signal y2[n]
stem(n, y3)
title('Problem 1.34 (c)')
xlabel('Time index (n)')
ylabel('y3[n]')

% Part d: x[n] = 2*x + sin(pi*n/4)u[n], output y4[n]

figure(4)       % Plot output signal y2[n]
stem(n, y4)
title('Problem 1.34 (d)')
xlabel('Time index (n)')
ylabel('y4[n]')

% Part e: parts a to d with new initial conditions

figure(5)       % Plot output signal y1[n-1]
stem(n, y5)
title('Problem 1.34 (e)')
xlabel('Time index (n)')
ylabel('y1[n-1]')

figure(6)       % Plot output signal y2[n-1]
stem(n, y6)
title('Problem 1.34 (e)')
xlabel('Time index (n)')
ylabel('y2[n-1]')

figure(7)       % Plot output signal y3[n-1]
stem(n, y7)
title('Problem 1.34 (e)')
xlabel('Time index (n)')
ylabel('y3[n-1]')

figure(8)       % Plot output signal y4[n-1]
stem(n, y8)
title('Problem 1.34 (e)')
xlabel('Time index (n)')
ylabel('y4[n-1]')

% Part f: parts a to d with system defined by y[n] = (0.8^n)y[n-1] + x[n]

figure(9)       % Plot output signal y1[n]
stem(n, y9)
title('Problem 1.34 (f)')
xlabel('Time index (n)')
ylabel('y1[n]')

figure(10)       % Plot output signal y2[n]
stem(n, y10)
title('Problem 1.34 (f)')
xlabel('Time index (n)')
ylabel('y2[n]')

figure(11)       % Plot output signal y3[n]
stem(n, y11)
title('Problem 1.34 (f)')
xlabel('Time index (n)')
ylabel('y3[n]')

figure(12)       % Plot output signal y4[n]
stem(n, y12)
title('Problem 1.34 (f)')
xlabel('Time index (n)')
ylabel('y4[n]')


