<s> import sys <EOL> import time <EOL> import os <EOL> import glob <EOL> import numpy <EOL> import cPickle <EOL> import aifc <EOL> import math <EOL> from numpy import NaN , Inf , arange , isscalar , array <EOL> from scipy . fftpack import rfft <EOL> from scipy . fftpack import fft <EOL> from scipy . fftpack . realtransforms import dct <EOL> from scipy . signal import fftconvolve <EOL> from matplotlib . mlab import find <EOL> import matplotlib . pyplot as plt <EOL> from scipy import linalg as la <EOL> import audioTrainTest as aT <EOL> import audioBasicIO <EOL> import utilities <EOL> from scipy . signal import lfilter , hamming <EOL> eps = <NUM_LIT> <EOL> """<STR_LIT>""" <EOL> def stZCR ( frame ) : <EOL> """<STR_LIT>""" <EOL> count = len ( frame ) <EOL> countZ = numpy . sum ( numpy . abs ( numpy . diff ( numpy . sign ( frame ) ) ) ) / <NUM_LIT:2> <EOL> return ( numpy . float64 ( countZ ) / numpy . float64 ( count - <NUM_LIT:1.0> ) ) <EOL> def stEnergy ( frame ) : <EOL> """<STR_LIT>""" <EOL> return numpy . sum ( frame ** <NUM_LIT:2> ) / numpy . float64 ( len ( frame ) ) <EOL> def stEnergyEntropy ( frame , numOfShortBlocks = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> Eol = numpy . sum ( frame ** <NUM_LIT:2> ) <EOL> L = len ( frame ) <EOL> subWinLength = int ( numpy . floor ( L / numOfShortBlocks ) ) <EOL> if L != subWinLength * numOfShortBlocks : <EOL> frame = frame [ <NUM_LIT:0> : subWinLength * numOfShortBlocks ] <EOL> subWindows = frame . reshape ( subWinLength , numOfShortBlocks , order = '<STR_LIT:F>' ) . copy ( ) <EOL> s = numpy . sum ( subWindows ** <NUM_LIT:2> , axis = <NUM_LIT:0> ) / ( Eol + eps ) <EOL> Entropy = - numpy . sum ( s * numpy . log2 ( s + eps ) ) <EOL> return Entropy <EOL> """<STR_LIT>""" <EOL> def stSpectralCentroidAndSpread ( X , fs ) : <EOL> """<STR_LIT>""" <EOL> ind = ( numpy . arange ( <NUM_LIT:1> , len ( X ) + <NUM_LIT:1> ) ) * ( fs / ( <NUM_LIT> * len ( X ) ) ) <EOL> Xt = X . copy ( ) <EOL> Xt = Xt / Xt . max ( ) <EOL> NUM = numpy . sum ( ind * Xt ) <EOL> DEN = numpy . sum ( Xt ) + eps <EOL> C = ( NUM / DEN ) <EOL> S = numpy . sqrt ( numpy . sum ( ( ( ind - C ) ** <NUM_LIT:2> ) * Xt ) / DEN ) <EOL> C = C / ( fs / <NUM_LIT> ) <EOL> S = S / ( fs / <NUM_LIT> ) <EOL> return ( C , S ) <EOL> def stSpectralEntropy ( X , numOfShortBlocks = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> L = len ( X ) <EOL> Eol = numpy . sum ( X ** <NUM_LIT:2> ) <EOL> subWinLength = int ( numpy . floor ( L / numOfShortBlocks ) ) <EOL> if L != subWinLength * numOfShortBlocks : <EOL> X = X [ <NUM_LIT:0> : subWinLength * numOfShortBlocks ] <EOL> subWindows = X . reshape ( subWinLength , numOfShortBlocks , order = '<STR_LIT:F>' ) . copy ( ) <EOL> s = numpy . sum ( subWindows ** <NUM_LIT:2> , axis = <NUM_LIT:0> ) / ( Eol + eps ) <EOL> En = - numpy . sum ( s * numpy . log2 ( s + eps ) ) <EOL> return En <EOL> def stSpectralFlux ( X , Xprev ) : <EOL> """<STR_LIT>""" <EOL> sumX = numpy . sum ( X + eps ) <EOL> sumPrevX = numpy . sum ( Xprev + eps ) <EOL> F = numpy . sum ( ( X / sumX - Xprev / sumPrevX ) ** <NUM_LIT:2> ) <EOL> return F <EOL> def stSpectralRollOff ( X , c , fs ) : <EOL> """<STR_LIT>""" <EOL> totalEnergy = numpy . sum ( X ** <NUM_LIT:2> ) <EOL> fftLength = len ( X ) <EOL> Thres = c * totalEnergy <EOL> CumSum = numpy . cumsum ( X ** <NUM_LIT:2> ) + eps <EOL> [ a , ] = numpy . nonzero ( CumSum > Thres ) <EOL> if len ( a ) > <NUM_LIT:0> : <EOL> mC = numpy . float64 ( a [ <NUM_LIT:0> ] ) / ( float ( fftLength ) ) <EOL> else : <EOL> mC = <NUM_LIT:0.0> <EOL> return ( mC ) <EOL> def stHarmonic ( frame , fs ) : <EOL> """<STR_LIT>""" <EOL> M = numpy . round ( <NUM_LIT> * fs ) - <NUM_LIT:1> <EOL> R = numpy . correlate ( frame , frame , mode = '<STR_LIT>' ) <EOL> g = R [ len ( frame ) - <NUM_LIT:1> ] <EOL> R = R [ len ( frame ) : - <NUM_LIT:1> ] <EOL> [ a , ] = numpy . nonzero ( numpy . diff ( numpy . sign ( R ) ) ) <EOL> if len ( a ) == <NUM_LIT:0> : <EOL> m0 = len ( R ) - <NUM_LIT:1> <EOL> else : <EOL> m0 = a [ <NUM_LIT:0> ] <EOL> if M > len ( R ) : <EOL> M = len ( R ) - <NUM_LIT:1> <EOL> Gamma = numpy . zeros ( ( M ) , dtype = numpy . float64 ) <EOL> CSum = numpy . cumsum ( frame ** <NUM_LIT:2> ) <EOL> Gamma [ m0 : M ] = R [ m0 : M ] / ( numpy . sqrt ( ( g * CSum [ M : m0 : - <NUM_LIT:1> ] ) ) + eps ) <EOL> ZCR = stZCR ( Gamma ) <EOL> if ZCR > <NUM_LIT> : <EOL> HR = <NUM_LIT:0.0> <EOL> f0 = <NUM_LIT:0.0> <EOL> else : <EOL> if len ( Gamma ) == <NUM_LIT:0> : <EOL> HR = <NUM_LIT:1.0> <EOL> blag = <NUM_LIT:0.0> <EOL> Gamma = numpy . zeros ( ( M ) , dtype = numpy . float64 ) <EOL> else : <EOL> HR = numpy . max ( Gamma ) <EOL> blag = numpy . argmax ( Gamma ) <EOL> f0 = fs / ( blag + eps ) <EOL> if f0 > <NUM_LIT> : <EOL> f0 = <NUM_LIT:0.0> <EOL> if HR < <NUM_LIT:0.1> : <EOL> f0 = <NUM_LIT:0.0> <EOL> return ( HR , f0 ) <EOL> def mfccInitFilterBanks ( fs , nfft ) : <EOL> """<STR_LIT>""" <EOL> lowfreq = <NUM_LIT> <EOL> linsc = <NUM_LIT:200> / <NUM_LIT> <EOL> logsc = <NUM_LIT> <EOL> numLinFiltTotal = <NUM_LIT> <EOL> numLogFilt = <NUM_LIT> <EOL> if fs < <NUM_LIT> : <EOL> nlogfil = <NUM_LIT:5> <EOL> nFiltTotal = numLinFiltTotal + numLogFilt <EOL> freqs = numpy . zeros ( nFiltTotal + <NUM_LIT:2> ) <EOL> freqs [ : numLinFiltTotal ] = lowfreq + numpy . arange ( numLinFiltTotal ) * linsc <EOL> freqs [ numLinFiltTotal : ] = freqs [ numLinFiltTotal - <NUM_LIT:1> ] * logsc ** numpy . arange ( <NUM_LIT:1> , numLogFilt + <NUM_LIT:3> ) <EOL> heights = <NUM_LIT> / ( freqs [ <NUM_LIT:2> : ] - freqs [ <NUM_LIT:0> : - <NUM_LIT:2> ] ) <EOL> fbank = numpy . zeros ( ( nFiltTotal , nfft ) ) <EOL> nfreqs = numpy . arange ( nfft ) / ( <NUM_LIT:1.> * nfft ) * fs <EOL> for i in range ( nFiltTotal ) : <EOL> lowTrFreq = freqs [ i ] <EOL> cenTrFreq = freqs [ i + <NUM_LIT:1> ] <EOL> highTrFreq = freqs [ i + <NUM_LIT:2> ] <EOL> lid = numpy . arange ( numpy . floor ( lowTrFreq * nfft / fs ) + <NUM_LIT:1> , numpy . floor ( cenTrFreq * nfft / fs ) + <NUM_LIT:1> , dtype = numpy . int ) <EOL> lslope = heights [ i ] / ( cenTrFreq - lowTrFreq ) <EOL> rid = numpy . arange ( numpy . floor ( cenTrFreq * nfft / fs ) + <NUM_LIT:1> , numpy . floor ( highTrFreq * nfft / fs ) + <NUM_LIT:1> , dtype = numpy . int ) <EOL> rslope = heights [ i ] / ( highTrFreq - cenTrFreq ) <EOL> fbank [ i ] [ lid ] = lslope * ( nfreqs [ lid ] - lowTrFreq ) <EOL> fbank [ i ] [ rid ] = rslope * ( highTrFreq - nfreqs [ rid ] ) <EOL> return fbank , freqs <EOL> def stMFCC ( X , fbank , nceps ) : <EOL> """<STR_LIT>""" <EOL> mspec = numpy . log10 ( numpy . dot ( X , fbank . T ) + eps ) <EOL> ceps = dct ( mspec , type = <NUM_LIT:2> , norm = '<STR_LIT>' , axis = - <NUM_LIT:1> ) [ : nceps ] <EOL> return ceps <EOL> def stChromaFeaturesInit ( nfft , fs ) : <EOL> """<STR_LIT>""" <EOL> freqs = numpy . array ( [ ( ( f + <NUM_LIT:1> ) * fs ) / ( <NUM_LIT:2> * nfft ) for f in range ( nfft ) ] ) <EOL> Cp = <NUM_LIT> <EOL> nChroma = numpy . round ( <NUM_LIT> * numpy . log2 ( freqs / Cp ) ) . astype ( int ) <EOL> nFreqsPerChroma = numpy . zeros ( ( nChroma . shape [ <NUM_LIT:0> ] , ) ) <EOL> uChroma = numpy . unique ( nChroma ) <EOL> for u in uChroma : <EOL> idx = numpy . nonzero ( nChroma == u ) <EOL> nFreqsPerChroma [ idx ] = idx [ <NUM_LIT:0> ] . shape <EOL> return nChroma , nFreqsPerChroma <EOL> def stChromaFeatures ( X , fs , nChroma , nFreqsPerChroma ) : <EOL> chromaNames = [ '<STR_LIT:A>' , '<STR_LIT>' , '<STR_LIT:B>' , '<STR_LIT:C>' , '<STR_LIT>' , '<STR_LIT:D>' , '<STR_LIT>' , '<STR_LIT:E>' , '<STR_LIT:F>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> spec = X ** <NUM_LIT:2> <EOL> C = numpy . zeros ( ( nChroma . shape [ <NUM_LIT:0> ] , ) ) <EOL> C [ nChroma ] = spec <EOL> C /= nFreqsPerChroma [ nChroma ] <EOL> finalC = numpy . zeros ( ( <NUM_LIT:12> , <NUM_LIT:1> ) ) <EOL> newD = int ( numpy . ceil ( C . shape [ <NUM_LIT:0> ] / <NUM_LIT> ) * <NUM_LIT:12> ) <EOL> C2 = numpy . zeros ( ( newD , ) ) <EOL> C2 [ <NUM_LIT:0> : C . shape [ <NUM_LIT:0> ] ] = C <EOL> C2 = C2 . reshape ( C2 . shape [ <NUM_LIT:0> ] / <NUM_LIT:12> , <NUM_LIT:12> ) <EOL> finalC = numpy . matrix ( numpy . sum ( C2 , axis = <NUM_LIT:0> ) ) . T <EOL> finalC /= spec . sum ( ) <EOL> return chromaNames , finalC <EOL> def stChromagram ( signal , Fs , Win , Step , PLOT = False ) : <EOL> """<STR_LIT>""" <EOL> Win = int ( Win ) <EOL> Step = int ( Step ) <EOL> signal = numpy . double ( signal ) <EOL> signal = signal / ( <NUM_LIT> ** <NUM_LIT:15> ) <EOL> DC = signal . mean ( ) <EOL> MAX = ( numpy . abs ( signal ) ) . max ( ) <EOL> signal = ( signal - DC ) / ( MAX - DC ) <EOL> N = len ( signal ) <EOL> curPos = <NUM_LIT:0> <EOL> countFrames = <NUM_LIT:0> <EOL> nfft = int ( Win / <NUM_LIT:2> ) <EOL> nChroma , nFreqsPerChroma = stChromaFeaturesInit ( nfft , Fs ) <EOL> chromaGram = numpy . array ( [ ] , dtype = numpy . float64 ) <EOL> while ( curPos + Win - <NUM_LIT:1> < N ) : <EOL> countFrames += <NUM_LIT:1> <EOL> x = signal [ curPos : curPos + Win ] <EOL> curPos = curPos + Step <EOL> X = abs ( fft ( x ) ) <EOL> X = X [ <NUM_LIT:0> : nfft ] <EOL> X = X / len ( X ) <EOL> chromaNames , C = stChromaFeatures ( X , Fs , nChroma , nFreqsPerChroma ) <EOL> C = C [ : , <NUM_LIT:0> ] <EOL> if countFrames == <NUM_LIT:1> : <EOL> chromaGram = C . T <EOL> else : <EOL> chromaGram = numpy . vstack ( ( chromaGram , C . T ) ) <EOL> FreqAxis = chromaNames <EOL> TimeAxis = [ ( t * Step ) / Fs for t in range ( chromaGram . shape [ <NUM_LIT:0> ] ) ] <EOL> if ( PLOT ) : <EOL> fig , ax = plt . subplots ( ) <EOL> chromaGramToPlot = chromaGram . transpose ( ) [ : : - <NUM_LIT:1> , : ] <EOL> Ratio = chromaGramToPlot . shape [ <NUM_LIT:1> ] / ( <NUM_LIT:3> * chromaGramToPlot . shape [ <NUM_LIT:0> ] ) <EOL> chromaGramToPlot = numpy . repeat ( chromaGramToPlot , Ratio , axis = <NUM_LIT:0> ) <EOL> imgplot = plt . imshow ( chromaGramToPlot ) <EOL> Fstep = int ( nfft / <NUM_LIT> ) <EOL> ax . set_yticks ( range ( Ratio / <NUM_LIT:2> , len ( FreqAxis ) * Ratio , Ratio ) ) <EOL> ax . set_yticklabels ( FreqAxis [ : : - <NUM_LIT:1> ] ) <EOL> TStep = countFrames / <NUM_LIT:3> <EOL> TimeTicks = range ( <NUM_LIT:0> , countFrames , TStep ) <EOL> TimeTicksLabels = [ '<STR_LIT>' % ( float ( t * Step ) / Fs ) for t in TimeTicks ] <EOL> ax . set_xticks ( TimeTicks ) <EOL> ax . set_xticklabels ( TimeTicksLabels ) <EOL> ax . set_xlabel ( '<STR_LIT>' ) <EOL> imgplot . set_cmap ( '<STR_LIT>' ) <EOL> plt . colorbar ( ) <EOL> plt . show ( ) <EOL> return ( chromaGram , TimeAxis , FreqAxis ) <EOL> def phormants ( x , Fs ) : <EOL> N = len ( x ) <EOL> w = numpy . hamming ( N ) <EOL> x1 = x * w <EOL> x1 = lfilter ( [ <NUM_LIT:1> ] , [ <NUM_LIT:1.> , <NUM_LIT> ] , x1 ) <EOL> ncoeff = <NUM_LIT:2> + Fs / <NUM_LIT:1000> <EOL> A , e , k = lpc ( x1 , ncoeff ) <EOL> rts = numpy . roots ( A ) <EOL> rts = [ r for r in rts if numpy . imag ( r ) >= <NUM_LIT:0> ] <EOL> angz = numpy . arctan2 ( numpy . imag ( rts ) , numpy . real ( rts ) ) <EOL> frqs = sorted ( angz * ( Fs / ( <NUM_LIT:2> * math . pi ) ) ) <EOL> return frqs <EOL> def beatExtraction ( stFeatures , winSize , PLOT = False ) : <EOL> """<STR_LIT>""" <EOL> toWatch = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:15> , <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT> ] <EOL> maxBeatTime = int ( round ( <NUM_LIT> / winSize ) ) <EOL> HistAll = numpy . zeros ( ( maxBeatTime , ) ) <EOL> for ii , i in enumerate ( toWatch ) : <EOL> DifThres = <NUM_LIT> * ( numpy . abs ( stFeatures [ i , <NUM_LIT:0> : - <NUM_LIT:1> ] - stFeatures [ i , <NUM_LIT:1> : : ] ) ) . mean ( ) <EOL> [ pos1 , _ ] = utilities . peakdet ( stFeatures [ i , : ] , DifThres ) <EOL> posDifs = [ ] <EOL> for j in range ( len ( pos1 ) - <NUM_LIT:1> ) : <EOL> posDifs . append ( pos1 [ j + <NUM_LIT:1> ] - pos1 [ j ] ) <EOL> [ HistTimes , HistEdges ] = numpy . histogram ( posDifs , numpy . arange ( <NUM_LIT:0.5> , maxBeatTime + <NUM_LIT> ) ) <EOL> HistCenters = ( HistEdges [ <NUM_LIT:0> : - <NUM_LIT:1> ] + HistEdges [ <NUM_LIT:1> : : ] ) / <NUM_LIT> <EOL> HistTimes = HistTimes . astype ( float ) / stFeatures . shape [ <NUM_LIT:1> ] <EOL> HistAll += HistTimes <EOL> if PLOT : <EOL> plt . subplot ( <NUM_LIT:9> , <NUM_LIT:2> , ii + <NUM_LIT:1> ) <EOL> plt . plot ( stFeatures [ i , : ] , '<STR_LIT:k>' ) <EOL> for k in pos1 : <EOL> plt . plot ( k , stFeatures [ i , k ] , '<STR_LIT>' ) <EOL> f1 = plt . gca ( ) <EOL> f1 . axes . get_xaxis ( ) . set_ticks ( [ ] ) <EOL> f1 . axes . get_yaxis ( ) . set_ticks ( [ ] ) <EOL> if PLOT : <EOL> plt . show ( block = False ) <EOL> plt . figure ( ) <EOL> I = numpy . argmax ( HistAll ) <EOL> BPMs = <NUM_LIT> / ( HistCenters * winSize ) <EOL> BPM = BPMs [ I ] <EOL> Ratio = HistAll [ I ] / HistAll . sum ( ) <EOL> if PLOT : <EOL> HistAll = HistAll [ BPMs < <NUM_LIT> ] <EOL> BPMs = BPMs [ BPMs < <NUM_LIT> ] <EOL> plt . plot ( BPMs , HistAll , '<STR_LIT:k>' ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> plt . show ( block = True ) <EOL> return BPM , Ratio <EOL> def stSpectogram ( signal , Fs , Win , Step , PLOT = False ) : <EOL> """<STR_LIT>""" <EOL> Win = int ( Win ) <EOL> Step = int ( Step ) <EOL> signal = numpy . double ( signal ) <EOL> signal = signal / ( <NUM_LIT> ** <NUM_LIT:15> ) <EOL> DC = signal . mean ( ) <EOL> MAX = ( numpy . abs ( signal ) ) . max ( ) <EOL> signal = ( signal - DC ) / ( MAX - DC ) <EOL> N = len ( signal ) <EOL> curPos = <NUM_LIT:0> <EOL> countFrames = <NUM_LIT:0> <EOL> nfft = int ( Win / <NUM_LIT:2> ) <EOL> specgram = numpy . array ( [ ] , dtype = numpy . float64 ) <EOL> while ( curPos + Win - <NUM_LIT:1> < N ) : <EOL> countFrames += <NUM_LIT:1> <EOL> x = signal [ curPos : curPos + Win ] <EOL> curPos = curPos + Step <EOL> X = abs ( fft ( x ) ) <EOL> X = X [ <NUM_LIT:0> : nfft ] <EOL> X = X / len ( X ) <EOL> if countFrames == <NUM_LIT:1> : <EOL> specgram = X ** <NUM_LIT:2> <EOL> else : <EOL> specgram = numpy . vstack ( ( specgram , X ) ) <EOL> FreqAxis = [ ( ( f + <NUM_LIT:1> ) * Fs ) / ( <NUM_LIT:2> * nfft ) for f in range ( specgram . shape [ <NUM_LIT:1> ] ) ] <EOL> TimeAxis = [ ( t * Step ) / Fs for t in range ( specgram . shape [ <NUM_LIT:0> ] ) ] <EOL> if ( PLOT ) : <EOL> fig , ax = plt . subplots ( ) <EOL> imgplot = plt . imshow ( specgram . transpose ( ) [ : : - <NUM_LIT:1> , : ] ) <EOL> Fstep = int ( nfft / <NUM_LIT> ) <EOL> FreqTicks = range ( <NUM_LIT:0> , int ( nfft ) + Fstep , Fstep ) <EOL> FreqTicksLabels = [ str ( Fs / <NUM_LIT:2> - int ( ( f * Fs ) / ( <NUM_LIT:2> * nfft ) ) ) for f in FreqTicks ] <EOL> ax . set_yticks ( FreqTicks ) <EOL> ax . set_yticklabels ( FreqTicksLabels ) <EOL> TStep = countFrames / <NUM_LIT:3> <EOL> TimeTicks = range ( <NUM_LIT:0> , countFrames , TStep ) <EOL> TimeTicksLabels = [ '<STR_LIT>' % ( float ( t * Step ) / Fs ) for t in TimeTicks ] <EOL> ax . set_xticks ( TimeTicks ) <EOL> ax . set_xticklabels ( TimeTicksLabels ) <EOL> ax . set_xlabel ( '<STR_LIT>' ) <EOL> ax . set_ylabel ( '<STR_LIT>' ) <EOL> imgplot . set_cmap ( '<STR_LIT>' ) <EOL> plt . colorbar ( ) <EOL> plt . show ( ) <EOL> return ( specgram , TimeAxis , FreqAxis ) <EOL> """<STR_LIT>""" <EOL> def stFeatureExtraction ( signal , Fs , Win , Step ) : <EOL> """<STR_LIT>""" <EOL> Win = int ( Win ) <EOL> Step = int ( Step ) <EOL> signal = numpy . double ( signal ) <EOL> signal = signal / ( <NUM_LIT> ** <NUM_LIT:15> ) <EOL> DC = signal . mean ( ) <EOL> MAX = ( numpy . abs ( signal ) ) . max ( ) <EOL> signal = ( signal - DC ) / MAX <EOL> N = len ( signal ) <EOL> curPos = <NUM_LIT:0> <EOL> countFrames = <NUM_LIT:0> <EOL> nFFT = Win / <NUM_LIT:2> <EOL> [ fbank , freqs ] = mfccInitFilterBanks ( Fs , nFFT ) <EOL> nChroma , nFreqsPerChroma = stChromaFeaturesInit ( nFFT , Fs ) <EOL> numOfTimeSpectralFeatures = <NUM_LIT:8> <EOL> numOfHarmonicFeatures = <NUM_LIT:0> <EOL> nceps = <NUM_LIT> <EOL> numOfChromaFeatures = <NUM_LIT> <EOL> totalNumOfFeatures = numOfTimeSpectralFeatures + nceps + numOfHarmonicFeatures + numOfChromaFeatures <EOL> stFeatures = numpy . array ( [ ] , dtype = numpy . float64 ) <EOL> while ( curPos + Win - <NUM_LIT:1> < N ) : <EOL> countFrames += <NUM_LIT:1> <EOL> x = signal [ curPos : curPos + Win ] <EOL> curPos = curPos + Step <EOL> X = abs ( fft ( x ) ) <EOL> X = X [ <NUM_LIT:0> : nFFT ] <EOL> X = X / len ( X ) <EOL> if countFrames == <NUM_LIT:1> : <EOL> Xprev = X . copy ( ) <EOL> curFV = numpy . zeros ( ( totalNumOfFeatures , <NUM_LIT:1> ) ) <EOL> curFV [ <NUM_LIT:0> ] = stZCR ( x ) <EOL> curFV [ <NUM_LIT:1> ] = stEnergy ( x ) <EOL> curFV [ <NUM_LIT:2> ] = stEnergyEntropy ( x ) <EOL> [ curFV [ <NUM_LIT:3> ] , curFV [ <NUM_LIT:4> ] ] = stSpectralCentroidAndSpread ( X , Fs ) <EOL> curFV [ <NUM_LIT:5> ] = stSpectralEntropy ( X ) <EOL> curFV [ <NUM_LIT:6> ] = stSpectralFlux ( X , Xprev ) <EOL> curFV [ <NUM_LIT:7> ] = stSpectralRollOff ( X , <NUM_LIT> , Fs ) <EOL> curFV [ numOfTimeSpectralFeatures : numOfTimeSpectralFeatures + nceps , <NUM_LIT:0> ] = stMFCC ( X , fbank , nceps ) . copy ( ) <EOL> chromaNames , chromaF = stChromaFeatures ( X , Fs , nChroma , nFreqsPerChroma ) <EOL> curFV [ numOfTimeSpectralFeatures + nceps : numOfTimeSpectralFeatures + nceps + numOfChromaFeatures - <NUM_LIT:1> ] = chromaF <EOL> curFV [ numOfTimeSpectralFeatures + nceps + numOfChromaFeatures - <NUM_LIT:1> ] = chromaF . std ( ) <EOL> if countFrames == <NUM_LIT:1> : <EOL> stFeatures = curFV <EOL> else : <EOL> stFeatures = numpy . concatenate ( ( stFeatures , curFV ) , <NUM_LIT:1> ) <EOL> Xprev = X . copy ( ) <EOL> return numpy . array ( stFeatures ) <EOL> def mtFeatureExtraction ( signal , Fs , mtWin , mtStep , stWin , stStep ) : <EOL> """<STR_LIT>""" <EOL> mtWinRatio = int ( round ( mtWin / stStep ) ) <EOL> mtStepRatio = int ( round ( mtStep / stStep ) ) <EOL> mtFeatures = [ ] <EOL> stFeatures = stFeatureExtraction ( signal , Fs , stWin , stStep ) <EOL> numOfFeatures = len ( stFeatures ) <EOL> numOfStatistics = <NUM_LIT:2> <EOL> mtFeatures = [ ] <EOL> for i in range ( numOfStatistics * numOfFeatures ) : <EOL> mtFeatures . append ( [ ] ) <EOL> for i in range ( numOfFeatures ) : <EOL> curPos = <NUM_LIT:0> <EOL> N = len ( stFeatures [ i ] ) <EOL> while ( curPos < N ) : <EOL> N1 = curPos <EOL> N2 = curPos + mtWinRatio <EOL> if N2 > N : <EOL> N2 = N <EOL> curStFeatures = stFeatures [ i ] [ N1 : N2 ] <EOL> mtFeatures [ i ] . append ( numpy . mean ( curStFeatures ) ) <EOL> mtFeatures [ i + numOfFeatures ] . append ( numpy . std ( curStFeatures ) ) <EOL> curPos += mtStepRatio <EOL> return numpy . array ( mtFeatures ) , stFeatures <EOL> def stFeatureSpeed ( signal , Fs , Win , Step ) : <EOL> signal = numpy . double ( signal ) <EOL> signal = signal / ( <NUM_LIT> ** <NUM_LIT:15> ) <EOL> DC = signal . mean ( ) <EOL> MAX = ( numpy . abs ( signal ) ) . max ( ) <EOL> signal = ( signal - DC ) / MAX <EOL> N = len ( signal ) <EOL> curPos = <NUM_LIT:0> <EOL> countFrames = <NUM_LIT:0> <EOL> lowfreq = <NUM_LIT> <EOL> linsc = <NUM_LIT:200> / <NUM_LIT> <EOL> logsc = <NUM_LIT> <EOL> nlinfil = <NUM_LIT> <EOL> nlogfil = <NUM_LIT> <EOL> nceps = <NUM_LIT> <EOL> nfil = nlinfil + nlogfil <EOL> nfft = Win / <NUM_LIT:2> <EOL> if Fs < <NUM_LIT> : <EOL> nlogfil = <NUM_LIT:5> <EOL> nfil = nlinfil + nlogfil <EOL> nfft = Win / <NUM_LIT:2> <EOL> [ fbank , freqs ] = mfccInitFilterBanks ( Fs , nfft , lowfreq , linsc , logsc , nlinfil , nlogfil ) <EOL> numOfTimeSpectralFeatures = <NUM_LIT:8> <EOL> numOfHarmonicFeatures = <NUM_LIT:1> <EOL> totalNumOfFeatures = numOfTimeSpectralFeatures + nceps + numOfHarmonicFeatures <EOL> stFeatures = [ ] <EOL> while ( curPos + Win - <NUM_LIT:1> < N ) : <EOL> countFrames += <NUM_LIT:1> <EOL> x = signal [ curPos : curPos + Win ] <EOL> curPos = curPos + Step <EOL> X = abs ( fft ( x ) ) <EOL> X = X [ <NUM_LIT:0> : nfft ] <EOL> X = X / len ( X ) <EOL> Ex = <NUM_LIT:0.0> <EOL> El = <NUM_LIT:0.0> <EOL> X [ <NUM_LIT:0> : <NUM_LIT:4> ] = <NUM_LIT:0> <EOL> stFeatures . append ( stHarmonic ( x , Fs ) ) <EOL> return numpy . array ( stFeatures ) <EOL> """<STR_LIT>""" <EOL> def dirWavFeatureExtraction ( dirName , mtWin , mtStep , stWin , stStep , computeBEAT = False ) : <EOL> """<STR_LIT>""" <EOL> allMtFeatures = numpy . array ( [ ] ) <EOL> processingTimes = [ ] <EOL> types = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> wavFilesList = [ ] <EOL> for files in types : <EOL> wavFilesList . extend ( glob . glob ( os . path . join ( dirName , files ) ) ) <EOL> wavFilesList = sorted ( wavFilesList ) <EOL> for wavFile in wavFilesList : <EOL> [ Fs , x ] = audioBasicIO . readAudioFile ( wavFile ) <EOL> t1 = time . clock ( ) <EOL> x = audioBasicIO . stereo2mono ( x ) <EOL> if computeBEAT : <EOL> [ MidTermFeatures , stFeatures ] = mtFeatureExtraction ( x , Fs , round ( mtWin * Fs ) , round ( mtStep * Fs ) , round ( Fs * stWin ) , round ( Fs * stStep ) ) <EOL> [ beat , beatConf ] = beatExtraction ( stFeatures , stStep ) <EOL> else : <EOL> [ MidTermFeatures , _ ] = mtFeatureExtraction ( x , Fs , round ( mtWin * Fs ) , round ( mtStep * Fs ) , round ( Fs * stWin ) , round ( Fs * stStep ) ) <EOL> MidTermFeatures = numpy . transpose ( MidTermFeatures ) <EOL> MidTermFeatures = MidTermFeatures . mean ( axis = <NUM_LIT:0> ) <EOL> if computeBEAT : <EOL> MidTermFeatures = numpy . append ( MidTermFeatures , beat ) <EOL> MidTermFeatures = numpy . append ( MidTermFeatures , beatConf ) <EOL> if len ( allMtFeatures ) == <NUM_LIT:0> : <EOL> allMtFeatures = MidTermFeatures <EOL> else : <EOL> allMtFeatures = numpy . vstack ( ( allMtFeatures , MidTermFeatures ) ) <EOL> t2 = time . clock ( ) <EOL> duration = float ( len ( x ) ) / Fs <EOL> processingTimes . append ( ( t2 - t1 ) / duration ) <EOL> if len ( processingTimes ) > <NUM_LIT:0> : <EOL> print "<STR_LIT>" . format ( ( <NUM_LIT:1.0> / numpy . mean ( numpy . array ( processingTimes ) ) ) ) <EOL> return ( allMtFeatures , wavFilesList ) <EOL> def dirsWavFeatureExtraction ( dirNames , mtWin , mtStep , stWin , stStep , computeBEAT = False ) : <EOL> '''<STR_LIT>''' <EOL> features = [ ] <EOL> classNames = [ ] <EOL> fileNames = [ ] <EOL> for i , d in enumerate ( dirNames ) : <EOL> [ f , fn ] = dirWavFeatureExtraction ( d , mtWin , mtStep , stWin , stStep , computeBEAT = computeBEAT ) <EOL> if f . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> features . append ( f ) <EOL> fileNames . append ( fn ) <EOL> if d [ - <NUM_LIT:1> ] == "<STR_LIT:/>" : <EOL> classNames . append ( d . split ( os . sep ) [ - <NUM_LIT:2> ] ) <EOL> else : <EOL> classNames . append ( d . split ( os . sep ) [ - <NUM_LIT:1> ] ) <EOL> return features , classNames , fileNames <EOL> def dirWavFeatureExtractionNoAveraging ( dirName , mtWin , mtStep , stWin , stStep ) : <EOL> """<STR_LIT>""" <EOL> allMtFeatures = numpy . array ( [ ] ) <EOL> signalIndices = numpy . array ( [ ] ) <EOL> processingTimes = [ ] <EOL> types = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> wavFilesList = [ ] <EOL> for files in types : <EOL> wavFilesList . extend ( glob . glob ( os . path . join ( dirName , files ) ) ) <EOL> wavFilesList = sorted ( wavFilesList ) <EOL> for i , wavFile in enumerate ( wavFilesList ) : <EOL> [ Fs , x ] = audioBasicIO . readAudioFile ( wavFile ) <EOL> x = audioBasicIO . stereo2mono ( x ) <EOL> [ MidTermFeatures , _ ] = mtFeatureExtraction ( x , Fs , round ( mtWin * Fs ) , round ( mtStep * Fs ) , round ( Fs * stWin ) , round ( Fs * stStep ) ) <EOL> MidTermFeatures = numpy . transpose ( MidTermFeatures ) <EOL> if len ( allMtFeatures ) == <NUM_LIT:0> : <EOL> allMtFeatures = MidTermFeatures <EOL> signalIndices = numpy . zeros ( ( MidTermFeatures . shape [ <NUM_LIT:0> ] , ) ) <EOL> else : <EOL> allMtFeatures = numpy . vstack ( ( allMtFeatures , MidTermFeatures ) ) <EOL> signalIndices = numpy . append ( signalIndices , i * numpy . ones ( ( MidTermFeatures . shape [ <NUM_LIT:0> ] , ) ) ) <EOL> return ( allMtFeatures , signalIndices , wavFilesList ) <EOL> def mtFeatureExtractionToFile ( fileName , midTermSize , midTermStep , shortTermSize , shortTermStep , outPutFile , <EOL> storeStFeatures = False , storeToCSV = False , PLOT = False ) : <EOL> """<STR_LIT>""" <EOL> [ Fs , x ] = audioBasicIO . readAudioFile ( fileName ) <EOL> x = audioBasicIO . stereo2mono ( x ) <EOL> if storeStFeatures : <EOL> [ mtF , stF ] = mtFeatureExtraction ( x , Fs , round ( Fs * midTermSize ) , round ( Fs * midTermStep ) , round ( Fs * shortTermSize ) , round ( Fs * shortTermStep ) ) <EOL> else : <EOL> [ mtF , _ ] = mtFeatureExtraction ( x , Fs , round ( Fs * midTermSize ) , round ( Fs * midTermStep ) , round ( Fs * shortTermSize ) , round ( Fs * shortTermStep ) ) <EOL> numpy . save ( outPutFile , mtF ) <EOL> if PLOT : <EOL> print "<STR_LIT>" + outPutFile + "<STR_LIT>" <EOL> if storeToCSV : <EOL> numpy . savetxt ( outPutFile + "<STR_LIT>" , mtF . T , delimiter = "<STR_LIT:U+002C>" ) <EOL> if PLOT : <EOL> print "<STR_LIT>" + outPutFile + "<STR_LIT>" <EOL> if storeStFeatures : <EOL> numpy . save ( outPutFile + "<STR_LIT>" , stF ) <EOL> if PLOT : <EOL> print "<STR_LIT>" + outPutFile + "<STR_LIT>" <EOL> if storeToCSV : <EOL> numpy . savetxt ( outPutFile + "<STR_LIT>" , stF . T , delimiter = "<STR_LIT:U+002C>" ) <EOL> if PLOT : <EOL> print "<STR_LIT>" + outPutFile + "<STR_LIT>" <EOL> def mtFeatureExtractionToFileDir ( dirName , midTermSize , midTermStep , shortTermSize , shortTermStep , storeStFeatures = False , storeToCSV = False , PLOT = False ) : <EOL> types = ( dirName + os . sep + '<STR_LIT>' , ) <EOL> filesToProcess = [ ] <EOL> for files in types : <EOL> filesToProcess . extend ( glob . glob ( files ) ) <EOL> for f in filesToProcess : <EOL> outPath = f <EOL> mtFeatureExtractionToFile ( f , midTermSize , midTermStep , shortTermSize , shortTermStep , outPath , storeStFeatures , storeToCSV , PLOT ) </s>
