def __init__(self, source,cf,update_interval,param={}): CombinedFilterbank.__init__(self, source) source = self.get_modified_source() parameters=set_parameters(cf,param) ERBspace = mean(diff(parameters['ERBrate'])) cf=atleast_1d(cf) #bank of passive gammachirp filters. As the control path uses the same passive filterbank than the signal path (buth shifted in frequency) #this filterbanl is used by both pathway. pGc=LogGammachirp(source,cf,b=parameters['b1'], c=parameters['c1']) fp1 = cf + parameters['c1']*parameters['ERBwidth']*parameters['b1']/parameters['order_gc'] nbr_cf=len(cf) #### Control Path #### n_ch_shift = round(parameters['lct_ERB']/ERBspace); #value of the shift in channels indch1_control = minimum(maximum(1, arange(1,nbr_cf+1)+n_ch_shift),nbr_cf).astype(int)-1 fp1_control = fp1[indch1_control] pGc_control=RestructureFilterbank(pGc,indexmapping=indch1_control) fr2_control = parameters['frat_control']*fp1_control asym_comp_control=AsymmetricCompensation(pGc_control, fr2_control,b=parameters['b2'], c=parameters['c2']) #### Signal Path #### fr1=fp1*parameters['frat0'] signal_path= AsymmetricCompensation(pGc, fr1,b=parameters['b2'], c=parameters['c2']) #### Controler #### updater = AsymCompUpdate(signal_path,source.samplerate,fp1,parameters) #the updater control = ControlFilterbank(signal_path, [pGc_control,asym_comp_control], signal_path, updater, update_interval) self.set_output(control)
def __init__(self, source, cf, type='human', param={}): CombinedFilterbank.__init__(self, source) source = self.get_modified_source() cf = atleast_1d(cf) nbr_cf=len(cf) parameters=set_parameters(cf,type,param) #conversion to stape velocity (which are the units needed for the further centres) source=source*parameters['stape_scale'] #### Linear Pathway #### #bandpass filter (second order gammatone filter) cf_linear=10**(parameters['cf_lin_p0']+parameters['cf_lin_m']*log10(cf)) bandwidth_linear=10**(parameters['bw_lin_p0']+parameters['bw_lin_m']*log10(cf)) gammatone=ApproximateGammatone(source, cf_linear, bandwidth_linear, order=parameters['order_linear']) #linear gain g=10**(parameters['g_p0']+parameters['g_m']*log10(cf)) func_gain=lambda x:g*x gain= FunctionFilterbank(gammatone,func_gain) #low pass filter(cascade of 4 second order lowpass butterworth filters) cutoff_frequencies_linear=10**(parameters['lp_lin_cutoff_p0']+parameters['lp_lin_cutoff_m']*log10(cf)) order_lowpass_linear=2 lp_l=LowPass(gain,cutoff_frequencies_linear) lowpass_linear=Cascade(gain,lp_l,4) #### Nonlinear Pathway #### #bandpass filter (third order gammatone filters) cf_nonlinear=10**(parameters['cf_nl_p0']+parameters['cf_nl_m']*log10(cf)) bandwidth_nonlinear=10**(parameters['bw_nl_p0']+parameters['bw_nl_m']*log10(cf)) bandpass_nonlinear1=ApproximateGammatone(source, cf_nonlinear, bandwidth_nonlinear, order=parameters['order_nonlinear']) #compression (linear at low level, compress at high level) a=10**(parameters['a_p0']+parameters['a_m']*log10(cf)) #linear gain b=10**(parameters['b_p0']+parameters['b_m']*log10(cf)) v=10**(parameters['c_p0']+parameters['c_m']*log10(cf))#compression exponent func_compression=lambda x:sign(x)*minimum(a*abs(x),b*abs(x)**v) compression=FunctionFilterbank(bandpass_nonlinear1, func_compression) #bandpass filter (third order gammatone filters) bandpass_nonlinear2=ApproximateGammatone(compression, cf_nonlinear, bandwidth_nonlinear, order=parameters['order_nonlinear']) #low pass filter cutoff_frequencies_nonlinear=10**(parameters['lp_nl_cutoff_p0']+parameters['lp_nl_cutoff_m']*log10(cf)) order_lowpass_nonlinear=2 lp_nl=LowPass(bandpass_nonlinear2,cutoff_frequencies_nonlinear) lowpass_nonlinear=Cascade(bandpass_nonlinear2,lp_nl,3) #adding the two pathways drnl_filter=lowpass_linear+lowpass_nonlinear self.set_output(drnl_filter)
def __init__(self, source, cf, update_interval=1, param={}): CombinedFilterbank.__init__(self, source) source = self.get_modified_source() parameters = set_parameters(cf, param) ERBspace = mean(diff(parameters['ERBrate'])) cf = atleast_1d(cf) #bank of passive gammachirp filters. As the control path uses the same passive filterbank than the signal path (buth shifted in frequency) #this filterbanl is used by both pathway. pGc = LogGammachirp(source, cf, b=parameters['b1'], c=parameters['c1']) # self.gc.filt_b=pGc.filt_b # self.gc.filt_a=pGc.filt_a fp1 = cf + parameters['c1'] * parameters['ERBwidth'] * parameters[ 'b1'] / parameters['order_gc'] nbr_cf = len(cf) #### Control Path #### n_ch_shift = round(parameters['lct_ERB'] / ERBspace) #value of the shift in channels indch1_control = minimum( maximum(1, arange(1, nbr_cf + 1) + n_ch_shift), nbr_cf).astype(int) - 1 fp1_control = fp1[indch1_control] pGc_control = RestructureFilterbank(pGc, indexmapping=indch1_control) fr2_control = parameters['frat_control'] * fp1_control asym_comp_control = AsymmetricCompensation(pGc_control, fr2_control, b=parameters['b2'], c=parameters['c2']) #### Signal Path #### fr1 = fp1 * parameters['frat0'] signal_path = AsymmetricCompensation(pGc, fr1, b=parameters['b2'], c=parameters['c2']) # self.asym_comp.filt_b=signal_path.filt_b # self.asym_comp.filt_a=signal_path.filt_a #### Controler #### updater = AsymCompUpdate(signal_path, source.samplerate, fp1, parameters) #the updater control = ControlFilterbank(signal_path, [pGc_control, asym_comp_control], signal_path, updater, update_interval) self.set_output(control)
def __init__(self, source,cf,update_interval,param={}): CombinedFilterbank.__init__(self, source) source = self.get_modified_source() cf = atleast_1d(cf) nbr_cf=len(cf) parameters=set_parameters(cf,param) if int(source.samplerate)!=50000: warnings.warn('To use the PMFR cochlear model the sample rate should be 50kHz') if not have_scikits_samplerate: raise ImportError('To use the PMFR cochlear model the sample rate should be 50kHz and scikits.samplerate package is needed for resampling') #source=source.resample(50*kHz) warnings.warn('The input to the PMFR cochlear model has been resampled to 50kHz') samplerate=source.samplerate ##### Control Path #### # wide band pass control #Initialisation of the filter coefficients of the bandpass filter x_cf=11.9*log10(0.8+cf/456) f_shift=(10**((x_cf+1.2)/11.9)-0.8)*456-cf filt_b=zeros((len(cf), 3, 3)) filt_a=zeros((len(cf), 3, 3)) poles=zeros((len(cf),3),dtype='complex') wbw=cf/4 poles[:,0:3]=tile(-2*pi*wbw+1j*2*pi*(cf+f_shift),[3,1]).T poles=(1+poles/(2*samplerate))/(1-poles/(2*samplerate)) #bilinear transform bfp=2*pi*cf zz=exp(1j*bfp/samplerate) gain_norm=abs((zz**2+2*zz+1)/(zz**2-zz*(poles[:,0]+conj(poles[:,0]))+poles[:,0]*conj(poles[:,0])))**3*2 filt_b[:,:,0]=vstack([ones(len(cf)),2*ones(len(cf)),ones(len(cf))]).T filt_b[:,:,1]=vstack([ones(len(cf)),2*ones(len(cf)),ones(len(cf))]).T filt_b[:,:,2]=vstack([ones(len(cf)),zeros(len(cf)),-ones(len(cf))]).T for iorder in xrange(3): filt_a[:,:,iorder]=vstack([ones(len(cf)),real(-(squeeze(poles[:,iorder])+conj(squeeze(poles[:,iorder])))),real(squeeze(poles[:,iorder])*conj(squeeze(poles[:,iorder])))]).T filt_b[:,:,2]=filt_b[:,:,2]/tile(gain_norm,[3,1]).T BP_control= LinearFilterbank(source,filt_b,filt_a) #bandpass filter instantiation (a controller will vary its coefficients) # first non linearity of control path Acp,Bcp,Ccp=100.,2.5,0.60 func_NL1_control=lambda x:sign(x)*Bcp*log(1.+Acp*abs(x)**Ccp) NL1_control=FunctionFilterbank(BP_control,func_NL1_control) # second non linearity of control path asym,s0,x1,s1=7.,8.,5.,3. shift = 1./(1.+asym) x0 = s0*log((1.0/shift-1)/(1+exp(x1/s1))) func_NL2_control=lambda x:(1.0/(1.0+exp(-(x-x0)/s0)*(1.0+exp(-(x-x1)/s1)))-shift)*parameters['nlgain'] NL2_control=FunctionFilterbank(NL1_control,func_NL2_control) #control low pass filter (its output will be used to control the signal path) LP_control=Butterworth(NL2_control, nbr_cf, 3, parameters['fc_LP_control'], btype='low') #low pass filter for feedback to control band pass (its output will be used to control the control path) LP_feed_back= Butterworth(LP_control, nbr_cf, 3,parameters['fc_LP_fb'], btype='low') #### Signal Path #### #Initisialisation of the filter coefficients of the bandpass filter x_cf=11.9*log10(0.8+cf/456) N=len(cf) f_shift=(10**((x_cf+1.2)/11.9)-0.8)*456-cf filt_b=zeros((len(cf), 3, 10)) filt_a=zeros((len(cf), 3, 10)) poles=zeros((len(cf),10),dtype='complex') a=-parameters['rgain']+1j*parameters['fp1']*2*pi poles[:,0:4]=tile(-parameters['rgain']+1j*parameters['fp1']*2*pi,[4,1]).T poles[:,4:8]=tile(real(poles[:,0])- parameters['ta']+1j*(imag(poles[:,0])- parameters['tb']),[4,1]).T poles[:,8:10]=tile((real(poles[:,0])+real(poles[:,4]))*.5+1j*(imag(poles[:,0])+imag(poles[:,4]))*.5,[2,1]).T zero_r=parameters['zero_r'] poles=(1+poles/(2*samplerate))/(1-poles/(2*samplerate)) zero_r=(1+zero_r/(2*samplerate))/(1-zero_r/(2*samplerate)) bfp=2*pi*cf gain_norm=1 zz=exp(1j*bfp/samplerate) for ii in xrange(10): gain_norm=gain_norm*abs((zz**2-zz*(-1+zero_r)-zero_r)/(zz**2-zz*(poles[:,ii]+conj(poles[:,ii]))+poles[:,ii]*conj(poles[:,ii]))) for iorder in xrange(10): filt_b[:,:,iorder]=vstack([ones(len(cf)),-(-1+zero_r),-zero_r]).T filt_a[:,:,iorder]=vstack([ones(N),real(-(squeeze(poles[:,iorder])+conj(squeeze(poles[:,iorder])))),real(squeeze(poles[:,iorder])*conj(squeeze(poles[:,iorder])))]).T filt_b[:,:,9]=filt_b[:,:,9]/tile(gain_norm,[3,1]).T signal_path= LinearFilterbank(source,filt_b,filt_a) #bandpass filter instantiation (a controller will vary its coefficients) #controlers definition updater_control_path=BP_control_update(BP_control,samplerate,cf,param) #instantiation of the updater for the control path control1 = ControlFilterbank(signal_path, LP_feed_back, BP_control,updater_control_path, update_interval) #controler for the band pass filter of the control path updater_signal_path=BP_signal_update(signal_path,samplerate,cf,param) #instantiation of the updater for the signal path control2 = ControlFilterbank(control1, LP_control, signal_path,updater_signal_path, update_interval) #controler for the band pass filter of the signal path self.set_output(control2)
def __init__(self, source, cf, update_interval, param={}): file = "/home/bertrand/Data/MatlabProg/brian_hears/ZilanyCarney-JASAcode-2009/wbout.mat" X = loadmat(file, struct_as_record=False) wbout = Sound(X['wbout'].flatten()) wbout.samplerate = 100 * kHz CombinedFilterbank.__init__(self, source) # source = self.get_modified_source() cf = atleast_1d(cf) nbr_cf = len(cf) samplerate = source.samplerate parameters = set_parameters(cf, param) # if int(source.samplerate)!=50000: # warnings.warn('To use the PMFR cochlear model the sample rate should be 50kHz') # if not have_scikits_samplerate: # raise ImportError('To use the PMFR cochlear model the sample rate should be 50kHz and scikits.samplerate package is needed for resampling') # #source=source.resample(50*kHz) # warnings.warn('The input to the PMFR cochlear model has been resampled to 50kHz') cohc = 1 # ohc scaling factor: 1 is normal OHC function; 0 is complete OHC dysfunction cihc = 1 # i ihc scaling factor: 1 is normal IHC function; 0 is complete IHC dysfunction bmplace = 11.9 * log10(0.80 + cf / 456.0) centerfreq = 456.0 * (pow(10, (bmplace + 1.2) / 11.9) - 0.80) CAgain = minimum( 60, maximum(15 * ones(len(cf)), 52 / 2 * (tanh(2.2 * log10(cf / 600) + 0.15) + 1))) # Parameters for the control-path wideband filter =======*/ bmorder = 3 Taumax, Taumin = get_tauwb(cf, CAgain, bmorder) taubm = cohc * (Taumax - Taumin) + Taumin ratiowb = Taumin / Taumax #====== Parameters for the signal-path C1 filter ======*/ bmTaumax, bmTaumin, ratiobm = get_taubm(cf, CAgain, Taumax) bmTaubm = cohc * (bmTaumax - bmTaumin) + bmTaumin fcohc = bmTaumax / bmTaubm # Parameters for the control-path wideband filter =======*/ wborder = 3 TauWBMax = Taumin + 0.2 * (Taumax - Taumin) TauWBMin = TauWBMax / Taumax * Taumin tauwb = TauWBMax + (bmTaubm - bmTaumax) * (TauWBMax - TauWBMin) / ( bmTaumax - bmTaumin) [wbgain, grdelay] = gain_groupdelay(1. / self.samplerate, centerfreq, cf, tauwb) # Nonlinear asymmetry of OHC function and IHC C1 transduction function*/ ohcasym = 7.0 ihcasym = 3.0 # ##### Control Path #### # print tauwb*pi*centerfreq # gt_control = BiQuadratic(source, centerfreq,tauwb*2*pi*centerfreq) gt_control = ApproximateGammatone(source, centerfreq, 1. / (2 * pi * tauwb), order=3) # gt_control = LinearGammachirp(source, centerfreq,tauwb, c=0) # gt_control = Gammatone(source, centerfreq, b=1./(pi*tauwb)) # def wb_gain(x,cf=array([1000]),tauwb=array([.0003]),TauWBMax=array([.0003]),wborder=3): # print x.shape,cf.shape # out=zeros_like(x) # for ix in xrange(len(x)): # out[ix,:] = (tauwb/TauWBMax)**wborder*x[ix,:]*10e3*maximum(ones(len(cf)),cf/5e3) # return out # gt_control1 = FunctionFilterbank(gt_control,wb_gain,cf=cf,tauwb=tauwb,TauWBMax=TauWBMax,wborder=wborder) max_temp = (tauwb / TauWBMax)**wborder * 10e3 * maximum( ones(len(cf)), cf / 5e3) gt_control1 = FunctionFilterbank(gt_control, lambda x: x * max_temp) # first non linearity of control path NL1_control = FunctionFilterbank(gt_control1, boltzman, asym=ohcasym, s0=12.0, s1=5.0, x1=5.0) # #control low pass filter (its output will be used to control the signal path) LP_control = LowPass_filter(NL1_control, cf, 600, 1.0, 2) # # second non linearity of control path NL2_control = FunctionFilterbank(LP_control, OHC_transduction, taumin=bmTaumin, taumax=bmTaumax, asym=ohcasym) # # #### C1 #### rsigma = zeros(len(cf)) c1_coefficients = Chirp_Coefficients(cf, bmTaumax, samplerate, rsigma, 1 * ones(len(cf))) [filt_b, filt_a] = c1_coefficients.return_coefficients() C1_filter = LinearFilterbank(source, filt_b, filt_a) C1_IHC = FunctionFilterbank(C1_filter, IHC_transduction, slope=0.1, asym=ihcasym, sign=1) #### C2 #### c2_coefficients = Chirp_Coefficients(cf, bmTaumax, samplerate, 0 * ones(len(cf)), 1. / ratiobm) [filt_b, filt_a] = c2_coefficients.return_coefficients() C2_filter = LinearFilterbank(source, filt_b, filt_a) gain_temp = cf**2 / 2e4 C2_IHC_pre = FunctionFilterbank(C2_filter, lambda x: x * abs(x) * gain_temp) C2_IHC = FunctionFilterbank(C2_IHC_pre, IHC_transduction, slope=0.2, asym=1.0, sign=-1) # C_IHC = C1_IHC + C2_IHC # C_IHC_lp = LowPass_filter(C_IHC, cf, 3000, 1.0, 7) #controlers definition updater = Filter_Update( [C1_filter], c1_coefficients, samplerate, cf, bmTaumax, bmTaumin, cohc, TauWBMax, TauWBMin) #instantiation of the updater for the control path output = ControlFilterbank( C1_filter, NL2_control, [C1_filter], updater, update_interval ) #controler for the band pass filter of the control path self.set_output(output)
def __init__(self, source,cf,update_interval,param={}): file="/home/bertrand/Data/MatlabProg/brian_hears/ZilanyCarney-JASAcode-2009/wbout.mat" X=loadmat(file,struct_as_record=False) wbout = Sound(X['wbout'].flatten()) wbout.samplerate = 100*kHz CombinedFilterbank.__init__(self, source) # source = self.get_modified_source() cf = atleast_1d(cf) nbr_cf=len(cf) samplerate=source.samplerate parameters=set_parameters(cf,param) # if int(source.samplerate)!=50000: # warnings.warn('To use the PMFR cochlear model the sample rate should be 50kHz') # if not have_scikits_samplerate: # raise ImportError('To use the PMFR cochlear model the sample rate should be 50kHz and scikits.samplerate package is needed for resampling') # #source=source.resample(50*kHz) # warnings.warn('The input to the PMFR cochlear model has been resampled to 50kHz') cohc =1 # ohc scaling factor: 1 is normal OHC function; 0 is complete OHC dysfunction cihc = 1 # i ihc scaling factor: 1 is normal IHC function; 0 is complete IHC dysfunction bmplace = 11.9 * log10(0.80 + cf / 456.0) centerfreq = 456.0*(pow(10,(bmplace+1.2)/11.9)-0.80) CAgain = minimum(60,maximum(15*ones(len(cf)),52/2*(tanh(2.2*log10(cf/600)+0.15)+1))) # Parameters for the control-path wideband filter =======*/ bmorder = 3; Taumax,Taumin = get_tauwb(cf,CAgain,bmorder) taubm = cohc*(Taumax-Taumin)+Taumin; ratiowb = Taumin/Taumax; #====== Parameters for the signal-path C1 filter ======*/ bmTaumax,bmTaumin,ratiobm = get_taubm(cf,CAgain,Taumax) bmTaubm = cohc*(bmTaumax-bmTaumin)+bmTaumin; fcohc = bmTaumax/bmTaubm; # Parameters for the control-path wideband filter =======*/ wborder = 3 TauWBMax = Taumin+0.2*(Taumax-Taumin); TauWBMin = TauWBMax/Taumax*Taumin; tauwb = TauWBMax+(bmTaubm-bmTaumax)*(TauWBMax-TauWBMin)/(bmTaumax-bmTaumin); [wbgain,grdelay] = gain_groupdelay(1./self.samplerate,centerfreq,cf,tauwb) # Nonlinear asymmetry of OHC function and IHC C1 transduction function*/ ohcasym = 7.0 ihcasym = 3.0 # ##### Control Path #### # print tauwb*pi*centerfreq # gt_control = BiQuadratic(source, centerfreq,tauwb*2*pi*centerfreq) gt_control = ApproximateGammatone(source, centerfreq, 1./(2*pi*tauwb), order=3) # gt_control = LinearGammachirp(source, centerfreq,tauwb, c=0) # gt_control = Gammatone(source, centerfreq, b=1./(pi*tauwb)) # def wb_gain(x,cf=array([1000]),tauwb=array([.0003]),TauWBMax=array([.0003]),wborder=3): # print x.shape,cf.shape # out=zeros_like(x) # for ix in xrange(len(x)): # out[ix,:] = (tauwb/TauWBMax)**wborder*x[ix,:]*10e3*maximum(ones(len(cf)),cf/5e3) # return out # gt_control1 = FunctionFilterbank(gt_control,wb_gain,cf=cf,tauwb=tauwb,TauWBMax=TauWBMax,wborder=wborder) max_temp = (tauwb/TauWBMax)**wborder*10e3*maximum(ones(len(cf)),cf/5e3) gt_control1 = FunctionFilterbank(gt_control,lambda x:x*max_temp) # first non linearity of control path NL1_control=FunctionFilterbank(gt_control1,boltzman,asym=ohcasym,s0=12.0,s1=5.0,x1=5.0) # #control low pass filter (its output will be used to control the signal path) LP_control = LowPass_filter(NL1_control,cf,600,1.0,2) # # second non linearity of control path NL2_control=FunctionFilterbank(LP_control,OHC_transduction,taumin=bmTaumin, taumax=bmTaumax, asym=ohcasym) # # #### C1 #### rsigma = zeros(len(cf)) c1_coefficients = Chirp_Coefficients(cf, bmTaumax, samplerate, rsigma,1*ones(len(cf))) [filt_b,filt_a] = c1_coefficients.return_coefficients() C1_filter = LinearFilterbank(source,filt_b,filt_a) C1_IHC = FunctionFilterbank(C1_filter,IHC_transduction,slope = 0.1,asym = ihcasym,sign=1) #### C2 #### c2_coefficients = Chirp_Coefficients(cf, bmTaumax, samplerate, 0*ones(len(cf)),1./ratiobm) [filt_b,filt_a] = c2_coefficients.return_coefficients() C2_filter = LinearFilterbank(source,filt_b,filt_a) gain_temp = cf**2/2e4 C2_IHC_pre = FunctionFilterbank(C2_filter,lambda x:x*abs(x)*gain_temp) C2_IHC = FunctionFilterbank(C2_IHC_pre,IHC_transduction,slope = 0.2,asym = 1.0,sign=-1) # C_IHC = C1_IHC + C2_IHC # C_IHC_lp = LowPass_filter(C_IHC,cf,3000,1.0,7) #controlers definition updater=Filter_Update([C1_filter],c1_coefficients,samplerate,cf,bmTaumax,bmTaumin,cohc,TauWBMax,TauWBMin) #instantiation of the updater for the control path output = ControlFilterbank(C1_filter, NL2_control, [C1_filter],updater, update_interval) #controler for the band pass filter of the control path self.set_output(output)