How can I make sound randomly occur during 10 second presentation of other stimulus?
Afficher commentaires plus anciens
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!
Réponses (2)
William Frane
le 4 Déc 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.
1 commentaire
Chelsea
le 4 Déc 2014
Catégories
En savoir plus sur Timing and presenting 2D and 3D stimuli dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!