def extractFeatures(self, tokken): import sinatraFilter as sF; import numpy as np; filterBox = sF.sinatraFiltersBox(); wS = int(len(tokken) / 10); featureVector = list(); featureBox = {}; featureBox['mean'] = np.mean(tokken); featureBox['std'] = np.std(tokken); featureBox['len'] = len(tokken) featureBox['meanPositive'] = np.mean(tokken[tokken > 0]); featureBox['meanNegative'] = np.mean(tokken[tokken < 0]); x, featureBox['XmaxFilter'] = filterBox.filterMaxInWindow(tokken, wS); x, featureBox['XsoftMaxFilter'] = filterBox.softenedMaxWindow(tokken,wS); x, featureBox['XmeanLogAvWindow'] = filterBox.sumAbsWindow(tokken,wS); for fkeys, feature in sorted(featureBox.items()): if fkeys[0]=='X': for intIter in feature: featureVector.append(intIter); else: featureVector.append(feature); return featureVector;
def segmentate(self, aD): # # segmentate allows the Front End to find which are the pieces of the # speech. # 1 - Clean the sound. We guess that high frequency # sounds are not important for the accent. So, # we need to perform a fourier transform, remove # the high frequency terms, and come back to period through # an inverse fourier transform. # 2 - Perform the derivatives of the amplitude. This is not a # a trivial problem, since the spectrum is not regular, and # differents minimums and maximums can be found. An approach # could be to split the sound in intervals and perform the # derivatives only with the maximum values of each interval. # # # coeffCleaning : treshold under which fourier transformed frequencies # are removed # wS : window-size import sinatraIO; import sinatraFilter; import numpy as np; filterBox = sinatraFilter.sinatraFiltersBox(); coeffCleaning = 1.5; wS = 800; rowL = 10000; print("reading {0}".format(aD.getName())); self.normalize(aD); aD = aD.getAudio(); print("\tremoving high frequencies"); splitNumber = int(len(aD)/wS); aDTransformed = np.fft.fft(aD); meanADTransformed = np.mean(np.absolute(aDTransformed)); aDTransformed[aDTransformed < coeffCleaning*meanADTransformed] = 0; aDTransformed[20000:] = 0; aDClean = np.fft.ifft(aDTransformed); aDClean = np.real(aDClean); #aDClean = aD; print("\tfiltering"); #y,z,n = filterBox.entropyInWindow(aDClean, 750); #aDClean = aDClean*(n>=1); aCY = np.zeros(splitNumber); aCX = np.zeros(splitNumber); fD = np.zeros(splitNumber-2); sD = np.zeros(splitNumber-2); aCX, aCY = filterBox.filterMaxInWindow(aDClean, wS); aCY = aCY ** 2; print("\tprocessing first and second order derivatives"); for derIter in range(1, splitNumber-2): fD[derIter] = (aCY[derIter+1]-aCY[derIter-1])/2; sD[derIter] = (1/4)*(aCY[derIter + 1] + aCY[derIter - 1] - 2*aCY[derIter]); print("\tsegmentating"); statusMin = 0; statusMax = 0; cutPoints = [0, 0]; rowControl = 1; matrixX = np.zeros(35); testArray = np.zeros(2); for sIter in range(2, splitNumber-3): if (fD[sIter]*fD[sIter+1]) <= 0: if (sD[sIter]+sD[sIter + 1]) > 0: cutPoints[statusMin]=aCX[sIter]; statusMin = statusMin + 1; else: statusMax = 1; if (statusMin == 2) and (statusMax == 1): statusMin = 1; statusMax = 0; rowX = np.zeros(rowL); tokkenL = int(cutPoints[1]-cutPoints[0] + 1); if (tokkenL > 500) and (tokkenL < rowL): rowX[1:tokkenL] = aDClean[int(cutPoints[0]):int(cutPoints[1])]; rowX = self.extractFeatures(rowX); tempTestArray = testArray; rowControl = rowControl + 1; testArray = np.zeros((rowControl, 2)); testArray[0:(rowControl-1),:] = tempTestArray; testArray[rowControl-1, 0] = cutPoints[0]; testArray[rowControl-1, 1] = cutPoints[1]; tempMatrixX = matrixX; matrixX = np.zeros((rowControl, 35)) matrixX[0:(rowControl - 1), :] = tempMatrixX; matrixX[(rowControl - 1), :] = rowX; cutPoints[0] = cutPoints[1]; print("task completed"); matrixX = matrixX[1:,:]; testArray = testArray[1:,:]; return matrixX, testArray, (rowControl - 1);
#!/home/charizard/anaconda3/bin/ipython3 # Bruno Cuevas Zuviria # Sinatra. Exersice 6. # # Modules # import sys import numpy as np import sinatraFrontEnd as sFE import sinatraFilter as sF import sinatraAudio as sA import matplotlib.pyplot as plt from scipy.io.wavfile import read import scipy.stats as ss filter = sF.sinatraFiltersBox() # # Input # print("reading...") filepath = sys.argv[1] freq, audioFile = read(filepath) print("read!") # # Segmentation # print("segmentation...") try: audioFile = audioFile[:, 0] except IndexError: pass audioFile = (audioFile - np.mean(audioFile)) / np.std(audioFile)
def segmentate(self, aD): import sinatraIO import sinatraFilter import numpy as np filterBox = sinatraFilter.sinatraFiltersBox() coeffCleaning = 1.5 wS = 700 rowL = 10000 rowControl = 1 print("reading {0}".format(aD.getName())) self.normalize(aD) aD = aD.getAudio() aD = aD[5000:] splitNumber = int(len(aD) / wS) aDClean = aD # yE,zE : depreciated # nE : contains the -log probability of belonging to the normal yE, zE, nE = filterBox.entropyInWindow(aDClean, 1400) del yE del zE statusStart = 0 matrixX = np.zeros(35) testArray = np.zeros(2) for sIter in range(2, len(aD) - 3): if statusStart == 0 and nE[sIter] >= 14: statusStart = 1 start = int(sIter) if statusStart == 1 and nE[sIter] < 14: statusStart = 0 minControl = 0 maxControl = 0 end = int(sIter) if end - start > 2800: audioTokken = aDClean[start:end] aCX, aCY = filterBox.exponentialDecay(audioTokken, wS) fD = filterBox.firstDerivative(aCY) sD = filterBox.secondDerivative(aCY) cStart = start for tIter in range(len(aCX) - 1): if fD[tIter] * fD[tIter + 1] < 0: if ((sD[tIter] + sD[tIter + 1]) * 0.5) < 0: maxControl = 1 else: minControl = 1 if (maxControl == 1): cutPoints = np.array([start, end]) tokkenLength = cutPoints[1] - cutPoints[0] tokkenRow = np.zeros(tokkenLength) tokkenRow = aDClean[cutPoints[0]:cutPoints[1]] tokkenInfo = self.extractFeatures(tokkenRow) tempTestArray = testArray rowControl = rowControl + 1 testArray = np.zeros((rowControl, 2)) testArray[0:(rowControl - 1), :] = tempTestArray testArray[rowControl - 1, 0] = start testArray[rowControl - 1, 1] = end tempMatrixX = matrixX matrixX = np.zeros((rowControl, 35)) matrixX[0:(rowControl - 1), :] = tempMatrixX matrixX[(rowControl - 1), :] = tokkenInfo maxControl = 0 minControl = 0 cStart = tIter statusStart = 0 print("task completed") try: matrixX = matrixX[1:, :] except IndexError: return [], testArray, 0 try: testArray = testArray[1:, :] except IndexError: return [], testArray, 0 testArray = testArray + 5000 return matrixX, testArray, (rowControl - 1)