#-----------------------------------------------------------------------
# Filtrage d'un signal quelconque
#
# Utilisation d'une barre pour modifier la fonction de transfert
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------

# Bibliothèques utilisées

import numpy as np
import numpy.fft as fft
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import matplotlib.widgets as mwg

# Paramètres modifiables
fc = 1000
ii = complex(0,1)

# Nombre d'harmoniques considérées
max_harm = 200

#fonction de transfert du filtre
def passe_bas(x, f0) :
    return 1/(1+ii*x/f0)

def passe_bas2(x, f0) :
    return 1/(1+1.4*ii*x/f0-(x/f0)**2)

def passe_haut(x, f0) :
    return ii*x/f0/(1+ii*x/f0)


# Bibliothèque de fonctions
@np.vectorize
def triangle(t) :
    return -0.25+t if t<0.5 else 0.75-t

@np.vectorize
def scie(t) :
    return -0.25+t if t<0.75 else t-1.25

@np.vectorize
def creneau(t) :
    return -0.5 + (1.0 if 0.1<=t<0.6 else 0.0)

@np.vectorize
def bruitblanc(t) :
    np.random.seed(int(t*1000000))
    return np.random.random()-0.5

@np.vectorize
def stick(t) :
    return -0.5+5*t if t<0.2 else np.sin((t)/0.15)

# Choix du signal et du filtre
H = passe_bas2
foo = creneau

#-----------------------------------------------------------------------

# Détection utilisation hors Pyzo
if '__iep__' not in globals() :
    matplotlib.interactive(False)

# Calcul du spectre
N = 8192
T = np.linspace(0.0, 1.0, N)
Y = foo(T)
FFT = fft.fft(Y)
f = fft.fftfreq(N, d=1/(N-1))
logf = np.log10(f)

# Filtrage
Yp = H(f, fc)*FFT

# Tracés
Tr = np.linspace(0.0, 2.0, 2*N)
Ampl = 20*np.log10(np.abs(Yp[:max_harm]))
G = np.max(20*np.log10(np.abs(FFT[1:max_harm])))+20*np.log10(np.abs(H(f,fc)))

# Signal
axTmp = plt.axes([0.11, 0.6, 0.78, 0.32])
axTmp.plot(Tr, np.concatenate([Y, Y]), "k--")
partial, = axTmp.plot(Tr, np.concatenate([np.real(fft.ifft(Yp)), np.real(fft.ifft(Yp))]), "b")
axTmp.set_xlim([0, 2])
axTmp.set_ylim([ min(Y) - (max(Y)-min(Y))*0.3, max(Y) + (max(Y)-min(Y))*0.3 ])
axTmp.set_title("signal")
axTmp.set_ylabel("amplitude")
axTmp.set_xlabel("temps (t/T0)")

# Spectre
axTmp = plt.axes([0.11, 0.15, 0.78, 0.32])
plt.grid(True)
gain, = axTmp.plot(f[:max_harm], G[:max_harm], "r")
axTmp.bar(f[:max_harm], 20*np.log10(np.abs(FFT[:max_harm])), color="k")
rects = axTmp.bar(f[:max_harm], Ampl, color="blue")
axTmp.set_xlim([0, f[max_harm]])
#axTmp.set_ylim([0, np.max(Ampl)*1.1])
axTmp.set_ylim([0, np.max(20*np.log10(np.abs(FFT[1:max_harm])))*1.1])
axTmp.set_title("spectre")
axTmp.set_ylabel("amplitude (dB)")
axTmp.set_xlabel("fréquences (f)")

# Slider
axTmp = plt.axes([0.11, 0.035, 0.78, 0.035])
slider = mwg.Slider(axTmp, 'log(f0)', valmin=-3, valmax=3, valinit=2)

def Update(lf0) :
    f0 = 10**lf0
    Yf = FFT * H(f, f0)
    Ga = np.max(20*np.log10(np.abs(FFT[1:max_harm])))+20*np.log10(np.abs(H(f,f0)))
    PartialY = np.real(fft.ifft(Yf))
    partial.set_data(Tr, np.concatenate([PartialY, PartialY]))
    gain.set_ydata(Ga[:max_harm])
    Ampli = 20*np.log10(np.abs(Yf[:max_harm]))
    for j, rect in enumerate(rects) :
        rect.set_height(Ampli[j])

slider.on_changed(Update)

# Update(fc)

# Détection utilisation hors Pyzo

if '__iep__' not in globals() :
    plt.show()
