How can I make sound randomly occur during 10 second presentation of other stimulus?

2 visualizzazioni (ultimi 30 giorni)
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!

Risposte (2)

William Frane
William Frane il 4 Dic 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))
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
Thorsten il 4 Dic 2014
Create a random number between 0 and 9, wait for that time and then play the sound.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by