# pour d= 40 cm au max

import pycanum.main as pycan

import matplotlib.pyplot as plt

from matplotlib.pyplot import *

import numpy

import math

import time
import matplotlib.ticker as tck


#On commence par définir la demi-largeur du paquet et la durée totale de l'expérience. L'unité est la période d'oscillation.



sigma = 8

T = 1000.0





#La fonction suivante définit le signal :



def signal(t):

    u=t-T/2

    if abs(u)<sigma:

        return 1*math.cos(2*math.pi*t)

    else:

        return 0.0



#calcul des échantillons et de la période d'échantillonnage :



N = 20000 # nombre d'echantillons

t = numpy.arange(N)*T/N

x = numpy.zeros(N)

for i in range(N):

    x[i] = signal(t[i])

f = ?????

periode = 1.0/f

fe = N/(periode*T)

te = 1.0/fe





can = pycan.Sysam("SP5")

can.config_entrees([0,1],[10.0,0.1])

can.config_echantillon(te*1e6,N)

can.ecrire(1,0.0,1,0.0) # on applique 0 V sur la sortie pendant 1 seconde

time.sleep(1)

can.acquerir_avec_sorties(x,numpy.zeros(N))

t=can.temps()

u=can.entrees()

can.fermer()





numpy.savetxt("paquet-1.txt",[t[0],u[0],u[1]])







[t,x,y] = numpy.loadtxt("paquet-1.txt")

debut = 9900

x=x[debut:]

y=y[debut:]

y=6*y-0.5

T = t[t.size-1]-t[0]

te=t[1]-t[0]

t = 1000*numpy.arange(x.size)*te



figure()

plot(t,x,'k-',label="signal entrée")

plot(t,y,'r-', label="signal de sortie")
legend()

xlabel('t (ms)')
ylabel('U (V)')

axis([-0.1,2,1.2*min(x),1.2*max(x)])
ax = plt.gca()
ax.xaxis.set_minor_locator(tck.MultipleLocator(base=0.05))
ax.yaxis.set_minor_locator(tck.MultipleLocator(base=0.1))
plt.grid(b=True, which='major', color='#000000', linestyle='-')
plt.grid(b=True, which='minor', color='#000000', linestyle='--')

show()