close all
clear all

B=poly([exp(j*pi/4) exp(-j*pi/4)])
A=poly([-0.9 0.95*exp(-j*3*pi/2) 0.95*exp(j*3*pi/2)])


w=0:0.001:pi;

H=freqz(B,A,w);
f2=figure;set(f2,'Color',[1 1 1])
subplot(2,1,1);plot(w,20*log10(abs(H)));y1=ylabel('|H| (dB)')
x1=xlabel('puls. norm. ');
set(y1,'FontSize',12);
set(x1,'FontSize',12);
axis([0 pi -80 50])

subplot(2,1,2);plot(w,angle(H));y1=ylabel('fase di H')
set(y1,'FontSize',12);
x1=xlabel('puls. norm. ');
set(x1,'FontSize',12);
axis([0 pi -pi pi])


zeri=roots(B);
poli=roots(A);

teta=0:0.01:2*pi;
f1=figure;plot(cos(teta),sin(teta),'k-');
set(f1,'Color',[1 1 1])
x1=xlabel('Real');y1=ylabel('Imag')
axis equal
set(x1,'FontSize',16);
set(y1,'FontSize',16);

hold on
l1=line([-1.1 1.1],[ 0 0]);
set(l1,'Color',[ 0 0 0]);
l2=line([ 0 0],[-1.1 1.1]);
set(l2,'Color',[ 0 0 0]);
axis([-1.1 1.1 -1.1 1.1])

z1=plot(real(zeri),imag(zeri),'bo');
set(z1,'MarkerSize',12)
p1=plot(real(poli),imag(poli),'rx');
set(p1,'MarkerSize',12)




w=0:0.001:pi
H=freqz(B,A,w);


for teta=linspace(0,pi,40)

  
    
    figure(f1)
    if teta>0
        delete(l1);
        delete(l2);
        delete(l3);
        delete(l4);
        delete(l5);
        
    end
    
    
    fas=exp(j*teta);
    l1=line([real(zeri(1)) real(fas)],[imag(zeri(1)) imag(fas)]);
    set(l1,'Color',[0 1 0],'LineWidth',2);
    l2=line([real(zeri(2)) real(fas)],[imag(zeri(2)) imag(fas)]);
    set(l2,'Color',[0 1 0],'LineWidth',2);
    
    l3=line([real(poli(1)) real(fas)],[imag(poli(1)) imag(fas)]);
    set(l3,'Color',[1 0 0],'LineWidth',2);
    l4=line([real(poli(2)) real(fas)],[imag(poli(2)) imag(fas)]);
    set(l4,'Color',[1 0 0],'LineWidth',2);
    l5=line([real(poli(3)) real(fas)],[imag(poli(3)) imag(fas)]);
    set(l5,'Color',[1 0 0],'LineWidth',2);
    
    figure(f2)
    hold on
    
    %devo trovare il corrispondente di teta in w
    
    diff=abs(w-teta);
    [minim indice_min]=min(diff);


subplot(2,1,1);hold on;p1=plot(w(1:indice_min),20*log10(abs(H(1:indice_min))));
set(p1,'LineWidth',2,'Color',[1 0 0])
axis([0 pi -80 50])

subplot(2,1,2);hold on;
p1=plot(w(1:indice_min),angle(H(1:indice_min)));
set(p1,'LineWidth',2,'Color',[1 0 0])
axis([0 pi -pi pi])

    
    pause(0.001);
end
    


