Calcul de la transformée de Fourier Discrète
Méthode : Calcul de la Transformée de Fourier Discrète (TFD) en python
Première étape, lecture du fichier son
1
import numpy as np
2
import soundfile as sf
3
import sounddevice as sd
4
from matplotlib import pyplot as plt
5
import wx
6
7
my_app = wx.App()
8
nom_fichier_son = wx.FileSelector("Fichier son",wildcard="*.wav")
9
son , Fe = sf.read(nom_fichier_son)
10
del my_app
deuxième étape, : si le signal est stéréophonique, on ne s'intéresse qu'à la voie 0
1
N = son.shape[0]
2
print("Nombre d'échantillons : ", N)
3
print("Fréquence d'échantillonnage : ", Fe)
4
S = np.fft.fft(son, axis=0)
5
if len(son.shape) == 1:
6
nb_courbe = 1
7
else:
8
nb_courbe = son.shape[1]
9
print("Nombre de voies : ", nb_courbe)
10
Troisième étape, le tracé du signal temporel est dans une fenêtre et le spectre dans une autre fenêtre.
1
# Tracer du signal temporel
2
fig1, ax1 = plt.subplots(nrows=1, ncols=nb_courbe)
3
fig2, ax2 = plt.subplots(nrows=1, ncols=nb_courbe)
4
fig3, ax3 = plt.subplots(nrows=1, ncols=nb_courbe)
5
te = np.arange(0,N)/Fe
6
freq = np.fft.fftfreq(N)*Fe
7
for idx_voie in range(0,nb_courbe):
8
if nb_courbe == 1:
9
graphe1 = ax1
10
graphe2 = ax2
11
graphe3 = ax3
12
y = son
13
Y = S
14
else:
15
graphe1 = ax1[idx_voie]
16
graphe2 = ax2[idx_voie]
17
graphe3 = ax3[idx_voie]
18
y = son[:, idx_voie]
19
Y = S[:, idx_voie]
20
graphe1.plot(te,y,label='Voie ' + str(idx_voie))
21
if idx_voie == 0:
22
graphe1.set(title=nom_fichier_son)
23
graphe1.grid(True)
24
graphe1.set(xlabel='Time (s)', ylabel=' y (u.a.)')
25
graphe1.legend()
26
plt.pause(0.1)
27
# Tracer du module du spectre de la TFD du signal temporel
28
plt.figure(2)
29
graphe2.plot(np.fft.fftshift(freq),
30
np.fft.fftshift(np.abs(Y).real/Fe),
31
marker='.',
32
label='Voie ' + str(idx_voie))
33
if idx_voie == 0:
34
graphe2.set(title='Module de la T.F.D.')
35
graphe2.legend()
36
graphe2.grid(True)
37
graphe2.set(xlabel='Fréquence (Hz)',ylabel='Amplitude (u.a.)')
38
# Tracer de la phase du spectre de la TFD du signal temporel
39
plt.figure(3)
40
graphe3.plot(np.fft.fftshift(freq),
41
np.fft.fftshift(np.angle(Y)),
42
marker='.',
43
label='Voie ' + str(idx_voie))
44
if idx_voie == 0:
45
graphe3.set(title='Phase de la T.F.D.')
46
graphe3.legend()
47
graphe3.grid(True)
48
graphe3.set(xlabel='Fréquence (Hz)',ylabel='Phase (rd)')
49
plt.show()
50