gpt4 book ai didi

python - 循环多个变量以应用 sound.Sound() Psychopy 函数

转载 作者:行者123 更新时间:2023-12-01 09:05:50 26 4
gpt4 key购买 nike

我正在尝试将 sound.Sound() 函数应用于循环中的 numpy 数组。具体来说,我想循环执行以下操作。 a_wave 到 d_wave 是 numpy 数组。

stim_a = sound.Sound(a_wave)
stim_b = sound.Sound(b_wave)
stim_c = sound.Sound(c_wave)
stim_d = sound.Sound(d_wave)

我已经尝试过下面的循环。

import sys
this = sys.modules[__name__]
import string

letters = list(string.ascii_lowercase)
a_d = letters[:4]
for x in range(len(a_d)):
setattr(this, 'stim_%s' % a_d[x], sound.Sound('%s_wave' % a_d[x]))

但结果出现以下错误:

File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.83.01-py2.7.egg\psychopy\sound.py", line 389, in __init__
self.setSound(value=value, secs=secs, octave=octave, hamming=hamming)
File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.83.01-py2.7.egg\psychopy\sound.py", line 146, in setSound
raise ValueError, "setSound: could not find a sound file named " + value
ValueError: setSound: could not find a sound file named a_wave

如果我能就此事获得一些建议,我将不胜感激。我在下面插入整个代码以供引用。

from __future__ import division
from psychopy import sound
import sys
this = sys.modules[__name__]
import numpy as np
import string

#generate string lists
letters = list(string.ascii_lowercase)
a_d = letters[:4]

sampling_rate = 44100 #in Hz
target_fq = 200
long_dur = 0.150
short_dur = 0.066
short_blank = 0.066
long_blank = 0.150
double_blank = 0.233

#short
short_index = np.array(range(int(round(short_dur / (1/float(sampling_rate)))-1)))
short_wave = np.sin(2*np.pi*target_fq*(short_index/float(sampling_rate)))
short_blank_wave = np.zeros(short_blank*sampling_rate) #Small fudge factor to bring total stim length to 4410

#long
long_index = np.array(range(int(round(long_dur / (1/float(sampling_rate)))-1)))
long_wave = np.sin(2*np.pi*target_fq*(long_index/float(sampling_rate)))
long_blank_wave = np.zeros(long_blank*sampling_rate)

#double blank
double_blank_wave = np.zeros(double_blank*sampling_rate)

#numpy arrays
a_wave = np.concatenate((long_wave, short_blank_wave, long_wave), axis=0)
b_wave = np.concatenate((short_wave, long_blank_wave, long_wave), axis=0)
c_wave = np.concatenate((long_wave, long_blank_wave, short_wave), axis=0)
d_wave = np.concatenate((short_wave, double_blank_wave, short_wave), axis=0)

for x in range(len(a_d)):
setattr(this, 'stim_%s' % a_d[x], sound.Sound('%s_wave' % a_d[x]))

最佳答案

您(以一种非常复杂的方式)只是构造字符串(例如 'a_wave')来传递给声音对象。声音对象自然地将它们解释为要打开的文件名,但失败了。即请注意,字符串 'a_wave' 与您尝试引用的 numpy 数组对象 a_wave 不对应。

你可以简单地做这样的事情:

sound_stim = sound.Sound()

for sound_array in (a_wave, b_wave, c_wave, d_wave):

for entry in sound_array:
sound_stim.setSound(entry)
sound_stim.play()
# probably need some sort of delay loop here, as
# playing doesn't block execution.

但是由于您没有明确表达您想要实现的目标,所以我只是在这里猜测。请注意,数组 a_wave 等根本不需要是 numpy 数组,它们可以只是 Python 列表,这将简化它们的构造。例如

a_wave = [long_wave, short_blank_wave, long_wave]

总的来说,您对代码进行了过度设计。保持简单和Pythonic。

关于python - 循环多个变量以应用 sound.Sound() Psychopy 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52056294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com