How can I make sound randomly occur during 10 second presentation of other stimulus?
3 views (last 30 days)
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.
William Frane on 4 Dec 2014
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);
See this page for info about seeding the RNG in different versions of MATLAB.
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))
if (currentTime >= soundTime && ~soundHasPlayed)
soundHasPlayed = true;
I'm not certain if that's what you were looking for, but hopefully it helps.
Sciences Psychology Psychtoolbox-3: Vision and neuroscience research Timing and presenting 2D and 3D stimuli