~ubuntu-branches/ubuntu/wily/brian/wily

« back to all changes in this revision

Viewing changes to docs_sphinx/examples-hears-tan_carney_2003_tan_carney_Fig7.txt

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2014-07-30 11:29:44 UTC
  • mfrom: (6.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20140730112944-ln0ogbq0kpyyuz47
Tags: 1.4.1-2
* Forgotten upload to unstable
* debian/control
  - policy boost to 3.9.5
  - updated Vcs- fields given migration to anonscm.d.o and provided -b
    debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. currentmodule:: brian
 
2
 
 
3
.. index::
 
4
   pair: example usage; Sound
 
5
   pair: example usage; MiddleEar
 
6
   pair: example usage; sqrt
 
7
   pair: example usage; set_default_samplerate
 
8
 
 
9
.. _example-hears-tan_carney_2003_tan_carney_Fig7:
 
10
 
 
11
Example: tan_carney_Fig7 (hears/tan_carney_2003)
 
12
================================================
 
13
 
 
14
CF-dependence of compressive nonlinearity in the Tan&Carney model.
 
15
Reproduces Fig. 7 from:
 
16
 
 
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.
 
21
 
 
22
::
 
23
 
 
24
    
 
25
    import itertools
 
26
    
 
27
    import numpy as np
 
28
    import matplotlib.pyplot as plt
 
29
    from scipy.interpolate import interp1d
 
30
    
 
31
    from brian import *
 
32
    #set_global_preferences(useweave=True)
 
33
    from brian.hears import *
 
34
    from brian.hears.filtering.tan_carney import TanCarneySignal, MiddleEar
 
35
    
 
36
    samplerate = 50*kHz
 
37
    set_default_samplerate(samplerate)
 
38
    duration = 50*ms
 
39
    
 
40
    def gen_tone(freq, level):
 
41
        ''' 
 
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
 
44
        two ramps of 2.5ms.
 
45
        '''
 
46
        freq = float(freq) * Hz
 
47
        level = float(level) * dB    
 
48
        return tone(freq, duration).ramp(when='both',
 
49
                                         duration=2.5*ms,
 
50
                                         inplace=False).atlevel(level)
 
51
    
 
52
    freqs = [500, 1100, 2000, 4000]
 
53
    levels = np.arange(-10, 100.1, 5)
 
54
    cf_level = list(itertools.product(freqs, levels))
 
55
    
 
56
    # steady-state
 
57
    start = 10*ms*samplerate
 
58
    end = 45*ms*samplerate
 
59
    
 
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()
 
66
    
 
67
    ref_rms = np.sqrt(np.mean((F_out_reference[start:end] -
 
68
                               np.mean(F_out_reference[start:end]))**2))
 
69
    
 
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()
 
75
    
 
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,
 
79
                           axis=2))
 
80
    
 
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)
 
85
    
 
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()
 
92
    
 
93
    reshaped_Fout = F_out.T.reshape((len(freqs), len(levels), -1))
 
94
    
 
95
    rms = np.sqrt(np.mean((reshaped_Fout[:, :, start:end].T -
 
96
                          np.mean(reshaped_Fout[:, :, start:end], axis=2).T).T**2,
 
97
                          axis=2))
 
98
    
 
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)
 
102
    plt.xlim(-20, 100)
 
103
    plt.ylim(1e-6, 1)
 
104
    plt.yscale('log')
 
105
    plt.xlabel('input signal SPL (dB)')
 
106
    plt.ylabel('rms of AC component of Fout')
 
107
    plt.show()
 
108
    
 
109