<s> from _base import * <EOL> from cqt import CNTPowerSpectrum , A0 , A1 , A2 , C8 , A8 <EOL> import numpy as np <EOL> import scipy . signal as sig <EOL> inf = float ( '<STR_LIT>' ) <EOL> class GammatoneSpectrum ( SpectrumBase ) : <EOL> @ staticmethod <EOL> def erb_space ( N , freq_base , freq_max ) : <EOL> EarQ = <NUM_LIT> <EOL> minBW = <NUM_LIT> <EOL> qw = EarQ * minBW <EOL> arr = np . arange ( <NUM_LIT:1.> , N + <NUM_LIT:1> , <NUM_LIT:1.> , dtype = np . double ) <EOL> f_cents = np . exp ( arr * ( np . log ( freq_base + qw ) - np . log ( freq_max + qw ) ) / N ) * ( freq_max + qw ) - qw <EOL> return f_cents <EOL> @ staticmethod <EOL> def make_erb_filter_coeffiences ( samplerate , N = <NUM_LIT:64> , freq_base = A2 , freq_max = C8 ) : <EOL> fc = GammatoneSpectrum . erb_space ( N , freq_base , freq_max ) <EOL> pi = np . pi <EOL> sin = np . sin <EOL> cos = np . cos <EOL> exp = np . exp <EOL> sqrt = np . sqrt <EOL> EarQ = <NUM_LIT> <EOL> minBW = <NUM_LIT> <EOL> order = <NUM_LIT:1.> <EOL> ERB = <NUM_LIT> * fc + minBW <EOL> BW = <NUM_LIT> * <NUM_LIT:2> * pi * ERB <EOL> T = <NUM_LIT:1.> / samplerate <EOL> fct = np . flipud ( fc * T ) <EOL> bwt = np . flipud ( BW * T ) <EOL> a0 = T <EOL> a2 = <NUM_LIT:0> <EOL> b0 = <NUM_LIT:1> <EOL> b1 = - <NUM_LIT:2> * cos ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) <EOL> b2 = exp ( - <NUM_LIT:2> * bwt ) <EOL> a11 = - ( <NUM_LIT:2> * T * cos ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) + <NUM_LIT:2> * T * sqrt ( <NUM_LIT:3> + <NUM_LIT:2> ** <NUM_LIT> ) * sin ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) ) / <NUM_LIT:2> <EOL> a12 = - ( <NUM_LIT:2> * T * cos ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) - <NUM_LIT:2> * T * sqrt ( <NUM_LIT:3> + <NUM_LIT:2> ** <NUM_LIT> ) * sin ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) ) / <NUM_LIT:2> <EOL> a13 = - ( <NUM_LIT:2> * T * cos ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) + <NUM_LIT:2> * T * sqrt ( <NUM_LIT:3> - <NUM_LIT:2> ** <NUM_LIT> ) * sin ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) ) / <NUM_LIT:2> <EOL> a14 = - ( <NUM_LIT:2> * T * cos ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) - <NUM_LIT:2> * T * sqrt ( <NUM_LIT:3> - <NUM_LIT:2> ** <NUM_LIT> ) * sin ( <NUM_LIT:2> * pi * fct ) / exp ( bwt ) ) / <NUM_LIT:2> <EOL> gain = np . abs ( <EOL> ( - <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct ) * T + <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct - bwt ) * T * ( cos ( <NUM_LIT:2> * pi * fct ) - sqrt ( <NUM_LIT:3> - <NUM_LIT:2> ** <NUM_LIT> ) * sin ( <NUM_LIT:2> * pi * fct ) ) ) * ( - <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct ) * T + <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct - bwt ) * T * ( cos ( <NUM_LIT:2> * pi * fct ) + sqrt ( <NUM_LIT:3> - <NUM_LIT:2> ** <NUM_LIT> ) * sin ( <NUM_LIT:2> * pi * fct ) ) ) * ( - <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct ) * T + <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct - bwt ) * T * ( cos ( <NUM_LIT:2> * pi * fct ) - sqrt ( <NUM_LIT:3> + <NUM_LIT:2> ** <NUM_LIT> ) * sin ( <NUM_LIT:2> * pi * fct ) ) ) * ( - <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct ) * T + <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct - bwt ) * T * ( cos ( <NUM_LIT:2> * pi * fct ) + sqrt ( <NUM_LIT:3> + <NUM_LIT:2> ** <NUM_LIT> ) * sin ( <NUM_LIT:2> * pi * fct ) ) ) / ( - <NUM_LIT:2> / exp ( <NUM_LIT:2> * bwt ) - <NUM_LIT:2> * exp ( <NUM_LIT> * pi * fct ) + <NUM_LIT:2> * ( <NUM_LIT:1> + exp ( <NUM_LIT> * pi * fct ) ) / exp ( bwt ) ) ** <NUM_LIT:4> ) <EOL> coeffies = np . zeros ( ( <NUM_LIT:5> , N , <NUM_LIT:3> ) ) <EOL> coeffies [ <NUM_LIT:0> ] [ ... , <NUM_LIT:0> ] , coeffies [ <NUM_LIT:0> ] [ ... , <NUM_LIT:1> ] , coeffies [ <NUM_LIT:0> ] [ ... , <NUM_LIT:2> ] = b0 , b1 , b2 <EOL> coeffies [ <NUM_LIT:1> : , : , <NUM_LIT:0> ] , coeffies [ <NUM_LIT:1> : , : , <NUM_LIT:2> ] = a0 , a2 <EOL> for i , p1 in enumerate ( [ a11 , a12 , a13 , a14 ] ) : <EOL> coeffies [ i + <NUM_LIT:1> , : , <NUM_LIT:1> ] = p1 <EOL> coeffies [ <NUM_LIT:1> , : , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ] /= gain <EOL> return coeffies <EOL> @ staticmethod <EOL> def filter ( samples , coefficiences , zi = None ) : <EOL> n_filters = coefficiences . shape [ <NUM_LIT:1> ] <EOL> if zi is None : <EOL> zi = np . zeros ( ( <NUM_LIT:4> , n_filters , coefficiences . shape [ - <NUM_LIT:1> ] - <NUM_LIT:1> ) ) <EOL> mtx = numpy . zeros ( ( len ( samples ) , n_filters ) ) <EOL> for i in range ( n_filters ) : <EOL> b = coefficiences [ <NUM_LIT:0> , i ] <EOL> a1 , a2 , a3 , a4 = coefficiences [ <NUM_LIT:1> : , i ] <EOL> s1 , zi [ <NUM_LIT:0> , i ] = sig . lfilter ( a1 , b , samples , zi = zi [ <NUM_LIT:0> , i ] ) <EOL> s2 , zi [ <NUM_LIT:1> , i ] = sig . lfilter ( a2 , b , s1 , zi = zi [ <NUM_LIT:1> , i ] ) <EOL> s3 , zi [ <NUM_LIT:2> , i ] = sig . lfilter ( a3 , b , s2 , zi = zi [ <NUM_LIT:2> , i ] ) <EOL> s4 , zi [ <NUM_LIT:3> , i ] = sig . lfilter ( a4 , b , s3 , zi = zi [ <NUM_LIT:3> , i ] ) <EOL> mtx [ ... , i ] = s4 <EOL> return mtx , zi <EOL> def walk ( self , N = <NUM_LIT:64> , freq_base = A2 , freq_max = C8 , start = <NUM_LIT:0> , end = None , each = True , combine = False , twin = <NUM_LIT> , thop = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> N = int ( N ) <EOL> assert N > <NUM_LIT:0> <EOL> samplerate = self . audio . samplerate <EOL> assert <NUM_LIT:1> <= freq_base <= freq_max <= samplerate / <NUM_LIT> <EOL> step = <NUM_LIT> <EOL> win = step <EOL> assert <NUM_LIT:0> < step <= win <EOL> coeffies = self . make_erb_filter_coeffiences ( samplerate , N , freq_base , freq_max ) <EOL> zi = None <EOL> cstep = int ( np . ceil ( thop * samplerate ) ) <EOL> cwin = int ( np . ceil ( twin * samplerate ) ) <EOL> Y = np . zeros ( ( <NUM_LIT:0> , N ) ) <EOL> if combine : <EOL> assert <NUM_LIT:0> < thop <= twin <EOL> assert <NUM_LIT:0> < cstep <= cwin <EOL> for samples in self . audio . walk ( win , step , start , end , join_channels = True ) : <EOL> y , zi = self . filter ( samples , coeffies , zi ) <EOL> if not combine : <EOL> if each : <EOL> for frame in y : yield frame <EOL> else : <EOL> yield y <EOL> else : <EOL> Y = np . append ( Y , y , <NUM_LIT:0> ) <EOL> while Y . shape [ <NUM_LIT:0> ] >= cwin : <EOL> wf , Y = Y [ : cwin ] , Y [ cstep : ] <EOL> yield np . sqrt ( np . mean ( np . square ( wf ) , <NUM_LIT:0> ) ) <EOL> if combine and Y . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> yield np . sqrt ( np . mean ( np . square ( Y ) , <NUM_LIT:0> ) ) <EOL> class Y1 ( GammatoneSpectrum ) : <EOL> pass <EOL> class Y2 ( Y1 ) : <EOL> @ staticmethod <EOL> def g ( y , gamma = <NUM_LIT:10> ) : <EOL> if not gamma or gamma >= inf : <EOL> return <NUM_LIT:0.5> <EOL> return <NUM_LIT:1.> / ( <NUM_LIT:1> + np . exp ( - gamma * y ) ) - <NUM_LIT:0.5> <EOL> def walk ( self , N = <NUM_LIT:64> , freq_base = A2 , freq_max = C8 , start = <NUM_LIT:0> , end = None , each = True , combine = False , twin = <NUM_LIT> , thop = <NUM_LIT> , gamma = <NUM_LIT:10> , cof = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> samplerate = self . audio . samplerate <EOL> level = <NUM_LIT:6> <EOL> vpre = np . zeros ( ( <NUM_LIT:1> , N ) ) <EOL> ncof = min ( <NUM_LIT> , float ( cof ) * <NUM_LIT:2> / samplerate ) <EOL> b , a = sig . butter ( <NUM_LIT:6> , ncof ) <EOL> zi = np . zeros ( ( N , level ) ) <EOL> cstep = int ( np . ceil ( thop * samplerate ) ) <EOL> cwin = int ( np . ceil ( twin * samplerate ) ) <EOL> Y = np . zeros ( ( <NUM_LIT:0> , N ) ) <EOL> if combine : <EOL> assert <NUM_LIT:0> < thop <= twin <EOL> assert <NUM_LIT:0> < cstep <= cwin <EOL> for y in super ( Y2 , self ) . walk ( N , freq_base , freq_max , start , end , each = False , combine = False ) : <EOL> yg = self . g ( np . diff ( np . append ( vpre , y , <NUM_LIT:0> ) , <NUM_LIT:1> , <NUM_LIT:0> ) , gamma ) <EOL> ygt , zi = sig . lfilter ( b , a , yg . T , zi = zi ) <EOL> if not combine : <EOL> if each : <EOL> for v in ygt . T : yield v <EOL> else : <EOL> yield ygt . T <EOL> else : <EOL> Y = np . append ( Y , ygt . T , <NUM_LIT:0> ) <EOL> while Y . shape [ <NUM_LIT:0> ] >= cwin : <EOL> wf , Y = Y [ : cwin ] , Y [ cstep : ] <EOL> yield np . sqrt ( np . mean ( np . square ( wf ) , <NUM_LIT:0> ) ) <EOL> if combine and Y . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> yield np . sqrt ( np . mean ( np . square ( Y ) , <NUM_LIT:0> ) ) <EOL> class Y3 ( Y2 ) : <EOL> def walk ( self , N = <NUM_LIT:64> , freq_base = A2 , freq_max = C8 , start = <NUM_LIT:0> , end = None , each = True , combine = False , twin = <NUM_LIT> , thop = <NUM_LIT> , gamma = <NUM_LIT:10> , cof = <NUM_LIT> ) : <EOL> samplerate = self . audio . samplerate <EOL> cstep = int ( np . ceil ( thop * samplerate ) ) <EOL> cwin = int ( np . ceil ( twin * samplerate ) ) <EOL> Y = np . zeros ( ( <NUM_LIT:0> , N ) ) <EOL> if combine : <EOL> assert <NUM_LIT:0> < thop <= twin <EOL> assert <NUM_LIT:0> < cstep <= cwin <EOL> for y in super ( Y3 , self ) . walk ( N , freq_base , freq_max , start , end , each = False , combine = False , gamma = gamma , cof = cof ) : <EOL> y [ ... , <NUM_LIT:1> : ] = np . diff ( y , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> if not combine : <EOL> if each : <EOL> for v in y : yield v <EOL> else : <EOL> yield y <EOL> else : <EOL> Y = np . append ( Y , y , <NUM_LIT:0> ) <EOL> while Y . shape [ <NUM_LIT:0> ] >= cwin : <EOL> wf , Y = Y [ : cwin ] , Y [ cstep : ] <EOL> yield np . sqrt ( np . mean ( np . square ( wf ) , <NUM_LIT:0> ) ) <EOL> if combine and Y . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> yield np . sqrt ( np . mean ( np . square ( Y ) , <NUM_LIT:0> ) ) <EOL> class Y4 ( Y3 ) : <EOL> def walk ( self , N = <NUM_LIT:64> , freq_base = A2 , freq_max = C8 , start = <NUM_LIT:0> , end = None , each = True , combine = False , twin = <NUM_LIT> , thop = <NUM_LIT> , gamma = <NUM_LIT:10> , cof = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> samplerate = self . audio . samplerate <EOL> cstep = int ( np . ceil ( thop * samplerate ) ) <EOL> cwin = int ( np . ceil ( twin * samplerate ) ) <EOL> Y = np . zeros ( ( <NUM_LIT:0> , N ) ) <EOL> if combine : <EOL> assert <NUM_LIT:0> < thop <= twin <EOL> assert <NUM_LIT:0> < cstep <= cwin <EOL> for y in super ( Y4 , self ) . walk ( N , freq_base , freq_max , start , end , each = False , combine = False , gamma = gamma , cof = cof ) : <EOL> y = np . maximum ( <NUM_LIT:0> , y ) <EOL> if not combine : <EOL> if each : <EOL> for v in y : yield v <EOL> else : yield y <EOL> else : <EOL> Y = np . append ( Y , y , <NUM_LIT:0> ) <EOL> while Y . shape [ <NUM_LIT:0> ] >= cwin : <EOL> wf , Y = Y [ : cwin ] , Y [ cstep : ] <EOL> yield np . sqrt ( np . mean ( np . square ( wf ) , <NUM_LIT:0> ) ) <EOL> if combine and Y . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> yield np . sqrt ( np . mean ( np . square ( Y ) , <NUM_LIT:0> ) ) <EOL> class Y5 ( Y4 ) : <EOL> @ staticmethod <EOL> def poisson ( tinteg , tinv , tconst ) : <EOL> w = np . arange ( <NUM_LIT:0.> , tinteg + tinv , tinv , dtype = np . double ) <EOL> return np . exp ( - np . flipud ( w ) / tconst ) <EOL> @ staticmethod <EOL> def rectangle ( tinteg , tinv ) : <EOL> w = np . ones ( int ( np . ceil ( tinteg / tinv ) ) , dtype = np . double ) <EOL> return w <EOL> def walk ( self , N = <NUM_LIT:64> , freq_base = A2 , freq_max = C8 , start = <NUM_LIT:0> , end = None , each = True , combine = False , twin = <NUM_LIT> , thop = <NUM_LIT> , gamma = <NUM_LIT:10> , cof = <NUM_LIT> , tinteg = <NUM_LIT> , tconst = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> samplerate = self . audio . samplerate <EOL> tinv = <NUM_LIT:1.> / samplerate <EOL> pwin = self . poisson ( tinteg , tinv , tconst ) <EOL> pwin /= len ( pwin ) <EOL> lenpre = len ( pwin ) - <NUM_LIT:1> <EOL> ypre = np . zeros ( ( lenpre , N ) ) <EOL> cstep = int ( np . ceil ( thop * samplerate ) ) <EOL> cwin = int ( np . ceil ( twin * samplerate ) ) <EOL> Y = np . zeros ( ( <NUM_LIT:0> , N ) ) <EOL> if combine : <EOL> assert <NUM_LIT:0> < thop <= twin <EOL> assert <NUM_LIT:0> < cstep <= cwin <EOL> for y in super ( Y5 , self ) . walk ( N , freq_base , freq_max , start , end , each = False , combine = False , gamma = gamma , cof = cof ) : <EOL> ym = np . append ( ypre , y , <NUM_LIT:0> ) <EOL> ypre = ym [ - lenpre : ] <EOL> for i , yband in enumerate ( ym . T ) : <EOL> y [ ... , i ] = np . sqrt ( np . convolve ( np . square ( yband ) , pwin , '<STR_LIT>' ) ) <EOL> if not combine : <EOL> if each : <EOL> for v in y : yield v <EOL> else : yield y <EOL> else : <EOL> Y = np . append ( Y , y , <NUM_LIT:0> ) <EOL> while Y . shape [ <NUM_LIT:0> ] >= cwin : <EOL> wf , Y = Y [ : cwin ] , Y [ cstep : ] <EOL> yield np . sqrt ( np . mean ( np . square ( wf ) , <NUM_LIT:0> ) ) <EOL> if combine and Y . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> yield np . sqrt ( np . mean ( np . square ( Y ) , <NUM_LIT:0> ) ) </s>
