# -*- coding: utf-8 -*-
"""
Ugo Hincelin - Novembre 2016
Animation pour montrer l'influence des différents paramètres d'un circuit RLC série sur la bande passante.
En Y : intensité normalisée par Imax
En X : pulsation omega
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

eo = 5 # Volt
im = 1 # unité de io

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
omega = np.arange(1e3, 1e5, 1e1)
L0 = 16 # milli Henry
C0 = 0.1 # micro Farad
R0 = 100 # Ohm
Q0 = 1/R0*np.sqrt(L0*1E-3/(C0*1E-6))
print("Q0 =",Q0)
io = im/(np.sqrt(1+Q0**2*(omega/(1/np.sqrt(L0*1E-3*C0*1E-6))-1/np.sqrt(L0*1E-3*C0*1E-6)/omega)**2))
l, = plt.plot(omega, io, lw=2, color='red')
plt.axis([1000, 100000, 0, 1])
plt.xlabel("pulsation")
plt.ylabel("Io/Imax")
ax.text(0.05, 0.95, "Q0 = %.2f" %(Q0),transform=ax.transAxes, fontsize=12, verticalalignment='top')

axcolor = 'lightgoldenrodyellow'
axL = plt.axes([0.25, 0.07, 0.65, 0.03], axisbg=axcolor)
axC = plt.axes([0.25, 0.12, 0.65, 0.03], axisbg=axcolor)
axR = plt.axes([0.25, 0.02, 0.65, 0.03], axisbg=axcolor)

ioL = Slider(axL, 'L (mH)', 10, 100, valinit=L0)
ioC = Slider(axC, 'C (microF)', 0.01, 0.2, valinit=C0)
ioR = Slider(axR,'R (ohm)',10,10000,valinit=R0)

def update(val):
    L = ioL.val*1E-3
    C = ioC.val*1E-6
    R = ioR.val
    omegaO = 1/np.sqrt(L*C)
    Q = 1/R*np.sqrt(L/C)
    l.set_ydata(im/(np.sqrt(1+Q**2*(omega/omegaO-omegaO/omega)**2)))
    fig.canvas.draw_idle()
    ax.set_label("Q = %.2f" %(Q))
    
ioL.on_changed(update)
ioC.on_changed(update)
ioR.on_changed(update)

resetax = plt.axes([0.03, 0.07, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')

def reset(event):
    ioL.reset()
    ioC.reset()
    ioR.reset()
button.on_clicked(reset)

plt.show()