import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider,Button


#%% Un signal sinusoïdal

def s(t, ampl,f,phi):
    return ampl * np.cos(2 * np.pi * f * t + phi)

t = np.linspace(0, 0.1, 1000)

# Define initial parameters
init_ampl= 5
init_phi = 0
init_f = 100

# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
line, = ax.plot(t, s(t, init_ampl, init_f, init_phi), lw=2)
ax.set_xlabel('$t$ [s]')
ax.set_ylabel('$s$ [V]')


# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.25, bottom=0.25)

# Make a horizontal slider to control the frequency.
axfreq = fig.add_axes([0.25, 0.15, 0.65, 0.03])
freq_slider = Slider(
    ax=axfreq,
    label='$f$ [Hz]',
    valmin=50,
    valmax=150,
    valinit=init_f,
)


# Make a horizontal slider to control the phase.

axphi = fig.add_axes([0.25, 0.1, 0.65, 0.03])
phi_slider = Slider(
    ax=axphi,
    label='$phi$ [rad]',
    valmin=-np.pi,
    valmax=np.pi,
    valinit=init_phi,
)


# Make a vertically oriented slider to control the amplitude
axamp = fig.add_axes([0.1, 0.25, 0.0225, 0.63])
amp_slider = Slider(
    ax=axamp,
    label="Amplitude",
    valmin=0,
    valmax=10,
    valinit=init_ampl,
    orientation="vertical"
)


# The function to be called anytime a slider's value changes
def update(val):
    line.set_ydata(s(t, amp_slider.val, freq_slider.val,phi_slider.val))
    fig.canvas.draw_idle()


# register the update function with each slider
freq_slider.on_changed(update)
amp_slider.on_changed(update)
phi_slider.on_changed(update)


ax.grid()


plt.show()



## Déphasage entre deux signaux sinusoïdaux



def s1(t,ampl,f,phi):
    return ampl * np.cos(2 * np.pi * f * t +phi)

def s2(t,ampl,f,phi):
    return ampl * np.cos(2 * np.pi * f * t +phi)


t = np.linspace(0, 0.05, 1000)

# Define initial parameters
init_ampl= 2
init_phi = 0
init_f = 100

# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
line, = ax.plot(t, s1(t, init_ampl*1.5, init_f,init_phi), lw=2,label='$s_1=S_{1m}\cos(\omega t)$')
line, = ax.plot(t, s2(t, init_ampl, init_f,init_phi), lw=2,label='$s_2=S_{2m}\cos(\omega t+\phi)$')
ax.set_xlabel('$t$ [s]')
ax.set_ylabel('$s$ [V]')

# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.25, bottom=0.25)

# Make a horizontal slider to control the phase.

axphi = fig.add_axes([0.25, 0.1, 0.65, 0.03])
phi_slider = Slider(
    ax=axphi,
    label='$\phi$ [rad]',
    valmin=-np.pi,
    valmax=np.pi,
    valinit=init_phi,
)


# The function to be called anytime a slider's value changes
def update(val):
    line.set_ydata(s2(t, init_ampl, init_f,phi_slider.val))
    fig.canvas.draw_idle()


# register the update function with each slider
freq_slider.on_changed(update)
amp_slider.on_changed(update)
phi_slider.on_changed(update)


ax.grid()

ax.legend()

plt.show()
