% Problem 2.4.1, page 54 - EE-3220 - Dr. Durant - 3/21-23/2015
% P2.4.1: Let x(n) = {2,4,-3,__1__,-5,4,7}. Generate and plot the samples (use the stem function) of the following sequences.
% 1. x1(n) = 2x(n-3) + 3x(n+4) - x(n)
x = [2 4 -3 1 -5 4 7]; % input signal
% The time shifts range from and advance of 4 to a delay of 3, so the output will span 4+3=7 more samples than the input.
x1 = 2*[zeros(1,7) x] ... % 2x(n-3)
+ 3*[x zeros(1,7)] ... % 3x(n+4)
- [zeros(1,4) x zeros(1,3)];
disp(x1)
% For confirmation, calculate error relative to finding the response by convolution.
h = [3 0 0 0 -1 0 0 2];
x1c = conv(x,h);
assert (length(x1) == length(x1c),'Logic error: Directly calculated result''s length doesn''t match length of convolution result')
fprintf('Code error relative to direct conv = %g.\n', norm(x1c - x1))
n0_x1 = -7; % Output begins at n=-7 since input begins at n=-3 and the maximum time advance is by 4 samples (x(n+4) term).
figure, stem(n0_x1+(0:length(x1)-1), x1), title('P2.4.1 - Dr. Durant')