function AmpStability(flead)
%Compensate a amplifier to 20dB gain.
if nargin<1
    flead=1e6;
    betaDC=0.1;
end

fmin=1e1;fmax=1e8;
f=10.^(log10(fmin):0.02:log10(fmax));
f1=100;
f2=100e3;
f3=1e6;

f1=1e3;
f2=1e6;
f3=10e6;
Aol=100000.*1./(1+j.*f/f1).*1./(1+j.*f/f2).*1./(1+j.*f/f3); %Open loog gain
AoldB=20.*log10(abs(Aol)); % Open loop gain in dB

figure(2)
clf
hslider = uicontrol('Style','Slider','String','f lead','min',4,'max',8,...
                    'Position',[10 10 100 15],'Callback',{@hslider_Callback});
set(hslider,'Value',log10(flead))
hslider2 = uicontrol('Style','Slider','String','Amp','min',0,'max',5,...
                    'Position',[150 10 100 15],'Callback',{@hslider2_Callback});
set(hslider2,'Value',log10(1/betaDC))

Comp();

    function hslider_Callback(source,eventdata)
        flead=10^get(hslider,'Value');
        Comp();
        %AmpStbility(flead)
    end
    function hslider2_Callback(source,eventdata)
        betaDC=1/10^get(hslider2,'Value');
        Comp();
        %AmpStbility(flead)
    end
 
    function Comp
        % Lead compensation
        beta=betaDC.*(1+j.*f/flead);
        betadB=20*log10(abs(beta));
        AolB=Aol.*beta;
        AolBdB=20.*log10(abs(AolB));
        Acl=Aol./(1+beta.*Aol);    % Closed loop gain without any compensation
        AcldB=20.*log10(abs(Acl));
        
        k=find(abs(135+360/2/pi.*unwrap(angle(AolB)))<=min(abs(135+360/2/pi.*unwrap(angle(AolB))))+0.01);
        fs=f(k(1));
        k2=find(abs(AcldB(1)-AcldB-3)<=min(abs(AcldB(1)-AcldB-3))+0.00001);
        fc=f(k2(1))
        
        figure(1)
        clf
        
        subplot(211)
        semilogx(f,AoldB,'b',f,AolBdB,'g',f,AcldB,'r',f,betadB,'k','LineWidth',2)
        legend('A_o_l','A_o_l\cdot\beta','A_c_l','\beta',3)
        xlabel('f [Hz]')
        ylabel('H(f) [dB]')
        axis([fmin fmax -20*log10(1/betaDC) 101])
        line([fmin fmax],[0 0],'Color','Black')
        line([fs fs],[-AolBdB(k(1))*0.5 AolBdB(k(1))*1.5],'Color','Green','LineWidth',3)
        grid on
        title(['Bode diagram \beta\cdotA_o_l (A_V=',...
               num2str(20*log10(1/betaDC),2),'dB, f_c=',num2str(fc/1e6,2),...
               'MHz, f_{Lead}=',num2str(flead/1e6,2),'MHz)'])
        subplot(212)
        semilogx(f,360/2/pi.*unwrap(angle(Aol)),'b',...
                 f,360/2/pi.*unwrap(angle(AolB)),'g',...
                 f,360/2/pi.*unwrap(angle(Acl)),'r',...
                 f,360/2/pi.*angle(beta),'k','LineWidth',2)
        set(gca,'Ytick',[-270:45:90])
        hold on
        line([fmin fmax],[-135 -135])
        line([fmin fmax],[0 0],'Color','Black')
        line([fs fs],[-140 10],'Color','Green','LineWidth',3)
        %axis([fmin fmax -270 0])
        axis([fmin fmax -180 90])
        legend('A_o_l','A_o_l\cdot\beta','A_c_l','\beta',3)
        xlabel('f [Hz]')
        ylabel('\phi [\circ]')
        grid on
        
        figure(2)
        plot(real(Aol.*betaDC),imag(Aol.*betaDC),'b',...
             real(AolB),imag(AolB),'g',...
             sin(0:0.01:6.3),cos(0:0.01:6.3),'k','LineWidth',2)
        line([-2 2],[0 0],'Color','Black')
        line([0 0],[-2 2],'Color','Black')
        line([0 -2],[0 -2])
        
        axis([-1.4 1.4 -1.1 1.1])
        xlabel('Re')
        ylabel('Im')
        title(['Nyqvist diagram \beta\cdotA_o_l (A_V=',...
               num2str(20*log10(1/betaDC),2),'dB, f_c=',num2str(fc/1e6,2),...
               'MHz, f_{Lead}=',num2str(flead/1e6,2),'MHz)'])
        legend('Resistive feedback','Lead compensation',1)
    end

end




