% Frequency response of RC circuit
%
% -----------------------------------------------------------------------------------
%
% (C) 2017, Krzysztof Sozanski, Digital Signal Processing in Power Electronics Control 
% Circuits, second edition, Springer-Verlag, London, 2017. 
% 
% -----------------------------------------------------------------------------------
%
% 
clear all; close all; 
l_width = 2; f_size = 12;
C=1; R=1; fcr=1/(2*pi*R*C);
df=0.002; f = (0:df:100-df)*fcr;
n=1:1:50000; frel=0.002*n;
omega=2*pi*f; fi=0;
A=1*sqrt(2); U1=A/(2^0.5)*exp(j*fi);
U2=(U1*1)./(1+j*omega*R*C);
figure('Name','Compass','NumberTitle','off');
 wekt1=[U1(1) U2(20)]; wekt2=[U1(1) U2(500)]; wekt3=[U1(1) U2(1000)]; 
 wekt4=[U1(1) U2(2500)]; 
 subplot(221), compass(wekt1,'r'), set(gca,'FontSize',f_size),
   title('f/f_{cr}=0.2'),
 subplot(222), compass(wekt2,'r'), set(gca,'FontSize',f_size),
   title('f/f_{cr}=1'),
 subplot(223), compass(wekt3,'r'), set(gca,'FontSize',f_size),
   title('f/f_{cr}=2'),
 subplot(224), compass(wekt4,'r'), set(gca,'FontSize',f_size),
   title('f/f_{cr}=5');
   print('compass.pdf','-dpdf');
f_size = 16;
figure('Name','Nyquist','NumberTitle','off');
 plot(U2/U1,'r','linewidth',l_width), set(gca,'FontSize',f_size), 
   xlabel('Re(H(j\omega))'); ylabel('Im(H(j\omega))');
   grid on;
   print('nyquist.pdf','-dpdf');
figure('Name','|H|, phase','NumberTitle','off');
  H_magdB=20*log10(abs(U2/U1));
  phase=angle(U2)*180/pi;
  subplot(211), semilogx(frel, H_magdB,'r'),set(gca,'FontSize',f_size),
    title('(a)'), 
    ylabel('A(\omega) [dB]'); grid on;
  subplot(212), semilogx(frel, phase,'r'),set(gca,'FontSize',f_size),
    title('(b)'), 
    ylabel('\phi(\omega) [deg]'), xlabel('\it f/f_{cr}'); grid on;
    print('Mag_and_phase_log.pdf','-dpdf');
