#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
import widgets
import scipy.constants as constants
from matplotlib import rc
#rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
#rc('text', usetex=True)
title = "Effet Doppler et détection synchrone"

#description = """Résolution de l'équation différentielle régissant $x$ en fonction des paramètres du système"""

#===========================================================
# --- Initial parameters  ---------------------
#===========================================================

parameters = {
    'v' : widgets.FloatSlider(value=0, description = 'Vitesse ($cm/s$)', min=0, max=20),
    }
#default parameter for the potential of the couple considered : E0, temperature T, number of exchanged electrons n

c = 340 #Vitesse des ondes dans le milieu (cm/s)
f=40000 #Fréquence en hertz$


#===========================================================
# --- Functions to plot-------------------------------------
#===========================================================

#x est le rapport entre la pulsation et la pulsation de coupure
def Signal_emis(t):
	u = np.sin(2*np.pi*f*t)
	return(u)
	
def Signal_recu(v,t):
	f1=f*(1+v/c)
	u= np.sin(2*np.pi*f1*t)
	return(u)
	
def Signaux_multiplies(v,t):
	return(Signal_emis(t)*Signal_recu(v,t) )


def Signal_filtre(v,t):
	return(np.cos(2*3.1416*t*f*(v/c)))


#===========================================================
# --- Plot of the updated curves ---------------------------
#===========================================================

# This function is called when the sliders are changed 
def plot_data(v):
    lines['Signal émis'].set_data(t1,Signal_emis(t1))
    lines['Signal reçus'].set_data(t1,Signal_recu(v,t1))
    lines['Signaux multipliés'].set_data(t1,Signaux_multiplies(v,t1))
    lines['Signal filtré'].set_data(t1,Signal_filtre(v,t1))
    fig.canvas.draw_idle()


#===========================================================
# --- Initialization of the plot ---------------------------
#===========================================================

fig,axes=plt.subplots(3,1)
ax1 = plt.subplot(3,1,1)
ax2 = plt.subplot(3,1,2)
ax3 = plt.subplot(3,1,3)
#plot of the text
#fig.text(0.01, .9, widgets.justify(description), multialignment='left', verticalalignment='top')

#ax = fig.add_axes([0.15, 0.3, 0.75, 0.6])
#ax.axhline(0, color='k')

lines = {}
lines['Signal émis'], = ax1.plot([], [], lw=2, color='red')
lines['Signal reçus'], = ax1.plot([], [], lw=2, color='blue')
lines['Signaux multipliés'], = ax2.plot([], [], lw=2, color='red')
lines['Signal filtré'], = ax3.plot([], [], lw=2, color='red')



t1= np.linspace(0,1E-3,1000)

ax1.set_xlim(0,100E-6)
ax2.set_xlim(0,1E-3)
ax3.set_xlim(0,1E-3)
ax1.set_ylim(-1, 1)
ax2.set_ylim(-1, 1)
ax3.set_ylim(-1, 1)


ax1.set_xlabel('t(s)')
ax2.set_xlabel('t(s)')
ax3.set_xlabel('t(s)')
ax1.set_ylabel("Signaux de l'émetteur et du récepteur")
ax2.set_ylabel('Amplitude du signal multiplié')
ax3.set_ylabel('Amplitude du signal filtré')


param_widgets = widgets.make_param_widgets(parameters, plot_data, slider_box=[0.35, 0.9, 0.4, 0.05])
#choose_widget = widgets.make_choose_plot(lines, box=[0.015, 0.25, 0.2, 0.15])
#reset_button = widgets.make_reset_button(param_widgets)

if __name__=='__main__':
    plt.show()
