#!/usr/bin/env python #-*- coding: utf-8 -*- # graphes de fonctions et des transformées de Fourier, utilisant numpy # et matplotlib pour les graphes import numpy as np # directive d'importation standard de numpy from numpy import fft # importation spécifique du module fft de numpy import matplotlib.pyplot as plt #from scipy import fftpack # directive d'importation standard du module équivalent de scipy # https://docs.scipy.org/doc/scipy-0.15.1/reference/api.html#guidelines-for-importing-functions-from-scipy #from pylab import * # directive d'importation alternative en mode "pylab" --> supprimer les plt., fft., def f1(t): f = np.sin(np.pi*2.*t) return f def f2(t): f = np.exp(-t/2.)*np.cos(np.pi*2.*t) return f def f3(t): f = (4./np.pi)*(np.sin(np.pi*2.*t)+np.sin(np.pi*6.*t)/3.+np.sin(np.pi*10.*t)/5.+np.sin(np.pi*14.*t)/7.+np.sin(np.pi*18.*t)/9.) return f # une TF peut se faire via : # fft.fft() du fait de la directive from numpy import fft # , ou np.fft.fft() du fait de import numpy as np # , ou fftpack.fft(y1) si on utilise le module de scipy x = np.arange(0.0,10.0,0.025) y1 = f1(x) z1 = fft.fft(y1) w1 = np.abs(z1[:len(z1)//2]) y2 = f2(x) z2 = fft.fft(y2) w2 = np.abs(z2[:len(z2)//2]) y3 = f3(x) z3 = fft.fft(y3) w3 = np.abs(z3[:len(z3)//2]) # doc subplot : http://matplotlib.org/api/pyplot_api.html?highlight=subplot#matplotlib.pyplot.subplot plt.subplot(3,2,1) # sous-graphes en 3 lignes et 2 colonnes, graphe 1 plt.title('Fonctions') plt.plot(x,y1) plt.xlabel("t/s") plt.ylabel("A(t)") plt.subplot(3,2,2) # sous-graphes en 3 lignes et 2 colonnes, graphe 2 plt.title(u'Transformées de Fourier') plt.plot(w1) plt.xlabel("f/Hz") plt.ylabel("A(f)") plt.subplot(3,2,3) # sous-graphes en 3 lignes et 2 colonnes, graphe 3 plt.plot(x,y2) plt.xlabel("t/s") plt.ylabel("A(t)") plt.subplot(3,2,4) # sous-graphes en 3 lignes et 2 colonnes, graphe 4 plt.plot(w2) plt.xlabel("f/Hz") plt.ylabel("A(f)") plt.subplot(3,2,5) # sous-graphes en 3 lignes et 2 colonnes, graphe 5 plt.plot(x,y3) plt.xlabel("t/s") plt.ylabel("A(t)") plt.subplot(3,2,6) # sous-graphes en 3 lignes et 2 colonnes, graphe 6 plt.plot(w3) plt.xlabel("f/Hz") plt.ylabel("A(f)") plt.savefig('fonctions-fft.png') plt.show()