How can I make sound randomly occur during 10 second presentation of other stimulus?
2 views (last 30 days)
Show older comments
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 Comments
Answers (2)
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);
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
on 4 Dec 2014
Create a random number between 0 and 9, wait for that time and then play the sound.
See Also
Categories
Find more on Timing and presenting 2D and 3D stimuli in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!