Python 中的听力图代码
Audiogram code in Python
我正在编写听力图的代码。
这是代码。
import matplotlib.pyplot as plt
import numpy as np
import pyaudio
freqs =[0.0, 125.0,1000.0,2000.0,3000.0,4000.0, 5000.0, 6000.0, 7000.0, 8000.0]
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=44100,
output=True)
def play_freqs():
volume = 0.1
duration = 1.0 # in seconds, may be float
fs = 44100 # sampling rate, Hz, must be integer
print "===> Starting audiometric test"
i = 0
points = [] #when plotting this is x axis
volumes = [] #when plotting this is y axis
while i<10:
if i == 0:
volume = 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume * samples)
x = raw_input('T/F') #it should continue until sounds of all frequencies from the list are played
if x == 'f':
while True: #it should make unlimited amount of iterations but when it comes across the condition if below; it should stop and continue with x = raw_input
volume = volume + 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume*samples)
if volume == 1.0:
np.append(points, freqs[i])
np.append(volumes, float(volume))
break
if x == 't':
volume = 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume * samples)
np.append(points, freqs[i])
np.append(volumes, float(volume))
i = i + 1 #it should continue with the next element from list freqs
def plot_signal(self, x,y):
plt.plot(np.array(x),np.array(y),'o')
plt.gca().invert_yaxis()
plt.show()
print "Finished"
play_freqs()
stream.stop_stream()
stream.close()
p.terminate()
现在的问题:
当x == 'f'进入while循环时;它没有出来。
我希望每次迭代都检查 if 条件。我的意思是当 x 例如is 'f': 声音应该调大;假设某人在这个频率上完全失聪,因此它应该提高音量直到达到 1.0 的音量。然后应该提示x.rawinput.
当 x == t 时,它应该播放声音,将其追加到列表中,并再次提示 x.rawinput。
即使患者没有听到所有音量的声音,它也应该移动到下一个频率,从而有机会分析其他声音。 x.rawinput 应该继续,直到完成所有频率。
请告诉我,我的代码有什么问题,因为不幸的是它卡在了循环的中间。我不需要直接的解决方案,而是提示我做错了什么。
非常感谢!
利亚
您的迭代器 'i' 仅在 'x' == t 时才会增加。您肯定希望它在每轮检查结束时增加,因此在范围内向上移动吗?
所以:
while i<10:
if i == 0:
volume = 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume * samples)
x = raw_input('T/F') #it should continue until sounds of all frequencies from the list are played
if x == 'f':
while True: #it should make unlimited amount of iterations but when it comes across the condition if below; it should stop and continue with x = raw_input
volume = volume + 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume*samples)
if volume == 1.0:
np.append(points, freqs[i])
np.append(volumes, float(volume))
break
if x == 't':
volume = 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume * samples)
np.append(points, freqs[i])
np.append(volumes, float(volume))
i = i + 1 #it should continue with the next element from list freqs
我正在编写听力图的代码。
这是代码。
import matplotlib.pyplot as plt
import numpy as np
import pyaudio
freqs =[0.0, 125.0,1000.0,2000.0,3000.0,4000.0, 5000.0, 6000.0, 7000.0, 8000.0]
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=44100,
output=True)
def play_freqs():
volume = 0.1
duration = 1.0 # in seconds, may be float
fs = 44100 # sampling rate, Hz, must be integer
print "===> Starting audiometric test"
i = 0
points = [] #when plotting this is x axis
volumes = [] #when plotting this is y axis
while i<10:
if i == 0:
volume = 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume * samples)
x = raw_input('T/F') #it should continue until sounds of all frequencies from the list are played
if x == 'f':
while True: #it should make unlimited amount of iterations but when it comes across the condition if below; it should stop and continue with x = raw_input
volume = volume + 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume*samples)
if volume == 1.0:
np.append(points, freqs[i])
np.append(volumes, float(volume))
break
if x == 't':
volume = 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume * samples)
np.append(points, freqs[i])
np.append(volumes, float(volume))
i = i + 1 #it should continue with the next element from list freqs
def plot_signal(self, x,y):
plt.plot(np.array(x),np.array(y),'o')
plt.gca().invert_yaxis()
plt.show()
print "Finished"
play_freqs()
stream.stop_stream()
stream.close()
p.terminate()
现在的问题:
当x == 'f'进入while循环时;它没有出来。
我希望每次迭代都检查 if 条件。我的意思是当 x 例如is 'f': 声音应该调大;假设某人在这个频率上完全失聪,因此它应该提高音量直到达到 1.0 的音量。然后应该提示x.rawinput.
当 x == t 时,它应该播放声音,将其追加到列表中,并再次提示 x.rawinput。
即使患者没有听到所有音量的声音,它也应该移动到下一个频率,从而有机会分析其他声音。 x.rawinput 应该继续,直到完成所有频率。
请告诉我,我的代码有什么问题,因为不幸的是它卡在了循环的中间。我不需要直接的解决方案,而是提示我做错了什么。
非常感谢!
利亚
您的迭代器 'i' 仅在 'x' == t 时才会增加。您肯定希望它在每轮检查结束时增加,因此在范围内向上移动吗?
所以:
while i<10:
if i == 0:
volume = 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume * samples)
x = raw_input('T/F') #it should continue until sounds of all frequencies from the list are played
if x == 'f':
while True: #it should make unlimited amount of iterations but when it comes across the condition if below; it should stop and continue with x = raw_input
volume = volume + 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume*samples)
if volume == 1.0:
np.append(points, freqs[i])
np.append(volumes, float(volume))
break
if x == 't':
volume = 0.1
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freqs[i] / fs)).astype(np.float32)
stream.write(volume * samples)
np.append(points, freqs[i])
np.append(volumes, float(volume))
i = i + 1 #it should continue with the next element from list freqs