Skip to content
Snippets Groups Projects
Commit 0540cc2f authored by Ed Rogers's avatar Ed Rogers
Browse files

Rename bands to freqs and randomise sleep time

parent bcc24f38
Branches
No related tags found
No related merge requests found
...@@ -25,21 +25,22 @@ class HearingTest: ...@@ -25,21 +25,22 @@ class HearingTest:
def __init__(self, library: SoundLibrary, canvas): def __init__(self, library: SoundLibrary, canvas):
self.library = library self.library = library
self.lower_bounds = np.full_like(self.bands, fill_value=self.lower_lim, dtype=float) self.lower_bounds = np.full_like(self.freqs, fill_value=self.lower_lim, dtype=float)
self.upper_bounds = np.full_like(self.bands, fill_value=self.upper_lim, dtype=float) self.upper_bounds = np.full_like(self.freqs, fill_value=self.upper_lim, dtype=float)
self.false_presses = 0 self.false_presses = 0
self.sounds = [] self.sounds = []
self.canvas = canvas self.canvas = canvas
@property @property
def bands(self): def freqs(self):
return self.library.freqs return self.library.freqs
def handle_key_press(self, _time): def handle_key_press(self, _time):
recent_sound = self.sounds[-1] sound = self.sounds[-1]
if not recent_sound.heard and (_time - recent_sound.time <= self.max_response_time): if not sound.heard and (_time - sound.time <= self.max_response_time):
recent_sound.heard = True sound.heard = True
self.set_lower_bound(recent_sound.freq, recent_sound.volume) if self.lower_bounds[self.freqs == sound.freq] < sound.volume:
self.set_lower_bound(sound.freq, sound.volume)
else: else:
self.false_presses += 1 self.false_presses += 1
self.check_for_not_heard(_time) self.check_for_not_heard(_time)
...@@ -48,12 +49,11 @@ class HearingTest: ...@@ -48,12 +49,11 @@ class HearingTest:
def play_next_sound(self): def play_next_sound(self):
# TODO select tone based on results # TODO select tone based on results
volume = random.random() volume = random.random()
freq = random.choice(self.bands) freq = random.choice(self.freqs)
self.library.play(freq, volume) self.library.play(freq, volume)
played_time = time.time() played_time = time.time()
# TODO randomise time next_sleep_time = random.uniform(self.max_response_time*1.2, self.max_response_time*3)
next_sleep_time = 1
self.sounds.append(SoundRecord(freq, volume, played_time)) self.sounds.append(SoundRecord(freq, volume, played_time))
test_finished = False test_finished = False
...@@ -64,17 +64,18 @@ class HearingTest: ...@@ -64,17 +64,18 @@ class HearingTest:
def check_for_not_heard(self, event_time): def check_for_not_heard(self, event_time):
for sound in reversed(self.sounds): for sound in reversed(self.sounds):
delay = event_time - sound.time delay = event_time - sound.time
print(delay) # print(delay)
if sound.heard is not None: if sound.heard is not None:
break break
elif delay > self.max_response_time: elif delay > self.max_response_time:
sound.heard = False sound.heard = False
if self.upper_bounds[self.freqs == sound.freq] > sound.volume:
self.set_upper_bound(sound.freq, sound.volume) self.set_upper_bound(sound.freq, sound.volume)
def set_lower_bound(self, band, volume): def set_lower_bound(self, freq, volume):
self.lower_bounds[self.bands == band] = volume self.lower_bounds[self.freqs == freq] = volume
self.canvas.update_result(self) self.canvas.update_result(self)
def set_upper_bound(self, band, volume): def set_upper_bound(self, freq, volume):
self.upper_bounds[self.bands == band] = volume self.upper_bounds[self.freqs == freq] = volume
self.canvas.update_result(self) self.canvas.update_result(self)
...@@ -56,14 +56,14 @@ class HearingMplCanvas(FigureCanvas): ...@@ -56,14 +56,14 @@ class HearingMplCanvas(FigureCanvas):
self.plot_result(HearingTest(parent.library, self)) self.plot_result(HearingTest(parent.library, self))
def plot_result(self, result: HearingTest): def plot_result(self, result: HearingTest):
inds = np.arange(0, result.bands.size) inds = np.arange(0, result.freqs.size)
self.axes.set_facecolor('k') self.axes.set_facecolor('k')
self.ul_bar = self.axes.bar(inds, result.upper_bounds - self.baseline, color=(0, 0.5, 0), bottom=self.baseline) self.ul_bar = self.axes.bar(inds, result.upper_bounds - self.baseline, color=(0, 0.5, 0), bottom=self.baseline)
self.ll_bar = self.axes.bar(inds, result.lower_bounds - self.baseline, color=(0, 1, 0), bottom=self.baseline) self.ll_bar = self.axes.bar(inds, result.lower_bounds - self.baseline, color=(0, 1, 0), bottom=self.baseline)
xlim = self.axes.get_xlim() xlim = self.axes.get_xlim()
for i in np.arange(result.lower_lim + self.baseline, result.upper_lim, result.get_block_size()): for i in np.arange(result.lower_lim + self.baseline, result.upper_lim, result.get_block_size()):
self.axes.plot(self.axes.get_xlim(), np.ones(2)*i, color='k') self.axes.plot(self.axes.get_xlim(), np.ones(2)*i, color='k')
labels = [str(b) for b in result.bands] labels = [str(b) for b in result.freqs]
labels.insert(0, '') labels.insert(0, '')
self.axes.set_xticklabels(labels) self.axes.set_xticklabels(labels)
self.axes.set_ylim(self.baseline, result.upper_lim - self.baseline) self.axes.set_ylim(self.baseline, result.upper_lim - self.baseline)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment