1
.. currentmodule:: brian
4
pair: example usage; Sound
5
pair: example usage; MiddleEar
6
pair: example usage; sqrt
7
pair: example usage; set_default_samplerate
9
.. _example-hears-tan_carney_2003_tan_carney_Fig7:
11
Example: tan_carney_Fig7 (hears/tan_carney_2003)
12
================================================
14
CF-dependence of compressive nonlinearity in the Tan&Carney model.
15
Reproduces Fig. 7 from:
17
Tan, Q., and L. H. Carney.
18
"A Phenomenological Model for the Responses of Auditory-nerve Fibers.
19
II. Nonlinear Tuning with a Frequency Glide".
20
The Journal of the Acoustical Society of America 114 (2003): 2007.
28
import matplotlib.pyplot as plt
29
from scipy.interpolate import interp1d
32
#set_global_preferences(useweave=True)
33
from brian.hears import *
34
from brian.hears.filtering.tan_carney import TanCarneySignal, MiddleEar
37
set_default_samplerate(samplerate)
40
def gen_tone(freq, level):
42
Little helper function to generate a pure tone at frequency `freq` with
43
the given `level`. The tone has a duration of 50ms and is ramped with
46
freq = float(freq) * Hz
47
level = float(level) * dB
48
return tone(freq, duration).ramp(when='both',
50
inplace=False).atlevel(level)
52
freqs = [500, 1100, 2000, 4000]
53
levels = np.arange(-10, 100.1, 5)
54
cf_level = list(itertools.product(freqs, levels))
57
start = 10*ms*samplerate
58
end = 45*ms*samplerate
60
# For Figure 7 we have manually adjusts the gain for different CFs -- otherwise
61
# the RMS values wouldn't be identical for low CFs. Therefore, try to estimate
62
# suitable gain values first using the lowest CF as a reference
63
ref_tone = gen_tone(freqs[0], levels[0])
64
F_out_reference = TanCarneySignal(MiddleEar(ref_tone, gain=1), freqs[0],
65
update_interval=1).process().flatten()
67
ref_rms = np.sqrt(np.mean((F_out_reference[start:end] -
68
np.mean(F_out_reference[start:end]))**2))
70
gains = np.linspace(0.1, 1, 50) # for higher CFs we need lower gains
71
cf_gains = list(itertools.product(freqs[1:], gains))
72
tones = Sound([gen_tone(freq, levels[0]) for freq, _ in cf_gains])
73
F_out_test = TanCarneySignal(MiddleEar(tones, gain=np.array([g for _, g in cf_gains])),
74
[cf for cf,_ in cf_gains], update_interval=1).process()
76
reshaped_Fout = F_out_test.T.reshape((len(freqs[1:]), len(gains), -1))
77
rms = np.sqrt(np.mean((reshaped_Fout[:, :, start:end].T -
78
np.mean(reshaped_Fout[:, :, start:end], axis=2).T).T**2,
81
# get the best gain for each CF using simple linear interpolation
82
gain_dict = {freqs[0]: 1.} # reference gain
83
for idx, freq in enumerate(freqs[1:]):
84
gain_dict[freq] = interp1d(rms[idx, :], gains)(ref_rms)
86
# now do the real test: tones at different levels for different CFs
87
tones = Sound([gen_tone(freq, level) for freq, level in cf_level])
88
F_out = TanCarneySignal(MiddleEar(tones,
89
gain=np.array([gain_dict[cf] for cf, _ in cf_level])),
90
[cf for cf, _ in cf_level],
91
update_interval=1).process()
93
reshaped_Fout = F_out.T.reshape((len(freqs), len(levels), -1))
95
rms = np.sqrt(np.mean((reshaped_Fout[:, :, start:end].T -
96
np.mean(reshaped_Fout[:, :, start:end], axis=2).T).T**2,
99
# This should more or less reproduce Fig. 7
100
plt.plot(levels, rms.T)
101
plt.legend(['%.0f Hz' % cf for cf in freqs], 0)
105
plt.xlabel('input signal SPL (dB)')
106
plt.ylabel('rms of AC component of Fout')