% Problem 1.1
%
fid = fopen('exercise11.txt','w');

figure(1); clf
n = -4:2;
x = [1,-2,4,6,-5,8,10];
fprintf(fid, 'exercise11\n');
fprintf(fid, '=====\n');
fprintf(fid, '%10s', 'x =');
fprintf(fid, '%4d', x);
fprintf(fid, '\n');
fprintf(fid, '%10s', 'n =');
fprintf(fid, '%4d', n);
fprintf(fid, '\n\n');

%
% a) 
%
[x11,n11] = sigshift(x,n,-2) 
[x12,n12] = sigshift(x,n,4)
[x1,n1] = sigadd(3*x11,n11,1*x12,n12);
[x2,n2] = sigadd(1*x1,n1,-2*x,n)

fprintf(fid, 'exercise11a\n');
fprintf(fid, '=====\n');
fprintf(fid, '%10s', 'x1(n) =');
fprintf(fid, '%4d', x2);
fprintf(fid, '\n');
fprintf(fid, '%10s', 'n =');
fprintf(fid, '%4d', n2);
fprintf(fid, '\n\n');

subplot(5,1,1); stem(n2,x2); title('Sequence in exercise11a')
xlabel('n'); ylabel('x1(n)'); axis([min(n2)-1,max(n2)+1,min(x2)-1,max(x2)+1])
set(gca,'XTickMode','manual','XTick',[min(n2),0,max(n2)])

%
% b) 
%
[x11,n11] = sigshift(x,n,-5); 
[x12,n12] = sigshift(x,n,-4);
[x1,n1] = sigadd(5*x11,n11,4*x12,n12);
[x2,n2] = sigadd(1*x1,n1,3*x,n)

fprintf(fid, 'exercise11b\n');
fprintf(fid, '=====\n');
fprintf(fid, '%10s', 'x2(n) =');
fprintf(fid, '%4d', x2);
fprintf(fid, '\n');
fprintf(fid, '%10s', 'n =');
fprintf(fid, '%4d', n2);
fprintf(fid, '\n\n');

subplot(5,1,2); stem(n2,x2); title('Sequence in exercise11b')
xlabel('n'); ylabel('x2(n)'); axis([min(n2)-1,max(n2)+1,min(x2)-1,max(x2)+1])
set(gca,'XTickMode','manual','XTick',[min(n2),0,max(n2)])

%
% c) 
%
[x11,n11] = sigshift(x,n,1); 
[x12,n12] = sigshift(x,n,-4);
[x1,n1]   = sigmult(x11,n11,x12,n12);
[x13,n13] = sigfold(x,n);
[x14,n14] = sigshift(x13,n13,-2);
[x3,n3]   = sigmult(x14,n14,x,n);
[x2,n2]   = sigadd(x1,n1,x3,n3);

fprintf(fid, 'exercise11c\n');
fprintf(fid, '=====\n');
fprintf(fid, '%10s', 'x3(n) =');
fprintf(fid, '%4d', x2);
fprintf(fid, '\n');
fprintf(fid, '%10s', 'n =');
fprintf(fid, '%4d', n2);
fprintf(fid, '\n\n');

subplot(5,1,3); stem(n2,x2); title('Sequence in exercise11c')
xlabel('n'); ylabel('x3(n)'); axis([min(n2)-1,max(n2)+1,min(x2)-1,max(x2)+1])
set(gca,'XTickMode','manual','XTick',[min(n2),0,max(n2)])

%
% d) 
%
nc = [-10:10];
xc = cos(0.1*pi*nc);
xe = 2*exp(0.5*nc);

[x1,n1] = sigmult(x,n,xe,nc);
[x11,n11] = sigshift(x,n,-2);
[x12,n12] = sigmult(x11,n11,xc,nc);
[x2,n2] = sigadd(x1,n1,x12,n12);

fprintf(fid, 'exercise11d\n');
fprintf(fid, '=====\n');
fprintf(fid, '%10s', 'x4(n) =');
fprintf(fid, '%4.0f', x2);
fprintf(fid, '\n');
fprintf(fid, '%10s', 'n =');
fprintf(fid, '%4d', n2);
fprintf(fid, '\n\n');


subplot(5,1,4); stem(n2,x2); title('Sequence in exercise11d')
xlabel('n'); ylabel('x4(n)'); axis([min(n2)-1,max(n2)+1,min(x2)-1,max(x2)+1])
set(gca,'XTickMode','manual','XTick',[min(n2),0,max(n2)])

k = +1;
[x1,n1] = sigmult(x,n,xe,nc);

y = zeros(1,length(n));
ny = n;

for i=1:5,
  [x1,n1] = sigshift(x,n,i);
  y1 = y; ny1 = ny;
  [y,ny]  = sigadd(i*x1,n1,y1,ny1);
end

fprintf(fid, 'exercise11e\n');
fprintf(fid, '=====\n');
fprintf(fid, '%10s', 'x5(n) =');
fprintf(fid, '%4.0f', y);
fprintf(fid, '\n');
fprintf(fid, '%10s', 'n =');
fprintf(fid, '%4d', ny);
fprintf(fid, '\n\n');


subplot(5,1,5); stem(ny,y); title('Sequence in exercise11e')
xlabel('n'); ylabel('x5(n)'); axis([min(ny)-1,max(ny)+1,min(y)-1,max(y)+1])
set(gca,'XTickMode','manual','XTick',[min(ny),0,max(ny)])

fclose(fid);
