0% found this document useful (0 votes)
12 views

Project Code

This Python code defines functions for conducting a hearing test application using Tkinter for the GUI. It generates random frequencies within a specified range to test each ear. When the user presses buttons to indicate if they heard a tone, it records their threshold levels and plots an audiogram at the end of the test to analyze hearing loss. The code initializes variables, defines functions for playing tones, handling button presses, generating audiograms, and more to implement the full hearing test workflow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Project Code

This Python code defines functions for conducting a hearing test application using Tkinter for the GUI. It generates random frequencies within a specified range to test each ear. When the user presses buttons to indicate if they heard a tone, it records their threshold levels and plots an audiogram at the end of the test to analyze hearing loss. The code initializes variables, defines functions for playing tones, handling button presses, generating audiograms, and more to implement the full hearing test workflow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

# import sounddevice as sd

# import numpy as np
# import random
# import tkinter as tk
# import matplotlib.pyplot as plt

# # Define global variables


# min_frequency = 125
# max_frequency = 16000
# num_frequencies = 2
# current_test_ear = 'Left'
# current_test_e = None

# random_frequency_range_R = []
# random_frequency_range_L = []

# left_ear_thresholds = []
# right_ear_thresholds = []

# current_frequency_index = 0

# def display():
# print(left_ear_thresholds)
# print(right_ear_thresholds)

# def generate_audiogram(frequencies, thresholds, ear_label):


# if any(threshold is not None for threshold in thresholds):
# plt.figure(figsize=(10, 6))
# plt.plot(frequencies, thresholds, 'ro-', label=f"{ear_label} Ear") # Red circles connected
by lines

# plt.title(f"Audiogram - {ear_label} Ear")


# plt.xlabel("Frequency (Hz)")
# plt.ylabel("Hearing Threshold (dB HL)")
# plt.grid(True)
# plt.gca().invert_yaxis()
# plt.xticks(frequencies, rotation=45) # Use frequency labels for x-axis
# plt.legend()
# plt.tight_layout()

# plt.show()
# else:
# print(f"No data collected for {ear_label} ear audiogram.")
# def play_tone(frequency, duration):
# samples = np.linspace(0, duration, int(duration * 44100), endpoint=False)
# tone = 0.5 * np.sin(2 * np.pi * frequency * samples)
# sd.play(tone, 44100)
# sd.wait()

# def next_tone():
# global current_frequency_index

# if current_frequency_index < len(random_frequency_range_L):


# if current_test_ear == 'Left':
# frequency = random_frequency_range_L[current_frequency_index]
# duration = random.uniform(1, 2)
# play_tone(frequency, duration)
# if current_test_ear == 'Right':
# frequency = random_frequency_range_R[current_frequency_index]
# duration = random.uniform(1, 2)
# play_tone(frequency, duration)
# current_frequency_index += 1
# tone_label.config(text=f"Frequency: {frequency} Hz")

# else:
# current_frequency_index = 0 # Reset the index for the right ear test
# test_button.config(state=tk.DISABLED)
# start_right_ear_button.config(state=tk.NORMAL)
# tone_label.config(text="Left Ear Test Completed")
# test_label.config(text="Testing Right Ear")
# # Enable the finish button after left ear test completion
# finish_button.config(state=tk.NORMAL)

# def start_left_ear_test():
# global current_test_ear
# current_test_ear = 'Left'
# next_tone()

# def start_right_ear_test():
# global current_test_ear
# current_test_ear = 'Right'
# start_right_ear_button.config(state=tk.DISABLED)
# next_tone()

# def yes_button_pressed():
# if current_test_ear == 'Left':
# left_ear_thresholds.append(random_frequency_range_L[current_frequency_index - 1])
# print(left_ear_thresholds)

# if current_test_ear == 'Right':
# right_ear_thresholds.append(random_frequency_range_R[current_frequency_index - 1])
# print(right_ear_thresholds)
# next_tone()

# def no_button_pressed():
# if current_test_ear == 'Left':
# left_ear_thresholds.append(None)
# if current_test_ear == 'Right':
# right_ear_thresholds.append(None)
# next_tone()

# def finish_test():
# tone_label.config(text="Both Ear Tests Completed")
# finish_button.config(state=tk.DISABLED)
# display()
# generate_audiogram(random_frequency_range_L, left_ear_thresholds, "Left")
# generate_audiogram(random_frequency_range_R, right_ear_thresholds, "Right")

# def generate_random_frequencies(min_frequency, max_frequency, num_frequencies):


# return [random.uniform(min_frequency, max_frequency) for _ in range(num_frequencies)]

# random_frequency_range_L = generate_random_frequencies(min_frequency,
max_frequency, num_frequencies)
# random_frequency_range_R = generate_random_frequencies(min_frequency,
max_frequency, num_frequencies)

# if __name__ == "__main__":
# root = tk.Tk()
# root.title("Hearing Test Application")

# test_frame = tk.Frame(root)
# test_frame.pack()

# test_label = tk.Label(test_frame, text="Testing Left ear")


# test_label.pack()

# tone_label = tk.Label(test_frame, text="")


# tone_label.pack()

# test_button = tk.Button(test_frame, text="Start Test", command=start_left_ear_test)


# test_button.pack()
# start_right_ear_button = tk.Button(test_frame, text="Start Right Ear Test",
command=start_right_ear_test, state=tk.DISABLED)
# start_right_ear_button.pack()

# yes_button = tk.Button(test_frame, text="Yes", command=yes_button_pressed)


# yes_button.pack()

# no_button = tk.Button(test_frame, text="No", command=no_button_pressed)


# no_button.pack()

# finish_button = tk.Button(test_frame, text="Finish Test", command=finish_test,


state=tk.DISABLED)
# finish_button.pack()

# root.mainloop()

import sounddevice as sd
import numpy as np
import random
import tkinter as tk
import matplotlib.pyplot as plt
import time

# Define global variables


min_frequency = 125
max_frequency = 8000 # Adjust this value as needed
num_frequencies = 5
current_test_ear = 'Left'
current_test_e = None
random_frequency_range_R = []
random_frequency_range_L = []
left_ear_thresholds = []
right_ear_thresholds = []
current_frequency_index = 0

def display():
print("--------------------------------------")
hear = 0
not_hear = 0
for i, threshold in enumerate(thresholds):
if threshold is not None and threshold >= dB_threshold:
hear += 1
else:
not_hear += 1
if hear >= not_hear:
print(f"| At {ear_label} No hearing loss detected.|")
else:
print(f"| At {ear_label} Hearing loss detected.|")
print("-------------------------------------")

def generate_audiogram(frequencies, thresholds, ear_label):


if any(threshold is not None for threshold in thresholds):
plt.figure(figsize=(5, 3))
plt.plot(frequencies, thresholds, 'ro-', label=f"{ear_label} Ear") # Red circles connected by
lines

plt.title(f"Audiogram - {ear_label} Ear")


plt.xlabel("Frequency (Hz)")
plt.ylabel("Hearing Threshold (dB HL)")
plt.grid(True)
plt.gca().invert_yaxis()
plt.xticks(frequencies, rotation=45) # Use frequency labels for x-axis
plt.legend()
plt.tight_layout()

plt.show()
else:
print(f"No data collected for {ear_label} ear audiogram.")

def play_tone(frequency, duration):


samples = np.linspace(0, duration, int(duration * 44100), endpoint=False)
tone = 0.5 * np.sin(2 * np.pi * frequency * samples)
sd.play(tone, 44100)
sd.wait()

def next_tone():
global current_frequency_index

if current_frequency_index < len(random_frequency_range_L):


frequency = random_frequency_range_L[current_frequency_index]
duration = random.uniform(1, 2)
play_tone(frequency, duration)
current_frequency_index += 1
tone_label.config(text=f"Frequency: {frequency} Hz")
else:
current_frequency_index = 0 # Reset the index for the right ear test
test_button.config(state=tk.DISABLED)
start_right_ear_button.config(state=tk.NORMAL)
if current_test_ear == 'Left':
tone_label.config(text="Left Ear Test Completed")
else:
tone_label.config(text="Right Ear Test Completed")
test_label.config(text="Testing Right Ear")
# Enable the finish button after left ear test completion
finish_button.config(state=tk.NORMAL)

def start_left_ear_test():
global current_test_ear
current_test_ear = 'Left'
next_tone()

def start_right_ear_test():
global current_test_ear
current_test_ear = 'Right'
start_right_ear_button.config(state=tk.DISABLED)
next_tone()

def yes_button_pressed():
if current_test_ear == 'Left':
left_ear_thresholds.append(random_frequency_range_L[current_frequency_index - 1])
print(left_ear_thresholds)

if current_test_ear == 'Right':
right_ear_thresholds.append(random_frequency_range_R[current_frequency_index - 1])
print(right_ear_thresholds)
next_tone()

def no_button_pressed():
if current_test_ear == 'Left':
left_ear_thresholds.append(None)
if current_test_ear == 'Right':
right_ear_thresholds.append(None)
next_tone()

def finish_test():
tone_label.config(text="Both Ear Tests Completed")
finish_button.config(state=tk.DISABLED)
# display()
time.sleep(5)
generate_audiogram(random_frequency_range_L, left_ear_thresholds, "Left")
generate_audiogram(random_frequency_range_R, right_ear_thresholds, "Right")

def generate_random_frequencies(min_frequency, max_frequency, num_frequencies):


return [random.uniform(min_frequency, max_frequency) for _ in range(num_frequencies)]

random_frequency_range_L = generate_random_frequencies(min_frequency, max_frequency,


num_frequencies)
random_frequency_range_R = generate_random_frequencies(min_frequency, max_frequency,
num_frequencies)

if __name__ == "__main__":
# def create_new_app(main_app):
# root = tk.Toplevel(main_app)
root = tk.Tk()
root.title("Hearing Test Application")

test_frame = tk.Frame(root)
test_frame.pack(pady=10) # Add padding to the frame

test_label = tk.Label(test_frame, text="Testing Left ear")


test_label.pack(pady=10) # Add padding to the label

tone_label = tk.Label(test_frame, text="")


tone_label.pack(pady=10) # Add padding to the label

test_button = tk.Button(test_frame, text="Start Test", command=start_left_ear_test)


test_button.pack(pady=10) # Add padding to the button

start_right_ear_button = tk.Button(test_frame, text="Start Right Ear Test",


command=start_right_ear_test, state=tk.DISABLED)
start_right_ear_button.pack(pady=10) # Add padding to the button

yes_button = tk.Button(test_frame, text="Yes", command=yes_button_pressed)


yes_button.pack(pady=10) # Add padding to the button

no_button = tk.Button(test_frame, text="No", command=no_button_pressed)


no_button.pack(pady=10) # Add padding to the button

finish_button = tk.Button(test_frame, text="Finish Test", command=finish_test,


state=tk.DISABLED)
finish_button.pack(pady=10) # Add padding to the button

root.geometry("400x600") # Set the window size to fit a mobile phone screen

root.mainloop()

You might also like