% Example of discrete-time systems in notes on Chapter 2.
% ELEC 320, R. Kozick

n = 0:10;					% Time values
x = [1, 2, 0, 1, -1, 3, 0, 0, 0, 0, 0];		% Input signal
xinit = 0;					% x[-1] = 0
yinit = 0;					% y[-1] = 0

% Systems 1 and 2: Output due to input signal x[n]

y1(1) = 0.5*(x(1) + xinit);
y2(1) = 0.9*yinit + x(1);

for i=2:11
  y1(i) = 0.5 * (x(i) + x(i-1));
  y2(i) = 0.9 * y2(i-1) + x(i);
end

figure(1)
stem(n, x);
title('Input signal')
ylabel('x[n]')
xlabel('n')

figure(2)
subplot(211)
stem(n, y1)
title('System 1')
ylabel('y[n]')
xlabel('n')
subplot(212)
stem(n, y2)
title('System 2')
ylabel('y[n]')
xlabel('n')

% Systems 1 and 2: Impulse response

x = [1, zeros(1,10)];
h1(1) = 0.5*(x(1) + xinit);
h2(1) = 0.9*yinit + x(1);

for i=2:11
  h1(i) = 0.5 * (x(i) + x(i-1));
  h2(i) = 0.9 * h2(i-1) + x(i);
end

figure(3)
subplot(211)
stem(n, h1)
title('System 1 impulse response')
ylabel('h[n]')
xlabel('n')
subplot(212)
stem(n, h2)
title('System 2 impulse response')
ylabel('h[n]')
xlabel('n')
