from scipy.fftpack import fft
import matplotlib.pyplot as plt
import numpy as np

import scipy.signal as sig

pi=np.pi
sin=np.sin
cos=np.cos


def spectre(s,Fe):
    N=len(s)
    S = fft(s)
    S=2/N* np.abs(S[0:N//2])
    S[0]=S[0]/2
    f = np.linspace(0.0, 1/2*Fe, N//2)
    return f,S

### Signal carré

Umoy=???
Um=???
f1=????

N=1000     #nbre de motifs analysés
Tmax=N/f1
NT=50  #nbre de points/période
fe = NT*f1

t = np.arange(start=0.0,stop=Tmax,step=1.0/fe)

# signal carré "parfait"

u = Umoy+Um*sig.square(2*pi*f1*t+pi/2,0.5)


f,S=spectre(u,fe)

###tracé du spectre
plt.figure(0)
plt.plot(f,S,'k')
plt.title('Représentation fréquentielle du signal u carré')
plt.grid()
plt.xlabel('f en Hertz')
plt.ylabel('Tension en volt')


plt.show()

