-
Notifications
You must be signed in to change notification settings - Fork 0
/
stimuli.py
256 lines (227 loc) · 9.56 KB
/
stimuli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
'''
Sets of stimuli should just be iterable objects of pairs:
(sound, location, meta)
Where sound is a single channel Sound object, location is an index into
an HRTFSet of locations, and meta is metadata about the stimulus (can be
None if there is no metadata to store). Metadata could include, for example,
the name of the sound file, etc.
Stimuli should in most cases try to include the following meta data:
time=time.gmtime()
type: a string identifying the stimuli generation algorithm
keywords specific to type that give the arguments to the stimuli generation
duration
level
tag: an optional value that can be passed to the stimuli generator
'''
from shared import *
class Stimuli(object):
'''
Use this to pass stimuli to FixedModel.parallel_apply_stimuli because
iterable objects are not picklable.
For example:
stimuli = Stimuli(whitenoise_stimuli, hrtfset, duration=50*ms)
'''
def __init__(self, func, *args, **kwds):
self.func = func
self.args = args
self.kwds = kwds
def __call__(self):
return self.func(*self.args, **self.kwds)
def whitenoise_stimuli(space, duration=100*ms, level=70*dB, tag=None):
'''
Note that level can be a single number or a tuple (min, max) in which case
levels will be selected uniformly between the two.
The meta information is a dictionary with keys duration, level, tag,
type='whitenoise', level_range and time=time.gmtime().
'''
if isinstance(level, tuple):
level_range = level
else:
level_range = (level, level)
while True:
location = space.get_random_itd()
level = rand()*(level_range[1]-level_range[0])+level_range[0]
sound = Sound.whitenoise(duration, samplerate).atlevel(level)
meta = {'level':level, 'tag':tag, 'duration':duration,
'type':'whitenoise', 'level_range':level_range,
'time':time.gmtime()}
yield sound, location, meta
def powerlawnoise_stimuli(space, duration=100*ms, level=70*dB, alpha=(0, 2),
tag=None):
'''
Note that level and alpha can be a single number or a tuple (min, max) in which case
levels/alphas will be selected uniformly between the two.
The meta information is a dictionary with keys duration, alpha, level, tag,
type='powerlawnoise', level_range, alpha_range and
time=time.gmtime().
'''
if isinstance(level, tuple):
level_range = level
else:
level_range = (level, level)
if isinstance(alpha, tuple):
alpha_range = alpha
else:
alpha_range = (alpha, alpha)
while True:
location = space.get_random_itd()
level = rand()*(level_range[1]-level_range[0])+level_range[0]
alpha = rand()*(alpha_range[1]-alpha_range[0])+alpha_range[0]
sound = powerlawnoise(duration, alpha, samplerate).atlevel(level)
meta = {'level':level, 'tag':tag, 'duration':duration, 'alpha':alpha,
'type':'powerlawnoise', 'level_range':level_range,
'alpha_range':alpha_range,
'time':time.gmtime()}
yield sound, location, meta
class WhiteNoiseStimuli(Stimuli):
__doc__ = whitenoise_stimuli.__doc__
def __init__(self, *args, **kwds):
Stimuli.__init__(self, whitenoise_stimuli, *args, **kwds)
class PowerLawNoiseStimuli(Stimuli):
__doc__ = powerlawnoise_stimuli.__doc__
def __init__(self, *args, **kwds):
Stimuli.__init__(self, powerlawnoise_stimuli, *args, **kwds)
natural_sound_directories = [
#'../../sounds/pittsburgh_natural_sounds/*.wav',
'../../sounds/neotropical_rainforest_mammals/CD 1/*.wav',
'../../sounds/neotropical_rainforest_mammals/CD 2/*.wav',
]
def natural_sounds_stimuli(space, duration=100*ms, level=70*dB,
noiselevel=None,
tag=None,
directories=natural_sound_directories):
'''
Note that level can be a single number or a tuple (min, max) in which case
levels will be selected uniformly between the two.
The meta information is a dictionary with keys duration, level, tag,
type='natural', filename, channel, start, level_range and time=time.gmtime().
'''
if isinstance(level, tuple):
level_range = level
else:
level_range = (level, level)
files = []
for directory in natural_sound_directories:
directory = os.path.join(os.path.split(__file__)[0], directory)
files.extend(glob.glob(directory))
while True:
file = files[randint(len(files))]
sound = loadsound(file)
if sound.duration<duration:
continue
if int(sound.samplerate)!=int(samplerate):
continue
start = rand()*(sound.duration-duration)
channel = randint(sound.nchannels)
sound = sound.channel(channel)
sound = sound[start:start+duration]
sound = Sound(array(asarray(sound), copy=True))
gc.collect()
location = space.get_random_itd()
level = rand()*(level_range[1]-level_range[0])+level_range[0]
try:
sound = sound.atlevel(level)
except OverflowError:
continue
if noiselevel is not None:
sound = sound+whitenoise(sound.duration).atlevel(noiselevel)
meta = {'level':level, 'tag':tag, 'duration':duration,
'noiselevel':noiselevel,
'type':'natural', 'filename':file, 'start':start,
'channel':'channel',
'level_range':level_range,
'time':time.gmtime()}
yield sound, location, meta
class NaturalSoundsStimuli(Stimuli):
__doc__ = natural_sounds_stimuli.__doc__
def __init__(self, *args, **kwds):
Stimuli.__init__(self, natural_sounds_stimuli, *args, **kwds)
def tone_stimuli(space, freqrange, duration=100*ms, level=70*dB,
tag=None):
'''
Note that level can be a single number or a tuple (min, max) in which case
levels will be selected uniformly between the two.
freqrange should be a sequence of at least two elements. Tone frequencies
are chosen by first randomly selecting an interval defined by two successive
elements on freqrange, and then uniformly randomly selecting a frequency
in this interval. By passing the sorted cf array, you will get tones
approximately distributed as the cfs.
The meta information is a dictionary with keys duration, level, tonefreq, tag,
type='tone', level_range, freqrange and
time=time.gmtime().
'''
if isinstance(level, tuple):
level_range = level
else:
level_range = (level, level)
freqrange = array(freqrange)
while True:
location = space.get_random_itd()
level = rand()*(level_range[1]-level_range[0])+level_range[0]
i = randint(len(freqrange)-1)
flow, fhigh = freqrange[i:i+2]
tonefreq = (rand()*(fhigh-flow)+flow)*Hz
sound = tone(tonefreq, duration, samplerate).atlevel(level)
meta = {'level':level, 'tag':tag, 'duration':duration,
'tonefreq':tonefreq,
'type':'tone', 'level_range':level_range,
'freqrange':freqrange,
'time':time.gmtime()}
yield sound, location, meta
class ToneStimuli(Stimuli):
__doc__ = tone_stimuli.__doc__
def __init__(self, *args, **kwds):
Stimuli.__init__(self, tone_stimuli, *args, **kwds)
def bandpassnoise_stimuli(space, freqrange, duration=100*ms, level=70*dB,
tag=None):
'''
Note that level can be a single number or a tuple (min, max) in which case
levels will be selected uniformly between the two.
freqrange should be a sequence of at least two elements. Centre frequencies
are chosen by first randomly selecting an interval defined by two successive
elements on freqrange, and then uniformly randomly selecting a frequency
in this interval. By passing the sorted cf array, you will get bandpass CFs
approximately distributed as the cfs.
The meta information is a dictionary with keys duration, level, bpcfreq, tag,
type='bandpassnoise', level_range, freqrange and
time=time.gmtime().
'''
if isinstance(level, tuple):
level_range = level
else:
level_range = (level, level)
freqrange = array(freqrange)
while True:
location = space.get_random_itd()
level = rand()*(level_range[1]-level_range[0])+level_range[0]
i = randint(len(freqrange)-1)
flow, fhigh = freqrange[i:i+2]
tonefreq = (rand()*(fhigh-flow)+flow)*Hz
sound = Sound(Gammatone(whitenoise(duration, samplerate),
[tonefreq]).process(), samplerate).atlevel(level)
meta = {'level':level, 'tag':tag, 'duration':duration,
'bpcfreq':tonefreq,
'type':'bandpassnoise', 'level_range':level_range,
'freqrange':freqrange,
'time':time.gmtime()}
yield sound, location, meta
class BandPassNoiseStimuli(Stimuli):
__doc__ = tone_stimuli.__doc__
def __init__(self, *args, **kwds):
Stimuli.__init__(self, bandpassnoise_stimuli, *args, **kwds)
if __name__=='__main__':
from spatial import *
space = Spatial(itd_max=100*usecond)
cf = erbspace(100*Hz, 1500*Hz, 480)
f = []
for sound, location, meta in bandpassnoise_stimuli(space, cf):
# f.append(meta['tonefreq'])
# if len(f)>=10000:
# break
for k, v in meta.iteritems():
print k, ':', v
plot(sound)
show()
exit()
hist(f)
show()