In [42]:
%pylab
%reset
from brian import *
set_global_preferences(useweave=True)
#set_global_preferences(usegpu=True)
from brian.hears import *
Welcome to pylab, a matplotlib-based Python environment [backend: TkAgg].
For more information, type 'help(pylab)'.

Define inputs

In [43]:
duration = .1*second
samplerate = 44.1*kHz
sound1 = tone(1*kHz, duration,samplerate = samplerate)
sound2 = whitenoise(duration,samplerate = samplerate)
taxis = arange(0*ms,duration,1./samplerate)
sound = sound1+sound2
sound = sound.ramp()
#sound = loadsound('test.wav')

#sound = sound.extended(20*ms)
#sound = sound.repeat(5)
#sound.level = 50*dB

figure()
subplot(311)
plot(taxis,sound1)
subplot(312)
plot(taxis,sound2)
subplot(313)
plot(taxis,sound)
xlabel('time axis (s)')
show()
In [40]:
#sound = powerlawnoise(200*ms, 1, samplerate=44100*Hz)
#sound = whitenoise(duration,samplerate = samplerate)
sound = harmoniccomplex(1000*Hz,100*ms)
sound = sound.ramp(when='both', duration=10.0 * msecond, envelope=lambda t :sin(pi*t/2)**2)
#sound = sound.extended(20*ms)
#sound = sound.repeat(5)
sound.level = 50*dB
figure()
subplot(211)
sound.spectrogram()
subplot(212)
plot(sound)
show()

Simple cochlear model: gammatone+envelope extraction

In [44]:
cf = erbspace(20*Hz, 20*kHz, 3000)
basilar_membrane = Gammatone(sound, cf)
half_wave_rect = lambda x: 3*clip(x, 0, Inf)**(1.0/3.0)
hair_cell1 = FunctionFilterbank(basilar_membrane,half_wave_rect)
hair_cell = LowPass(hair_cell1, 800*Hz)

out_hair_cell = hair_cell.process()


imshow(out_hair_cell.T, origin='lower left', aspect='auto', vmin=0,extent=(0, sound.duration/ms,cf[0], cf[-1]))
ylabel('Frequency (Hz)')
xlabel('Time (ms)')
show()

Auditory nerve model

In [50]:
# Leaky integrate-and-fire model with noise and refractoriness
tau = 1*ms
eqs = '''
dv/dt = (I-v)/(tau)+0.2*xi*(2/(tau))**.5 : 1
I : 1
'''
anf = FilterbankGroup(hair_cell1, 'I', eqs, reset=0, threshold=1, refractory=2*ms)

M = SpikeMonitor(anf)
run(sound.duration)
figure()
raster_plot(M)
show()

Multiple paths

In [52]:
sound.level = 50*dB
linear_path = Gammatone(sound, cf)

bandpass_nonlinear1 = Gammatone(sound, cf+10)
func_compression = lambda x: sign(x)*abs(x)**0.3
non_linear_path = FunctionFilterbank(bandpass_nonlinear1, func_compression)

total_path = linear_path+non_linear_path
output = total_path.process()

figure()
imshow(output.T, origin='lower left', aspect='auto', vmin=0,extent=(0, sound.duration/ms,cf[0], cf[-1]))
ylabel('Frequency (Hz)')
xlabel('Time (ms)')
show()

Non-linear model

In [14]:
sound.level = 100*dB
cf = erbspace(20*Hz, 2*kHz, 300)
param_drnl = {'lp_nl_cutoff_m':1.1}
drnl_filter=DRNL(sound, cf, type='human', param=param_drnl)
output = drnl_filter.process()

imshow(output.T, origin='lower left', aspect='auto', vmin=0,extent=(0, sound.duration/ms,cf[0], cf[-1]))
ylabel('Frequency (Hz)')
xlabel('Time (ms)')
show()

Simulation parallelization

In [28]:
nsounds = 3
sound = Sound(randn(4000, nsounds), samplerate=16*kHz)

nchannels = 100
cf = erbspace(20*Hz, 10*kHz, nchannels)
cf = tile(cf,nsounds)

sound = Repeat(sound,nchannels)

gf = Gammatone(sound, cf)

output = gf.process()

imshow(output.T, origin='lower left', aspect='auto', vmin=0,extent=(0, sound.duration/ms,cf[0], cf[-1]))
ylabel('Frequency (Hz)')
xlabel('Time (ms)')
show()

Online computation

example: compute online rms value Filterbank.process() method allows us to pass an optional function f(input, running) process() will first call running = f(output, 0) for the first buffered segment input It will then call running = f(output, running) for each subsequent segment
In [32]:
def sum_of_squares(input, running):
    return running+sum(input**2, axis=0)
nsamples = 100
rms = sqrt(gf.process(sum_of_squares)/nsamples)
print rms
[ 0.33467137  0.34731127  0.35702791  0.37547593  0.36254372  0.37587322
  0.39852222  0.40815269  0.4389028   0.4911106   0.51064907  0.49710132
  0.47380814  0.46445007  0.46466203  0.49929454  0.54862494  0.55898672
  0.5752122   0.60992201  0.59089479  0.5404061   0.53967981  0.55858502
  0.5353092   0.49376489  0.4906964   0.54787497  0.59646792  0.63732329
  0.65482915  0.6469999   0.63371205  0.61718786  0.64791964  0.6655609
  0.64640589  0.62798376  0.63162654  0.67150314  0.74155884  0.81528201
  0.87081293  0.90772492  0.91379024  0.89190434  0.88272417  0.89824593
  0.93500465  0.96378526  0.97260845  0.93077832  0.95146536  1.02022723
  1.01864771  1.00584695  1.02714083  1.11223307  1.18132958  1.16860336
  1.146998    1.14923836  1.16983651  1.21454975  1.26306496  1.24085318
  1.20256552  1.22023293  1.28653174  1.36524927  1.39018886  1.38392026
  1.40920368  1.44293153  1.45825673  1.46672936  1.50396776  1.56428959
  1.60311642  1.63546316  1.68567169  1.70834346  1.74545301  1.75385568
  1.72873721  1.72420234  1.75140693  1.8364141   1.92449624  2.00266106
  2.07677917  2.05932543  2.03032761  1.55387449  1.96507517  2.11867222
  2.27925597  2.32001802  2.3033758   2.300264    0.31304189  0.30987501
  0.2899308   0.33192513  0.3931009   0.39746493  0.38820657  0.40633955
  0.44580906  0.44181702  0.41028399  0.37326548  0.3622783   0.39097973
  0.43192326  0.48004629  0.52278686  0.52115358  0.47107377  0.45627449
  0.46893035  0.51088308  0.5822393   0.61542467  0.58541029  0.57457202
  0.60138222  0.60956795  0.63805409  0.68808885  0.71908867  0.67809423
  0.63986839  0.69102225  0.72891999  0.71148832  0.72454715  0.73724841
  0.7437189   0.78477351  0.82724108  0.85374297  0.83609923  0.80215671
  0.81179297  0.85559068  0.86830223  0.86126508  0.88108944  0.90539074
  0.88234121  0.87009496  0.89639764  0.93421866  0.9774528   1.03975231
  1.09657621  1.14056232  1.19262833  1.2260151   1.18944064  1.14630392
  1.17890379  1.23622223  1.25943032  1.26220072  1.30652933  1.38245301
  1.41828352  1.42121872  1.42503128  1.41710194  1.41693833  1.45853238
  1.47027533  1.47103545  1.49668115  1.53632039  1.58688699  1.63982681
  1.676376    1.68152922  1.69176024  1.71787973  1.77064869  1.83961526
  1.86742861  1.8755557   1.89735775  1.97384241  2.05703649  2.05724091
  2.05934219  1.53216816  1.963355    2.12466622  2.26156112  2.30277613
  2.33264334  2.38415225  0.42445676  0.44748854  0.44947661  0.42614891
  0.39404003  0.36189457  0.40739658  0.48670006  0.46158171  0.40874758
  0.41862194  0.43696909  0.42438574  0.41926255  0.41704816  0.42178562
  0.43944655  0.4309789   0.44086017  0.46137589  0.47537579  0.51369384
  0.5722089   0.65501948  0.71561627  0.71765234  0.6704682   0.61623516
  0.58338753  0.59818958  0.64106608  0.63891027  0.63767192  0.66120206
  0.67722985  0.71593898  0.780824    0.77293866  0.7095516   0.69298419
  0.72162948  0.73707295  0.76141473  0.80198526  0.85468309  0.92423388
  0.95000827  0.92223493  0.90556056  0.91632055  0.95925089  0.98454791
  1.01621515  1.07907431  1.1384373   1.16239558  1.15894045  1.10145885
  1.04567837  1.09479058  1.18091766  1.24620628  1.25989537  1.21431
  1.15980517  1.1981926   1.30002018  1.3514977   1.36091639  1.38670769
  1.42339304  1.44476787  1.44961821  1.45977617  1.45232712  1.43979112
  1.45736245  1.50487938  1.56027105  1.5980582   1.61516112  1.62925135
  1.68411811  1.73422811  1.76971241  1.80137667  1.83424816  1.88167304
  1.93836406  2.01195526  2.08045389  2.05790367  2.01959237  1.57793102
  1.9762297   2.11371971  2.28582825  2.33770076  2.34785761  2.36682344]