% ELEC310 assignment 3 Question 3 


fs=44100; % sampling freq. in Hz
freqs=[0:fs/2/512:fs/2]; % sample points
%xradian=[0:pi/512:pi];

% Calculate frequency response by z=exp(j*2*pi*freqs/fs)

%(a) H1(z)=(z+1)^2/z^2
H1_w = 1/2 * ( (cos(2*pi*freqs/fs)+1).^2 + (sin(2*pi*freqs/fs)).^2 );
P1_w = atan2( (sin(2*pi*freqs/fs)),(cos(2*pi*freqs/fs)+1) ) - (2*pi*freqs/fs);
%H1_w = 1/2*( (exp(j*2*pi*freqs/fs)+1).^2 / (exp(j*2*pi*freqs/fs)).^2);

% (b) H2(z)=(z-1)/z
H2_w = sqrt( (cos(2*pi*freqs/fs)-1).^2 + (sin(2*pi*freqs/fs)).^2 );
P2_w = atan2( (sin(2*pi*freqs/fs)),(cos(2*pi*freqs/fs)-1) ) - (2*pi*freqs/fs);
%P2_w = angle( (exp(j*2*pi*freqs/fs)-1)./(exp(j*2*pi*freqs/fs))  );
%H2_w = (exp(j*2*pi*freqs/fs)-1).^1 / (exp(j*2*pi*freqs/fs)).^1;

% (c) H3(z)=1/(z-a)
a=0.9;
H3_w = sqrt( (cos(2*pi*freqs/fs)-a).^2 + (sin(2*pi*freqs/fs)).^2 );
H3_w = 1./H3_w;
P3_w = -atan2( (sin(2*pi*freqs/fs)),(cos(2*pi*freqs/fs)-a) );

%freqs=xradian;
figure
subplot(3,1,1)
plot(freqs,H1_w);
title('magnitude responses of the filters');
ylabel('magnitude');
subplot(3,1,2)
plot(freqs,H2_w);
ylabel('magnitude');
subplot(3,1,3)
plot(freqs,H3_w);
xlabel('frequency');
ylabel('magnitude');

figure
subplot(3,1,1)
plot(freqs,P1_w);
title('phase responses of the filters');
ylabel('phase');
subplot(3,1,2)
plot(freqs,P2_w);
ylabel('phase');
subplot(3,1,3)
plot(freqs,P3_w);
xlabel('frequency');
ylabel('phase');


