<s> import numpy as np <EOL> from dear . spectrum import DFTSpectrum <EOL> class MFCCs ( DFTSpectrum ) : <EOL> @ staticmethod <EOL> def freq2mel ( freq ) : <EOL> return <NUM_LIT:1000> * np . log ( <NUM_LIT:1> + freq / <NUM_LIT> ) / np . log ( <NUM_LIT:1> + <NUM_LIT> / <NUM_LIT> ) <EOL> @ staticmethod <EOL> def mel2freq ( mel ) : <EOL> return <NUM_LIT> * ( np . exp ( mel / <NUM_LIT> * np . log ( <NUM_LIT:1> + <NUM_LIT> / <NUM_LIT> ) ) - <NUM_LIT:1> ) <EOL> @ staticmethod <EOL> def pre_calculate ( N , win , freq_min , freq_max , nqfreq ) : <EOL> bins = int ( win ) / <NUM_LIT:2> <EOL> mel_min , mel_max = MFCCs . freq2mel ( freq_min ) , MFCCs . freq2mel ( freq_max ) <EOL> mel_centrs = np . arange ( N + <NUM_LIT:2> ) * ( mel_max - mel_min ) / ( N + <NUM_LIT:1> ) + mel_min <EOL> bin_centrs = np . round ( MFCCs . mel2freq ( mel_centrs ) / nqfreq * bins ) <EOL> filters = np . zeros ( ( N , bins ) ) <EOL> for i in xrange ( N ) : <EOL> st , ct , ed = bin_centrs [ i : i + <NUM_LIT:3> ] <EOL> assert st < ct < ed <EOL> up = ( np . arange ( st , ct ) - st ) / ( ct - st ) <EOL> down = ( ed - np . arange ( ct , ed ) ) / ( ed - ct ) <EOL> filters [ i , int ( st ) : int ( ct ) ] = up <EOL> filters [ i , int ( ct ) : int ( ed ) ] = down <EOL> dc = np . ones ( ( N , N ) ) * np . arange ( N , dtype = np . double ) <EOL> dcmt = np . pi * ( <NUM_LIT:0.5> + dc ) / N <EOL> dcmt = ( <NUM_LIT:1.0> / np . sqrt ( N / <NUM_LIT> ) ) * np . cos ( dc . T * dcmt ) <EOL> dcmt [ <NUM_LIT:0> ] /= np . sqrt ( <NUM_LIT> ) <EOL> var = { <EOL> '<STR_LIT>' : win , <EOL> '<STR_LIT>' : filters , <EOL> '<STR_LIT>' : dcmt <EOL> } <EOL> return type ( '<STR_LIT>' , ( object , ) , var ) <EOL> @ staticmethod <EOL> def transform ( frame , var ) : <EOL> f = np . dot ( np . abs ( frame [ : - <NUM_LIT:1> ] ) * var . WL , var . Triangles . T ) <EOL> f = np . dot ( np . log ( f ) , var . DCTMatrix ) <EOL> return f <EOL> def walk ( self , N = <NUM_LIT:20> , freq_min = <NUM_LIT:0> , freq_max = <NUM_LIT> , win = <NUM_LIT> , step = <NUM_LIT> , start = <NUM_LIT:0> , end = None , win_shape = np . hanning ) : <EOL> samplerate = self . audio . samplerate <EOL> nqfreq = samplerate / <NUM_LIT> <EOL> N = int ( N ) <EOL> assert <NUM_LIT:0> < N < int ( win ) / <NUM_LIT:2> <EOL> freq_max = ( freq_max is None ) and nqfreq or freq_max <EOL> assert <NUM_LIT:0> <= freq_min < freq_max <= nqfreq <EOL> var = MFCCs . pre_calculate ( N , win , freq_min , freq_max , nqfreq ) <EOL> for samples in super ( MFCCs , self ) . walk ( win , step , start , end , join_channels = True , win_shape = win_shape ) : <EOL> frame = MFCCs . transform ( samples , var ) <EOL> yield frame </s>
