<s> import numpy as np <EOL> from scipy . fftpack import dct <EOL> from collections import deque <EOL> class MFCCFrontEnd : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sourcerate = <NUM_LIT> , framesize = <NUM_LIT> , <EOL> usehamming = True , preemcoef = <NUM_LIT> , <EOL> numchans = <NUM_LIT> , ceplifter = <NUM_LIT> , numceps = <NUM_LIT:12> , <EOL> enormalise = True , zmeansource = True , usepower = True , usec0 = True , usecmn = False , <EOL> usedelta = True , useacc = True , n_last_frames = <NUM_LIT:0> , <EOL> lofreq = <NUM_LIT> , hifreq = <NUM_LIT> , mel_banks_only = False ) : <EOL> self . sourcerate = sourcerate <EOL> self . framesize = framesize <EOL> self . usehamming = usehamming <EOL> self . preemcoef = preemcoef <EOL> self . numchans = numchans <EOL> self . ceplifter = ceplifter <EOL> self . enormalise = enormalise <EOL> self . zmeansource = zmeansource <EOL> self . usepower = usepower <EOL> self . usec0 = usec0 <EOL> self . usecmn = usecmn <EOL> self . usedelta = usedelta <EOL> self . useacc = useacc <EOL> self . numceps = numceps <EOL> self . lofreq = lofreq <EOL> self . hifreq = hifreq <EOL> self . mel_banks_only = mel_banks_only <EOL> self . prior = <NUM_LIT:0.0> <EOL> self . n_last_frames = n_last_frames <EOL> self . mfcc_queue = deque ( maxlen = <NUM_LIT:4> + n_last_frames ) <EOL> self . mfcc_delta_queue = deque ( maxlen = <NUM_LIT:4> + n_last_frames ) <EOL> self . init_hamming ( ) <EOL> self . init_mel_filter_bank ( ) <EOL> self . init_cep_liftering_weights ( ) <EOL> def freq_to_mel ( self , freq ) : <EOL> return <NUM_LIT> * np . log ( <NUM_LIT:1.0> + freq / <NUM_LIT> ) <EOL> def mel_to_freq ( self , mel ) : <EOL> return <NUM_LIT> * ( np . exp ( mel / <NUM_LIT> ) - <NUM_LIT:1.0> ) <EOL> def init_hamming ( self ) : <EOL> self . hamming = np . hamming ( self . framesize ) <EOL> def init_mel_filter_bank ( self ) : <EOL> """<STR_LIT>""" <EOL> minMel = self . freq_to_mel ( self . lofreq ) <EOL> maxMel = self . freq_to_mel ( self . hifreq ) <EOL> filterMatrix = np . zeros ( ( self . numchans , self . framesize / <NUM_LIT:2> + <NUM_LIT:1> ) ) <EOL> melRange = np . array ( xrange ( self . numchans + <NUM_LIT:2> ) ) <EOL> melCenterFilters = melRange * ( maxMel - minMel ) / ( <EOL> self . numchans + <NUM_LIT:1> ) + minMel <EOL> dfreq = self . sourcerate / self . framesize <EOL> centerIndex = np . array ( <EOL> np . round ( self . mel_to_freq ( melCenterFilters ) / dfreq ) , int ) <EOL> for i in xrange ( self . numchans ) : <EOL> start , centre , end = centerIndex [ i : i + <NUM_LIT:3> ] <EOL> k1 = np . float32 ( centre - start ) <EOL> k2 = np . float32 ( end - centre ) <EOL> up = ( np . array ( xrange ( start , centre ) ) - start ) / k1 <EOL> down = ( end - np . array ( xrange ( centre , end ) ) ) / k2 <EOL> filterMatrix [ i ] [ start : centre ] = up <EOL> filterMatrix [ i ] [ centre : end ] = down <EOL> self . mel_filter_bank = filterMatrix . transpose ( ) <EOL> def init_cep_liftering_weights ( self ) : <EOL> cep_lift_weights = np . zeros ( ( self . numceps , ) ) <EOL> a = np . pi / self . ceplifter <EOL> b = self . ceplifter / <NUM_LIT> <EOL> for i in range ( self . numceps ) : <EOL> cep_lift_weights [ i ] = <NUM_LIT:1.0> + b * np . sin ( i * a ) <EOL> self . cep_lift_weights = cep_lift_weights <EOL> def preemphasis ( self , frame ) : <EOL> out_frame = np . zeros_like ( frame ) <EOL> out_frame [ <NUM_LIT:0> ] = frame [ <NUM_LIT:0> ] - self . preemcoef * self . prior <EOL> for i in range ( <NUM_LIT:1> , len ( frame ) ) : <EOL> out_frame [ i ] = frame [ i ] - self . preemcoef * frame [ i - <NUM_LIT:1> ] <EOL> self . prior = frame [ - <NUM_LIT:1> ] <EOL> return out_frame <EOL> def param ( self , frame ) : <EOL> """<STR_LIT>""" <EOL> if self . zmeansource : <EOL> frame = frame - np . mean ( frame ) <EOL> frame = self . preemphasis ( frame ) <EOL> if self . usehamming : <EOL> frame = self . hamming * frame <EOL> complex_spectrum = np . fft . rfft ( frame ) <EOL> power_spectrum = complex_spectrum . real * complex_spectrum . real + complex_spectrum . imag * complex_spectrum . imag <EOL> if not self . usepower : <EOL> power_spectrum = np . sqrt ( power_spectrum ) <EOL> mel_spectrum = np . dot ( power_spectrum , self . mel_filter_bank ) <EOL> for i in range ( len ( mel_spectrum ) ) : <EOL> if mel_spectrum [ i ] < <NUM_LIT:1.0> : <EOL> mel_spectrum [ i ] = <NUM_LIT:1.0> <EOL> mel_spectrum = np . log ( mel_spectrum ) <EOL> if self . mel_banks_only : <EOL> mfcc = mel_spectrum <EOL> self . mfcc_queue . append ( mel_spectrum ) <EOL> else : <EOL> cepstrum = dct ( mel_spectrum , type = <NUM_LIT:2> , norm = '<STR_LIT>' ) <EOL> c0 = cepstrum [ <NUM_LIT:0> ] <EOL> htk_cepstrum = cepstrum [ <NUM_LIT:1> : self . numceps + <NUM_LIT:1> ] <EOL> cep_lift_mfcc = self . cep_lift_weights * htk_cepstrum <EOL> if self . usec0 : <EOL> mfcc = np . append ( cep_lift_mfcc , c0 ) <EOL> else : <EOL> mfcc = cep_lift_mfcc <EOL> self . mfcc_queue . append ( mfcc ) <EOL> if self . usedelta : <EOL> if len ( self . mfcc_queue ) >= <NUM_LIT:2> : <EOL> delta = np . zeros_like ( mfcc ) <EOL> for i in range ( <NUM_LIT:1> , len ( self . mfcc_queue ) ) : <EOL> delta += self . mfcc_queue [ i ] - self . mfcc_queue [ i - <NUM_LIT:1> ] <EOL> delta /= len ( self . mfcc_queue ) - <NUM_LIT:1> <EOL> self . mfcc_delta_queue . append ( delta ) <EOL> else : <EOL> delta = np . zeros_like ( mfcc ) <EOL> if self . useacc : <EOL> if len ( self . mfcc_delta_queue ) >= <NUM_LIT:2> : <EOL> acc = np . zeros_like ( mfcc ) <EOL> for i in range ( <NUM_LIT:1> , len ( self . mfcc_delta_queue ) ) : <EOL> acc += self . mfcc_delta_queue [ i ] - self . mfcc_delta_queue [ i - <NUM_LIT:1> ] <EOL> acc /= len ( self . mfcc_delta_queue ) - <NUM_LIT:1> <EOL> else : <EOL> acc = np . zeros_like ( mfcc ) <EOL> if self . usedelta : <EOL> mfcc = np . append ( mfcc , delta ) <EOL> if self . useacc : <EOL> mfcc = np . append ( mfcc , acc ) <EOL> for i in range ( self . n_last_frames ) : <EOL> if len ( self . mfcc_queue ) > i + <NUM_LIT:1> : <EOL> mfcc = np . append ( mfcc , self . mfcc_queue [ - <NUM_LIT:1> - i - <NUM_LIT:1> ] ) <EOL> else : <EOL> mfcc = np . append ( mfcc , np . zeros_like ( self . mfcc_queue [ - <NUM_LIT:1> ] ) ) <EOL> return mfcc . astype ( np . float32 ) </s>
