-
Notifications
You must be signed in to change notification settings - Fork 5
/
attenuation.py
47 lines (34 loc) · 1.6 KB
/
attenuation.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
import numpy as np
from scipy.interpolate import interp1d
import flyvr.audio.stimuli
class Attenuator(object):
def __init__(self, attenuation_factors):
self.frequencies = list(attenuation_factors.keys())
self.factors = list(attenuation_factors.values())
self.attenuation_factors = attenuation_factors
@classmethod
def load_from_file(cls, filename):
af = np.loadtxt(filename)
frequencies = af[:, 0]
factors = af[:, 1]
attenuation_factors = dict(list(zip(frequencies, factors)))
return cls(attenuation_factors)
def attenuate(self, data, frequency):
# Frequency determines the attenuation, if it is None, then just pass
# back the original data unchanged. Some stimulation data does not
# need attenuation and this handles those cases.
if frequency is None:
return data
# Otherwise, we need attenuate the data based on the frequency and the pre-loaded
# attenuation factors.
else:
# Lookup the attenuation factor, if we find it, scale the data.
try:
attenuation_factor = self.attenuation_factors[frequency]
except:
# If we don't find it, then we need to linearly interpolate between the
# values to get the right factor.
interpolator = interp1d(list(self.attenuation_factors.keys()), list(self.attenuation_factors.values()),
kind='linear')
attenuation_factor = interpolator(frequency)
return data * attenuation_factor