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
#%%
from time import time
from scipy.io import wavfile
import numpy as np
import matplotlib.pyplot as plt
import fcts as fcts
import importlib
import frequency_estimator as f0
importlib.reload(fcts)
importlib.reload(f0)
#%%
################################################
# Extraction des données
################################################
#%%
start_time = time()
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
T = 1/samplerate
b = int(round(bEnSec*samplerate))
......@@ -32,34 +33,40 @@ n = int(round(duree*samplerate))
t = np.linspace(bEnSec, bEnSec+n*T, n)
data = dataL[b:n+b]#On recupere n echantillons depuis b soit depuis bEnSec --> Support temporel
#%%
################################################
# 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é
################################################
#%%
dataCut = fcts.decouperExtraits(data)
print(str(len(dataCut))+" cuts")
dataCut = fcts.decouperExtraits(data, 2500, 0.1, samplerate, 20000)
#%%
################################################
# 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)):
#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(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(np.mean(freqArray))
print(np.std(freqArray))
print('Time elapsed: %.3f s\n' % (time() - start_time))
# %%
......@@ -23,41 +23,48 @@ def aff(titre,x,y):
plt.title(titre)
plt.show()
def decouperExtraits(data):
def decouperExtraits(data, seuil, dureeSilence, sampleRate, sampleRateChoosen):
DATA=[]
cut=[]
onAir=False
step=5000
step=int(np.round(sampleRateChoosen*dureeSilence))
echelle = sampleRate/sampleRateChoosen
dureeExtrait = len(data)/sampleRate
k=0
seuil=2500
for i in range(step,len(data)):
if ((abs(data[i])<seuil) & onAir):
d=0
f=0
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):
k+=1
cut.append(data[i])
else:
k=0
onAir=False
DATA.append(cut)
cut=[]
f=int(np.round(i*echelle))
DATA.append([d,f])
d=0
f=0
else:
if(abs(data[i])>=seuil):
if(abs(data[int(np.round(i*echelle))])>=seuil):
if(not(onAir)):
onAir=True
d=int(np.round(i*echelle))
k=0
cut.append(data[i])
DATA.append(cut)
if(d>0):
DATA.append([d,len(data)-1])
return DATA
def garderUniquementCris(sampleRate,tMin, tMax, fMin, fMax, f0Func, dataCut):
def garderUniquementCris(sampleRate,tMin, tMax, fMin, fMax, f0Func, dataCut, data):
nSamplesMin = tMin*sampleRate
nSamplesMax = tMax*sampleRate
res=[]
freqs=[]
for i in range(len(dataCut)):
if((len(dataCut[i])>nSamplesMin) & (len(dataCut[i])<nSamplesMax)):
freq = np.round(f0Func(np.asarray(dataCut[i]),sampleRate))
d=dataCut[i][0]
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)):
res.append(dataCut[i])
freqs.append(freq)
......
......@@ -81,9 +81,9 @@ def freq_from_HPS(sig, fs):
# harmonic product spectrum:
c = abs(rfft(windowed))
maxharms = 8
maxharms = 6
resarray = []
for x in range(2, 6):
for x in range(2, maxharms):
a = copy(c[::x]) # Should average or maximum instead of decimating
# max(c[::x],c[1::x],c[2::x],...)
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