使用Psychopy生成纯音刺激总是报错:
import numpy as np
from psychopy import sound, core, visual, event, data
import random
# === 参数设置 ===
sample_rate = 44100
freq = 1000 # Hz
durations_ms = list(range(30, 201, 10)) # [30, 40, ..., 200]
tone_dict = {}
# === 提前生成多个时长的纯音 ===
for dur_ms in durations_ms:
dur_sec = dur_ms / 1000.0
t = np.linspace(0, dur_sec, int(sample_rate * dur_sec), endpoint=False)
wave = 0.5 * np.sin(2 * np.pi * freq * t) # 正弦波,振幅0.5
tone_dict[dur_ms] = wave
# === 创建窗口 ===
win = visual.Window([800, 600])
fixation = visual.TextStim(win, text='+')
prompt = visual.TextStim(win, text="声音在第几次出现?按 1 或 2")
# === QUEST 设置 ===
quest = data.QuestHandler(startVal=60, startValSd=20, pThreshold=0.75,
gamma=0.5, beta=3.5, delta=0.01,
nTrials=25, minVal=30, maxVal=200)
for stimDur in quest:
stimDur_ms = int(round(stimDur / 10.0) * 10) # 四舍五入到 10ms 精度
stimDur_ms = max(30, min(200, stimDur_ms)) # 边界裁剪
tone_arr = tone_dict[stimDur_ms]
correct_interval = random.choice([1, 2])
# 第一间隔
fixation.draw(); win.flip(); core.wait(0.5)
if correct_interval == 1:
s = sound.Sound(value=tone_arr, sampleRate=sample_rate)
s.play()
core.wait(stimDur_ms / 1000.0)
core.wait(0.5)
# 第二间隔
fixation.draw(); win.flip(); core.wait(0.5)
if correct_interval == 2:
s = sound.Sound(value=tone_arr, sampleRate=sample_rate)
s.play()
core.wait(stimDur_ms / 1000.0)
core.wait(0.5)
# 被试响应
prompt.draw(); win.flip()
keys = event.waitKeys(keyList=['1', '2', 'escape'])
if 'escape' in keys:
break
response = int(keys[0])
quest.addResponse(response == correct_interval)
print(f"估计的听觉阈限为:{quest.quantile():.2f} ms")
win.close()
core.quit()
报错:TypeError: cannot unpack non-iterable NoneType object
该怎么办,快要崩溃了。