How can I make sound randomly occur during 10 second presentation of other stimulus?
1 次查看(过去 30 天)
显示 更早的评论
Hi there,
I'm using Psychtoolbox to create an experiment. I have the code for sound, presentation of images, etc. I'm trying to figure out how I might be able to make a 1s sound occur randomly at some point during the 10s presentation of an image?
I don't necessarily need code involving sound, or even Psychtoolbox specific. I'm just wondering if anyone has any suggestions about how to time one stimulus to occur in relation to another.
Thanks!
0 个评论
回答(2 个)
William Frane
2014-12-4
Here's one way of generating a random time:
% Seed the random number generator (otherwise the same random sequence is generated every time)
rng('shuffle'); % Newer versions of MATLAB
%rand('twister', sum(100*clock)); % Older versions of MATLAB
imageTime = 0;
imageDuration = 10;
soundDuration = 1;
% This assumes that you want the sound to be played early enough to ensure that
% its playback has finished by the time the time the image disappears
soundTime = imageTime + rand()*(imageDuration - soundDuration);
Assuming you have some sort of clock available, you could then use these values like this:
% Initialization code
soundHasPlayed = false;
% Experiment loop
currentTime = ReasonablyAccurateClock();
if (currentTime >= imageTime && currentTime <= (imageTime + imageDuration))
drawImage();
end
if (currentTime >= soundTime && ~soundHasPlayed)
playSound();
soundHasPlayed = true;
end
I'm not certain if that's what you were looking for, but hopefully it helps.
Thorsten
2014-12-4
Create a random number between 0 and 9, wait for that time and then play the sound.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!