import cmath as c # required to handle complex
import numpy as n
import pylab as p

# numerical values of the relevant quantities
R = 680; L = 0.5; C = 1.e-7; rint = 30; rG = 50
omega0 = 1/(n.sqrt(L*C))
f0 = omega0/(2*n.pi)

# array of frequencies
f = n.linspace(0.1,3.e3,1000)

# convert freq in angular freq
def freq(ff):
    return ff*2*n.pi

# define the impedance of the rLC parallel
def Zpar(freq):
    return (1/(rint+1j*freq*L)+1j*freq*C)**(-1)
    
# define the total impedance
def Ztot(freq):
    return (rint+rG+R+Zpar(freq))

# define the out impedance
def Zout(freq):
    return (R)

# define the in impedance
def Zin(freq):
    return (Zpar(freq)+R)

# define the transfer function
def T(freq):
    return (Zout(freq)/Zin(freq))

# compute its module
def A(TT):
    return (n.sqrt(TT.real**2+TT.imag**2))

# compute the dephasing
def deltafi(TT):
    return (n.arctan(TT.imag/TT.real)/n.pi)

# plot
p.subplot(2,1,1)
p.rc('font',size=14)
p.plot(f,A(T(freq(f))),color='red')
p.ylabel('$A(f)$', fontsize = 16)
p.grid()
p.minorticks_on()

p.subplot(2,1,2)
p.rc('font',size=14)
p.plot(f,deltafi(T(freq(f))))
p.ylabel('$\Delta \phi$  [$\pi$ rad]',fontsize=16) 
p.xlabel('$f$  [Hz]',fontsize = 16)
p.grid()
p.minorticks_on()

p.savefig('D:/stash/dida14_15/lab2_14_15/complex/fig_antiris.pdf')
p.show()
