%lab3
%#1a,c,d
n=[0:10]';
x=zeros(size(n));
index=find(n>=0 & n<5);
x(index)=1;

y=x.*(-1).^n;
w=[0:pi/1024:pi]';
Xspect=freqz(x,1,w);
Yspect=freqz(y,1,w);

magX=abs(Xspect);
phaseX=angle(Xspect);
magY=abs(Yspect);
phaseY=angle(Yspect);

g=conv(y,y)
Gspect=freqz(g,1,w);
magG=abs(Gspect);
phaseG=angle(Gspect);

figure(1)
subplot(3,2,1);
stem(n,x) ; %x(n) input signal
xlabel('n');
ylabel('Amplitude');
title('Problem #1A: x(n)');
grid on

subplot(3,2,2);
stem(n,y) ; %y(n) output signal
xlabel('n');
ylabel('Amplitude');
title('Problem #1B: Y(n)');
grid on

subplot(3,2,3);
plot(w,magX) ; %Magnitude plot of x(n)
xlabel('\omega');
ylabel('Amplitude');
title('Magnitude spectrum of x(n)');
grid on
axis([0,pi,0,8]);

subplot(3,2,4);
plot(w,magY) ; %Magnitude plot of y(n)
xlabel('\omega');
ylabel('Amplitude');
title('Magnitude spectrum of y(n)');
grid on
axis([0,pi,0,8]);

subplot(3,2,5);
plot(w,phaseX) ; %Phase plot of x(n)
xlabel('\omega');
ylabel('Radians');
title('Phase spectrum of x(n)');
grid on
axis([0,pi,-3,2]);

subplot(3,2,6);
plot(w,phaseY) ; %Phase  plot of y(n)
xlabel('\omega');
ylabel('Radians');
title('Phase spectrum of y(n)');
grid on
axis([0,pi,-4,4]);

figure(2)
subplot(2,1,1)
plot(w,magG); %Magnitude plot of G(n)
xlabel('\omega');
ylabel('Amplitude');
title('Magnitude strectrum of g(n)');
grid on
axis([0,pi,-1,26]);

subplot(2,1,2)
plot(w,phaseG); %phase plot of g(n)
xlabel('\omega');
ylabel('Radians');
title('Phase Spectrum of g(n)');
grid on
axis([0,pi,-5,5]);