#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
CPGE PSI/PSI*
P Hauroigné / R. Dardevet
"""


"""
PARTIE IV : ANALYSE SPECTRALE NUMERIQUE : FFT
"""

# Importation des bibliothèques
import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import *
plt.close()

###############################
# Fonction qui:
# 1 - calcule la FFT
# 2 - trace le signal et sa FFT
numGraphe = 0

def courbeFFT(tabT, tabSignal):
    # tab T: tableau des instants ti
    # tabSignal: tableau des valeurs du signal aux instants ti: s(ti)
    # les deux tableaux doivent avoir la maille taille
    global numGraphe
    numGraphe +=1
    Ne= len(tabT)   # nb d'échantillons
    Te=tabT[1]-tabT[0]  # durée entre 2 échantillons
    # Calcul de l'amplitudes du spectre des Ne échantillons du signal
    spectre = 2*abs(rfft(tabSignal))/Ne
    # Génération des fréquences correspondantes
    listeFreq = rfftfreq(Ne,Te)
    # Création du graphe
    fig=plt.figure(numGraphe,figsize=(15,15))
    plt.gcf().subplots_adjust(hspace = .3)
    # Représentation du signal
    plt.subplot(211)
    plt.title('Signal échantillonné')
    plt.plot(tabT,tabSignal,'.-')
    plt.grid(True)
    plt.xlabel('Temps (en s)')
    # Représentation de la FFT
    plt.subplot(212)
    plt.title('Spectre du signal échantillonné')
    plt.stem(listeFreq,spectre,use_line_collection=True)
    plt.grid(True)
    plt.xlabel('Fréquence de 0 à fe/2 (en Hz)')
    plt.show()
###############################

# Exemple

def signal(t,f):
    return amp*np.sin(2*np.pi*f*t)

amp = 5
Fe = 1000
Te = 1/Fe
Ne = 1000
Ta = Ne*Te
t = np.linspace(0,Ta,Ne,endpoint=False)

s = signal(t,100)

courbeFFT(t,s)



"""
Manipulation Partie IV.2: Premières observations
"""



"""
Manipulation Partie IV.3: Fuites spectrales
"""


"""
Manipulation Partie IV.4: Repliement de spectre - Aliasing
"""


"""
Manipulation Partie IV.5: Résolution spectrale
"""



