Commit 0a77f7ba authored by LucasDANIELE's avatar LucasDANIELE
Browse files

Optimisation

parent a1502a20
...@@ -7,24 +7,25 @@ Created on Fri Mar 26 09:45:25 2021 ...@@ -7,24 +7,25 @@ Created on Fri Mar 26 09:45:25 2021
#%% #%%
from time import time
from scipy.io import wavfile from scipy.io import wavfile
import numpy as np import numpy as np
import matplotlib.pyplot as plt
import fcts as fcts import fcts as fcts
import importlib import importlib
import frequency_estimator as f0 import frequency_estimator as f0
importlib.reload(fcts) importlib.reload(fcts)
importlib.reload(f0) importlib.reload(f0)
#%%
################################################ ################################################
# Extraction des données # Extraction des données
################################################ ################################################
#%%
start_time = time()
bEnSec = (0*60) bEnSec = (0*60)
duree = 60 duree = 3600
samplerate, dataLR = wavfile.read("input/male.wav")#On lit le fichier qui a des infos oreille gauche, oreille droite samplerate, dataLR = wavfile.read("input/male2.wav")#On lit le fichier qui a des infos oreille gauche, oreille droite
dataL = dataLR[:,0]#On recupère que le son de l'oreille gauche dataL = dataLR[:,0]#On recupère que le son de l'oreille gauche
T = 1/samplerate T = 1/samplerate
b = int(round(bEnSec*samplerate)) b = int(round(bEnSec*samplerate))
...@@ -32,34 +33,40 @@ n = int(round(duree*samplerate)) ...@@ -32,34 +33,40 @@ n = int(round(duree*samplerate))
t = np.linspace(bEnSec, bEnSec+n*T, n) t = np.linspace(bEnSec, bEnSec+n*T, n)
data = dataL[b:n+b]#On recupere n echantillons depuis b soit depuis bEnSec --> Support temporel data = dataL[b:n+b]#On recupere n echantillons depuis b soit depuis bEnSec --> Support temporel
#%%
################################################ ################################################
# Support temporel sans modifs # Support temporel sans modifs
################################################ ################################################
#%%
fcts.aff("Support temporel sans modifs",t,data)
fcts.listen("output/extrait.wav", samplerate, data)
#fcts.aff("Support temporel sans modifs",t,data)
#fcts.listen("output/extrait.wav", samplerate, data)
#%%
################################################ ################################################
# Support temporel signal découpé # Support temporel signal découpé
################################################ ################################################
#%%
dataCut = fcts.decouperExtraits(data)
print(str(len(dataCut))+" cuts")
dataCut = fcts.decouperExtraits(data, 2500, 0.1, samplerate, 20000)
#%%
################################################ ################################################
# Support temporel uniquement cris # Support temporel uniquement cris
################################################ ################################################
#%%
dataCutCris, freqArray = fcts.garderUniquementCris(samplerate,0.5,1.5,250,450,f0.freq_from_HPS, dataCut) dataCutCris, freqArray = fcts.garderUniquementCris(samplerate,0.38,0.6,250,450,f0.freq_from_HPS, dataCut, data)
for i in range(len(dataCutCris)): for i in range(len(dataCutCris)):
#fcts.aff("cri"+str(i), t, np.asarray(dataCutCris[i])) #fcts.aff("cri"+str(i), t, np.asarray(dataCutCris[i]))
fcts.listen("output/cri"+str(i)+".wav", samplerate, np.asarray(dataCutCris[i])) d=dataCutCris[i][0]
f=dataCutCris[i][1]
fcts.listen("output/cri"+str(i)+".wav", samplerate, np.asarray(data[d:f]))
print("enregistrement: " + str(i)) print("enregistrement: " + str(i))
print(np.round(freqArray[i])) print("f0 -> " + str(np.round(freqArray[i])))
print("duration -> " + str((f-d)/samplerate))
print("sum -> " + str(np.sum(abs(data[d:f]))))
print("________________") print("________________")
print(np.mean(freqArray)) print(np.mean(freqArray))
print(np.std(freqArray)) print(np.std(freqArray))
print('Time elapsed: %.3f s\n' % (time() - start_time))
# %% # %%
...@@ -23,41 +23,48 @@ def aff(titre,x,y): ...@@ -23,41 +23,48 @@ def aff(titre,x,y):
plt.title(titre) plt.title(titre)
plt.show() plt.show()
def decouperExtraits(data): def decouperExtraits(data, seuil, dureeSilence, sampleRate, sampleRateChoosen):
DATA=[] DATA=[]
cut=[]
onAir=False onAir=False
step=5000 step=int(np.round(sampleRateChoosen*dureeSilence))
echelle = sampleRate/sampleRateChoosen
dureeExtrait = len(data)/sampleRate
k=0 k=0
seuil=2500 d=0
for i in range(step,len(data)): f=0
if ((abs(data[i])<seuil) & onAir): for i in range(0,int(np.round(dureeExtrait)*sampleRateChoosen-step)-1000):
if ((abs(data[int(np.round(i*echelle))])<seuil) & onAir):
if(k<step): if(k<step):
k+=1 k+=1
cut.append(data[i])
else: else:
k=0 k=0
onAir=False onAir=False
DATA.append(cut) f=int(np.round(i*echelle))
cut=[] DATA.append([d,f])
d=0
f=0
else: else:
if(abs(data[i])>=seuil): if(abs(data[int(np.round(i*echelle))])>=seuil):
if(not(onAir)): if(not(onAir)):
onAir=True onAir=True
d=int(np.round(i*echelle))
k=0 k=0
cut.append(data[i]) if(d>0):
DATA.append(cut) DATA.append([d,len(data)-1])
return DATA return DATA
def garderUniquementCris(sampleRate,tMin, tMax, fMin, fMax, f0Func, dataCut): def garderUniquementCris(sampleRate,tMin, tMax, fMin, fMax, f0Func, dataCut, data):
nSamplesMin = tMin*sampleRate nSamplesMin = tMin*sampleRate
nSamplesMax = tMax*sampleRate nSamplesMax = tMax*sampleRate
res=[] res=[]
freqs=[] freqs=[]
for i in range(len(dataCut)): for i in range(len(dataCut)):
if((len(dataCut[i])>nSamplesMin) & (len(dataCut[i])<nSamplesMax)): d=dataCut[i][0]
freq = np.round(f0Func(np.asarray(dataCut[i]),sampleRate)) f=dataCut[i][1]
if((f-d>nSamplesMin) & (f-d<nSamplesMax)):
freq = np.round(f0Func(np.asarray(data[d:f]),sampleRate))
if((freq>fMin) & (freq<fMax)): if((freq>fMin) & (freq<fMax)):
res.append(dataCut[i]) res.append(dataCut[i])
freqs.append(freq) freqs.append(freq)
......
...@@ -81,9 +81,9 @@ def freq_from_HPS(sig, fs): ...@@ -81,9 +81,9 @@ def freq_from_HPS(sig, fs):
# harmonic product spectrum: # harmonic product spectrum:
c = abs(rfft(windowed)) c = abs(rfft(windowed))
maxharms = 8 maxharms = 6
resarray = [] resarray = []
for x in range(2, 6): for x in range(2, maxharms):
a = copy(c[::x]) # Should average or maximum instead of decimating a = copy(c[::x]) # Should average or maximum instead of decimating
# max(c[::x],c[1::x],c[2::x],...) # max(c[::x],c[1::x],c[2::x],...)
c = c[:len(a)] c = c[:len(a)]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment