Génération d'un son


In [16]:
from IPython.display import Audio
import numpy as np
max_time = 3
f1 = 220.0
f2 = 224.0
rate = 8000.0
L = 3
times = np.linspace(0,L,rate*L)
signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)

Audio(data=signal, rate=rate)


Out[16]:

Ecoute d'un fichier wave à partir de drive


In [17]:
from google.colab import drive
import os

drive.mount('/content/gdrive')
print(os.listdir('./gdrive/My Drive/'))
print(os.getcwd())
Audio('./gdrive/My Drive/test.wav')


Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).
['Colab Notebooks', 'ecole_yves', 'image', 'test.wav']
/content
Out[17]:

Visualisation du son


In [18]:
import matplotlib.pyplot as plt
from scipy.io import wavfile # get the api
fs, data = wavfile.read('./gdrive/My Drive/test.wav') # load the data
a = data.T[0] # this is a two channel soundtrack, I get the first track
plt.plot(a,'b') 
plt.show()


/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:3: WavFileWarning: Chunk (non-data) not understood, skipping it.
  This is separate from the ipykernel package so we can avoid doing imports until

Visualisation du spectre de fréquence


In [0]:


In [19]:
from scipy.fftpack import fft
b=[(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1)
c = fft(b) # calculate fourier transform (complex numbers list)
d = int(len(c)/2)  # you only need half of the fft list (real signal symmetry)
plt.plot(abs(c[:(d-1)]),'r') 
plt.show()