%
%   2017_04_06 - Christopher Willuweit
%
%   3d-Plot des Betrages der Systemfunktion von Filtern.
%


%close all;
clear all;

% Comment out a single of the following four lines to select filter

%[b,a] = butter(4,0.25);
%[b,a] = cheby1(4,5,0.25);
%[b,a] = cheby2(4,5,0.25);
[b,a] = ellip(4,1,15,0.25);

[Re,Im] = meshgrid(-1.5:0.01:1.5,-1.5:0.01:1.5);

space = Re+i*Im;

% Calculate System Function
magn_H = polyval(b,space)./polyval(a,space);

% Calculate Frequency Response
[h,w] = freqz(b,a);



% Plot Stuff
figure;
subplot(1,2,1);
surf(Re,Im,((abs(magn_H))),'EdgeColor','none')
zlim([-0.2  2.5])
set(gca,'clim',[-0.2 2.5]);
hold on
plot3(cos([-w]),sin([-w]),abs(h),'r','LineWidth',2)    
plot3(cos([w]),sin([w]),abs(h),'r','LineWidth',2)    
scatter3(real(roots(a)),imag(roots(a)),zeros(length(roots(a)),1),'xr','LineWidth',4)
scatter3(real(roots(b)),imag(roots(b)),0.2*ones(length(roots(b)),1),'or','filled','LineWidth',4)

xlabel('Real(z)');
ylabel('Imag(z)');
zlabel('|H(z)|');
rotate3d on;
title('System Function |H(z)|');

subplot(1,2,2);
plot(w./pi,abs(h))
xlabel('\Omega / \pi')
ylabel('|H(e^{j\Omega})|')
grid on
title('Freq. Response |H(e^{j\Omega})|');

if(0)
figure;
surf(A,B,(log10(abs(test1))),'EdgeColor','none')
%zlim([0 2.5])
%colormap(winter)
hold on

plot3(cos([-w]),sin([-w]),[log10(abs(h))],'r','LineWidth',2)    

plot3(cos([w]),sin([w]),log10(abs(h)),'r','LineWidth',2)    
end
