%lab3
%#2
clc
clear
b=[1, 1, 1, 1, 1, 1];
a=1;
w=[0:pi/1024:pi]';
grd=grpdelay(b,a,w);

n=[0:10]';
h=zeros(size(n));
ind=find(n>=0 & n<=5);
h(ind)=1;   %h(n)

x=zeros(size(n));
ind=find(n>=0 & n<7);
x(ind)=1;   %x(n)

y=conv(h,x);%y(n)

energy=0;
for i=1:length(n)
    energy=energy+(y(i))^2;
end
energy      %energy of y(n)

Num=[1,1,1,1,1,1];
Hft=freqz(Num,1,w);
magH=abs(Hft);
phaseH=angle(Hft);
phaseH

ind_zero=find(w==0);    %w=0
w_zero=magH(ind_zero)
ind_pi_4=find(w==pi/4); %w=pi/4
w_pi_4=magH(ind_pi_4)
ind_pi_2=find(w==pi/2); %w=pi/2
w_pi_2=magH(ind_pi_2)
ind_pi=find(w==pi);     %w=pi
w_pi=magH(ind_pi)

subplot(3,2,1);
stem(n,h);
xlabel('n');
ylabel('Amplitude');
title('Lab3, #2a - h(n)');
grid on
axis([0,10,0,1.5]);

subplot(3,2,2);
stem(n,x);
xlabel('n');
ylabel('Amplitude');
title('x(n)');
grid on
axis([0,10,0,1.5]);

subplot(3,2,3);
stem(y);
xlabel('n');
ylabel('Amplitude');
title('y(n)');
grid on
axis([0,20,0,8]);

subplot(3,2,4);
plot(w,grd);
xlabel('\omega');
ylabel('Samples');
title('Group Delay of the Filter');
grid on
axis([0,pi,2.5,4.5]);

subplot(3,2,5);
plot(w,magH);
xlabel('\omega');
ylabel('Amplitude');
title('Magnitude Response of Filter');
grid on
axis([0,pi,0,8]);

subplot(3,2,6);
plot(w,phaseH);
xlabel('\omega');
ylabel('Radians');
title('Phase Response of Filter');
grid on
axis([0,pi,-3,3]);
