import matplotlib
matplotlib.use('nbagg')
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import argrelmax
M = np.loadtxt('ChuteLibre2.TXT')
t = M[:,0]
V = M[:,1]
# Digitisation
V[V>1.0] = 5.0
V[V<1.0] = 0.0
dV = np.diff(V)
ind = argrelmax(dV)
plt.figure()
plt.plot(1000*t, V, 'b-')
plt.plot(1000*t[ind], V[ind], 'ro')
plt.xlabel(r'$t$ [ms]')
plt.show()
t_thres = t[ind]
dt = np.diff(t_thres)
vn = 0.006/dt
p = np.polyfit(t_thres[1:], vn, deg=1)
plt.figure()
plt.plot(1000*t_thres[1:], vn, 'ro')
tt = np.linspace(0.0, 200e-3, 5)
plt.plot(1000*tt, p[0]*tt+p[1])
plt.xlabel(r'$t$ [ms]')
plt.ylabel(r'$v$')
plt.show()
print('g =', p[0], 'm/s^2')
print('t0 =', p[1], 's')